所以在CodeIgniter中,有一个很酷的功能,就是创建一个HTML表,只需将它传递给一个数组并处理标题,等等.对于Laravel来说,有没有人能够在Laravel中使用CI版本?只是四处打听
在Laravel
AFAIK中没有这样的东西,但你可以创建自己的东西,类似(仅限一个想法)这个(一个php
类,不仅仅是Laravel
)
class Table {
protected $table = null;
protected $header = null;
protected $attr = null;
protected $data = null;
public function __construct($data = null, $attr = null, $header = null)
{
if(is_null($data)) return;
$this->data = $data;
$this->attr = $attr;
if(is_array($header)) {
$this->header = $header;
}
else {
if(count($this->data) && $this->is_assoc($this->data[0]) || is_object($this->data[0])) {
$headerKeys = is_object($this->data[0]) ? array_keys((array)$this->data[0]) : array_keys($this->data[0]);
$this->header = array();
foreach ($headerKeys as $value) {
$this->header[] = $value;
}
}
}
return $this;
}
public function build()
{
$atts = '';
if(!is_null($this->attr)) {
foreach ($this->attr as $key => $value) {
$atts .= $key . ' = "' . $value . '" ';
}
}
$table = '<table ' . $atts . ' >';
if(!is_null($this->header)) {
$table .= '<thead><tr>';
foreach ($this->header as $value) {
$table .= '<th>' . ucfirst($value) . '</th>';
}
$table .= '</thead></tr>';
}
$table .= '<tbody>';
foreach ($this->data as $value) {
$table .= $this->createRow($value);
}
$table .= '</tbody>';
$table .= '</table>';
return $this->table = $table;
}
protected function createRow($array = null)
{
if(is_null($array)) return false;
$row = '<tr>';
foreach ($array as $value) {
$row .= '<td>' . $value . '</td>';
}
$row .= '</tr>';
return $row;
}
protected function is_assoc($array){
return is_array($array) && array_diff_key($array, array_keys(array_keys($array)));
}
}
Run Code Online (Sandbox Code Playgroud)
现在,你可以按照下面给出的那样使用它(这里是一个例子.)
$data = array(
array('name' => 'Heera', 'age'=>'35', 'address' =>'Masimpur', 'phone'=>'123456'),
array('name' => 'Usman', 'age'=>'28', 'address' =>'Kamal Gor', 'phone'=>'654321')
);
$attr = array('class'=>'tbl someClass', 'id'=>'myTbl', 'style'=>'width:400px;color:red', 'border'=>'1');
$t = new Table($data, $attr);
echo $t->build();
Run Code Online (Sandbox Code Playgroud)
或者,使用第三个参数设置标题,如
$t = new Table($data, $attr, array('Known As', 'Years', 'Location', 'Contact'));
Run Code Online (Sandbox Code Playgroud)
这只是一个想法,可能会更好.现在只需将此类与Laravel
使用Laravel
规则集成.您可以Html
通过将其注册为服务来扩展课程或将其用作个人课程.看看这个扩展课程的答案laravel
.