Home > php教程 > PHP源码 > 表格输出组件

表格输出组件

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-02 09:14:19
Original
939 people have browsed it
跳至 [1] [全屏预览]
<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ListView
 * 列表组件
 * 
 * 使用方法
 * <?php
 * 带多选框的使用
    html::run('ListView', [
        'datas' => $data['datas'],  
        'columns' => [
            [
                'type' => 'checkbox' , 
                'name' => 'uid',
                'id' => 'user',
                'htmlOptions' => [
                    'style' => 'color:green',
                ]
            ],
            ['name' => 'visitid', 'header' => '随访id', 'value' => '$data["visitid"]'],
            ['name' => 'visitname', 'header' => '方案名称', 'value' => '$data["visitname"]'],
            ['name' => 'joiner_nums', 'header' => '参与人', 'value' => 'VisitDbModel::getJoiner_nums($data)'],
            ['name' => 'realname', 'header' => '创建人', 'value' => '$data["realname"]'],
            ['name' => 'department_name', 'header' => '科室', 'value' => '$data["department_name"]'],
            ['name' => 'ques_nums', 'header' => '问题', 'value' => '$data["ques_nums"]'],                                        
            [
                'type' => 'button' , 
                'header' => '操作' , 
                'template'=>'{invitation}    {detail}',             操作模板配置
                'htmlOptions' => [
                    'class' => 'doit'
                ],
               'buttons' => [
                    'invitation' => [
                        'label' => '邀请参与人',
                        'url' => '/admin/visit/invitation/?visitid=$data["visitid"]',
                        'htmlOptions' => ['class' => 'fllow_add_btn'],
                        'visible' => 'isset($_GET["type"]) && $_GET["type"] == "myvisit" ? true : false',
                    ],
                    'detail' => [
                        'label' => '详情',
                        'url' => '/admin/visit/detail/?visitid=$data["visitid"]',
                        'htmlOptions' => ['class' => 'fllow_add_btn'],
                        'visible' => 'VisitDbModel::test($data)',                       权限控制表达式,结果为true按钮将会显示,否则不显示
                    ]
                ],
            ],
        ], 
    ]);
?>
 * 带多选框和操作列的使用
 * <?php
    html::run('ListView', [
        'datas' => $data['datas'],
        'columns' => [
            [
                'type' => 'checkbox' , 
                'name' => 'uid',
                'id' => 'uid',
                'htmlOptions' => [
                    'style' => 'color:green',
                ]
            ],
            ['name' => 'visitid', 'header' => '随访id', 'value' => '$data["visitid"]'],
            ['name' => 'visitname', 'header' => '方案名称', 'value' => '$data["visitname"]'],
            ['name' => 'joiner_nums', 'header' => '参与人', 'value' => 'VisitDbModel::getJoiner_nums($data)'],
            ['name' => 'realname', 'header' => '创建人', 'value' => '$data["realname"]'],
            ['name' => 'department_name', 'header' => '科室', 'value' => '$data["department_name"]'],
            ['name' => 'ques_nums', 'header' => '问题', 'value' => '$data["ques_nums"]'],                                        
            [
                'type' => 'button' , 
                'header' => '操作' , 
                'template'=>'{invitation}    {detail}',
                'buttons' => [
                    'invitation' => [
                        'label' => '邀请参与人',
                        'url' => '/admin/visit/invitation/?doctor_id=$data["doctor_id"]',
                        'htmlOptions' => ['class' => 'fllow_add_btn'],
                        'visible' => 'true',
                    ],
                    'detail' => [
                        'label' => '详情',
                        'url' => '/admin/visit/detail/?visitid=$data["visitid"]',
                        'htmlOptions' => ['class' => 'fllow_add_btn'],
                        'visible' => 'false',       
                    ]
                ],
            ],
        ],                                    
    ]);
?>
 * 
 * @author 风居住的地方
 */
class ListView {
    /**
     * 接收数据,将会进行数据处理
     * @var type 
     */
    public $datas;
    /**
     * 表格的html选项
     * @var type 
     */
    public $htmlOptions;    
    /**
     * 数据字段
     * @var type 
     */
    public $columns;
    /**
     * 数据为空显示的消息
     * @var type 
     */
    public $emptyMessage = '暂无数据';
      
    public function __construct($datas) {
            foreach ($datas as $k => $v)
                    $this->$k = $v;
                        
            $this->initData();            
    }
    
    public function run() {
            $html = '';
            $html .= html::tag('table', $this->htmlOptions , false);
            ob_start();
            ob_implicit_flush(false);
            $this->renderStart();
            $this->renderContent();            
            $html .= ob_get_clean();  
            $html .= html::endTag('table');            
            
            return $html;
    }
    
    /**
     * 渲染头
     */
    protected function renderStart() {
            $html = '';
            $html .= html::tag('thead');
            $html .= html::tag('tr');
            $html .= $this->_renderStart('th');                        
            $html .= html::endTag('tr');
            $html .= html::endTag('thead');
            echo $html;
    }
    
