Laravel 4:像CodeIgniter一样生成HTML表

时间:2013-10-30 01:45:18

标签: php laravel-4

所以在CodeIgniter中,有一个很酷的功能,就是创建一个HTML表,只需要传递一个数组并处理标题等等。是否还有Laravel的任何内容,是否有人能够在Laravel中使用CI版本?只是四处询问

3 个答案:

答案 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

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

  • 属性()......我认为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