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;
    }
}
로그인 후 복사
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)