1

So in CodeIgniter, there was a cool feature of creating an HTML table just by passing it an array and it handles the header, etc. Is there ANYTHING out there for Laravel, has anybody been able to use the CI version in Laravel? just asking around

4

3 回答 3

6

在 AFAIK 中没有这样的东西,Laravel但你可以创建自己的东西,比如(一个想法)这个(一个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)));
    }
}

现在,您可以按照下面给出的方式使用它(此处的示例。

$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'));

这只是一个想法,可能会更好。现在只需将此类与LaravelusingLaravel的规则集成。您可以Html通过将其注册为服务来扩展类或将其用作单独的类。看看这个答案以扩展laravel.

于 2013-10-30T06:44:15.663 回答
2

试试nayjest/grids Laravel 包。

于 2015-04-07T14:59:54.263 回答
0

您可以使用脚本 drupal 并将其转换为 laravel:https ://api.drupal.org/api/drupal/core%21includes%21theme.inc/function/theme_table/8

快速浏览一下,您只需要替换几个函数:

  • Attributes()... 我认为 laravel 有这样的东西你可以使用。我认为 Drupal 8 正在使用 symfony2 中的一些东西...... laravel 可能正在使用同样的东西。
  • _theme_table_cell()、tablesort_header() 等......这些类型的函数中有 drupal_render()......你不想尝试移植它......所以你可能只是从函数中删除它(未经测试) 因为它在 laravel 的上下文中没有意义。

这将非常好地实现您正在寻找的内容。

编辑:我也遇到了这个: http: //kohana.keyframesandcode.com/docs/modules/table/ 我没有测试过,但这里引用了它:http ://forumsarchive.laravel.io/viewtopic.php ?id=2178

于 2014-04-04T02:46:59.863 回答