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

表格输出组件

Jun 02, 2016 am 09:14 AM

跳至 [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;
    }
}
Nach dem Login kopieren
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

R.E.P.O. Energiekristalle erklärten und was sie tun (gelber Kristall)
2 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
Repo: Wie man Teamkollegen wiederbelebt
1 Monate vor By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Abenteuer: Wie man riesige Samen bekommt
4 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)