douke6424 2013-10-29 17:45
浏览 25
已采纳

Laravel 4:像CodeIgniter一样生成HTML表

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

  • 写回答

3条回答 默认 最新

  • dscs63759 2013-10-29 22:44
    关注

    There is nothing like this in Laravel AFAIK but you may create your own, something like (an idea only) this (a php class, not only for 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)));
        }
    }
    

    Now, you can use it as given below (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();
    

    Or, set an header using third argument, like

    $t = new Table($data, $attr, array('Known As', 'Years', 'Location', 'Contact'));
    

    This is just an idea and could be better. Now just integrate this class with Laravel using Laravel's rule. You may extend Html class or use as an individual class by registering it as a service. Take a look at this answer for extending a class in laravel.

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部