所以在CodeIgniter中,有一个很酷的功能,就是创建一个HTML表,只需要传递一个数组并处理标题等等。是否还有Laravel的任何内容,是否有人能够在Laravel中使用CI版本?只是四处询问
答案 0 :(得分:6)
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)));
}
}
现在,您可以按照下面的说明使用它(An Example Here.)
$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();
或者,使用第三个参数设置标题,如
$t = new Table($data, $attr, array('Known As', 'Years', 'Location', 'Contact'));
这只是一个想法,可能会更好。现在只需使用Laravel
的规则将此类与Laravel
集成。您可以通过将其注册为服务来扩展Html
类或作为单个类使用。请查看this answer以扩展laravel
中的课程。
答案 1 :(得分:2)
试试nayjest/grids Laravel套餐。
答案 2 :(得分:0)
您可以使用drupal使用的脚本并将其转换为laravel:https://api.drupal.org/api/drupal/core%21includes%21theme.inc/function/theme_table/8
快速浏览一下,你只需要替换几个函数:
这样可以很好地实现您所寻找的目标。
编辑:我也遇到过这样的问题:http://kohana.keyframesandcode.com/docs/modules/table/我没有对其进行过测试,但在此引用了它:http://forumsarchive.laravel.io/viewtopic.php?id=2178