    protected function _renderStart($tag) {   
         
            if (empty($this->columns)) 
                    return '';
            
            $html = '';
            foreach($this->columns as $data) {
                    if (isset($data['type']) && $data['type'] == 'checkbox') {
                            if (isset($data['htmlOptions']['type']))    unset($data['htmlOptions']['type']);
                            if (isset($data['htmlOptions']['name']))    unset($data['htmlOptions']['name']);
                            if (isset($data['htmlOptions']['id']))    unset($data['htmlOptions']['id']);
                            if (isset($data['htmlOptions']['value']))    unset($data['htmlOptions']['value']);
                        
                            $data['htmlOptions']['type'] = $data['type'];
                            $data['htmlOptions']['name'] = $data['name'].'[]';
                            $data['htmlOptions']['id'] = $data['id'];
                            $data['htmlOptions']['value'] = '';
                            $data['htmlOptions']['onclick'] = 'SelectAll()';
                            
                            $checkbox = html::tag('input', $data['htmlOptions']);
                            $html .= html::tag($tag, [], $checkbox);
                            
                            $html .= $this->regsterJs($data['id'] , $data['name'].'[]');
                            
                    } else {
                            if (isset($data['raw']) && $data['raw'] == 'false') {
                                    eval('$header = '.$data['header'].';');
                                    
                                    if (isset($header)) {
                                            $data['header'] = $header;
                                    }
                            }
                            $html .= html::tag($tag, [], isset($data['header']) ? $data['header'] : '');
                    }
            }
            
            return $html;
    }

    /**
     * 渲染内容
     */
    protected function renderContent() {
            $html = '';
            $html .= html::tag('tbody');            
            $html .= $this->_renderContent('td');                                    
            $html .= html::endTag('tbody');
            echo $html;
    }
    
    /**
     * 初始化原始数据
     */
    protected function initData() {
            if (empty($this->datas))
                return ;
            
            foreach($this->datas as $i => &$data) {
                    foreach ($this->columns as $value) {
                            if (isset($value['type']) && $value['type'] == 'checkbox') {
                                    $data['checkbox'] = $this->_checkData($value , $data);
                            }

                            if (isset($value['type']) && isset($value['header']) && isset($value['template']) && isset($value['buttons'])) 
                                    $data['actions'] = $this->_initData($value , $data);
                    }
            }
            
    }
    
    protected function _checkData($value , &$data) {            
            if (!isset($value['htmlOptions']))
                    $value['htmlOptions'] = [];
            
            $value['htmlOptions']['type'] = 'checkbox';
            $value['htmlOptions']['name'] = $value['name'].'[]';
            $value['htmlOptions']['value'] = '';
                                
            return html::tag('input', $value['htmlOptions']);
    }


    /**
     * 具体初始化执行
     * @param type $value
     * @param array $data
     * @return array
     */
    protected function _initData($value , &$data) {
            $data['actions'] = [];            
            foreach($value['buttons'] as $key => $v) {
                    if (isset($v['visible'])) {
                            eval('$visible = '.$v['visible'].';');

                            if (!$visible) {
                                    $value['template'] = str_replace("{{$key}}", '' , $value['template']);
                                    unset($value['buttons'][$key]);
                                    continue;
                            }
                    }                    
                
                    eval('$url = "'.str_replace('"', '', $v['url']).'";');
                    
                    $v['htmlOptions']['href'] = $url;                                        
                    $data['actions'][$key] = html::tag('a', $v['htmlOptions'], $v['label']);
            }
            
            foreach($data['actions'] as $k => $v) {
                    $value['template'] = str_replace("{{$k}}", $v, $value['template']);
            }
            return $value['template'];
    }
    
    /**
     * 渲染具体内容
     * @param type $tag
     * @return string
     */
    public function _renderContent($tag) {
            $html = '';
            if (empty($this->datas)) {
                    $html .= html::tag('tr');
                    $html .= html::tag($tag, ['colspan' => count($this->columns)], $this->emptyMessage);
                    $html .= html::endTag('tr');
                    return $html;
            }
                        
            foreach($this->datas as $data) {
                    $html .= html::tag('tr');
                    
                    foreach($this->columns as $value) {
                            if (!isset($value['htmlOptions']))
                                    $value['htmlOptions'] = [];
                            
                            if (isset($value['type'])) {
                                    if ($value['type'] == 'checkbox' || $value['type'] == 'button') {
                                            
                                            if ($value['type'] == 'checkbox') {
                                                    if (isset($value['htmlOptions']['type']))    unset($value['htmlOptions']['type']);
                                                    if (isset($value['htmlOptions']['name']))    unset($value['htmlOptions']['name']);

                                                    $value['htmlOptions']['type'] = $value['type'];
                                                    $value['htmlOptions']['name'] = $value['name'].'[]';
                                                    $value['htmlOptions']['value'] = $data[$value['name']];
                                                    
                                                    $checkbox = html::tag('input', $value['htmlOptions']);

                                                    $html .= html::tag($tag, [], $checkbox);
                                            } else {
                                                    $html .= html::tag($tag, $value['htmlOptions'], !isset($data['actions']) ? false : $data['actions']);
                                            }
                                    }                                    
                                    
                            } else {
                                    eval('$v = '.$value['value'].';');

                                    $html .= html::tag($tag, $value['htmlOptions'], $v);
                            }
                    }
                    $html .= html::endTag('tr');
            }
            return $html;
    }
    
    /**
     * 注册js代码
     * @param type $id
     */
    protected function regsterJs($id , $name) {
        $js =<<<JSS
   <script type="text/javascript">
        function SelectAll() {
            var checkboxs=document.getElementsByName("$name");
            for (var i=0;i<checkboxs.length;i++) {
                var e=checkboxs[i];
                if (e.id == '') {
                    e.checked=!e.checked;
                }
            }
        }
    </script>
   
JSS;
        return $js;
    }
}
Copy after login
Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template