服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|

服务器之家 - 编程语言 - PHP教程 - yii框架分类树扩展示例

yii框架分类树扩展示例

2020-12-15 16:42PHP教程网 PHP教程

这篇文章主要介绍了yii框架分类树扩展示例,提供两种方式的分类树格式,表格和下拉框形式的树形结构,需要的朋友可以参考下

提供两种方式的分类树格式,表格和下拉框形式的树形结构
可以自定义表格和下拉框的样式,自定义以哪一列的参数为格式化数据,自定义层级关系参数,自定义表格列名称,也可以设置时间的格式化。

yii框架分类树扩展示例

yii框架分类树扩展示例



调用方式

表格方式:

复制代码 代码如下:

<?php $this->widget('ext.tree.widgets.TreeWidget',array(
        'dataProvider'  => $dataProvider,           // 传递数据
        'pid'           => 'pid',                   // 设置层级关系id
        'tableClass'    => 'items table table-striped table-bordered table-condensed',  // 表格样式
        'formatParam'   => 'name',                  // 设置格式化字段   
        'formatTime'    => array(                   // 设置格式化的时间参数
            'created'
        ),              
        'tableHead'     => array(                   // 设置表格列头信息
                '分类ID',
                '频道',
                '中文名',
                '英文名',
                '首字母',
                '排序',
                '分类级别',
                '父ID',
                '创建时间',
        ),   
    )); ?>

 

下拉框方式

复制代码 代码如下:

<?php $this->widget('ext.tree.widgets.TreeWidget',array(
            'dataProvider'  => $cate,           // 传递数据
            'pid'           => 'pid',                   // 设置父ID           
            'formatParam'   => 'name',                  // 设置格式化字段
            'treeType'      => false,                   // 输出树格式
            'selectClass'  => 'class="span11"',         // 设置下拉框样式
             'defaultSelectValue' => array(             // 设置下拉框的默认值和选项
                    0 , '≡ 作为一级栏目 ≡'
             ),
        )); ?>

 

TreeWidget.php

 

复制代码 代码如下:


<?php

 

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

/**
 * Description of Tree
 *
 * @author 汪嘉诚
 * @email 819434425@qq.com
 * 
 * 表格方式调用
    <?php $this->widget('ext.tree.widgets.TreeWidget',array(
        'dataProvider'  => $dataProvider,           // 传递数据
        'pid'           => 'pid',                   // 设置层级关系id
        'tableClass'    => 'items table table-striped table-bordered table-condensed',  // 表格样式
        'formatParam'   => 'name',                  // 设置格式化字段   
        'formatTime'    => array(                   // 设置格式化的时间参数
            'created'
        ),              
        'tableHead'     => array(                   // 设置表格列头信息
                '分类ID',
                '频道',
                '中文名',
                '英文名',
                '首字母',
                '排序',
                '分类级别',
                '父ID',
                '创建时间',
        ),   
    )); ?>
 *
 * 下拉框方式调用
 * <?php $this->widget('ext.tree.widgets.TreeWidget',array(
            'dataProvider'  => $cate,           // 传递数据
            'pid'           => 'pid',                   // 设置父ID           
            'formatParam'   => 'name',                  // 设置格式化字段
            'treeType'      => false,                   // 输出树格式
            'selectClass'  => 'class="span11"',         // 设置下拉框样式
             'defaultSelectValue' => array(             // 设置下拉框的默认值和选项
                    0 , '≡ 作为一级栏目 ≡'
             ),
        )); ?>
 */
class TreeWidget extends Widget {
    /**
     * CArrayDataProvider 数据对象或数组数据
     * 组件数据接收参数
     * @var Object || array
     */
    public $dataProvider;

    /**
     * 赋值接收数据
     * @var type
     */
    public $arrAll = array();

    /**
     * 按_ID作键名的多维关系
     * @var type
     */
    public $arrIdRelation = array();

    /**
     * 按_ID作键名的多维关系的简化,用来输出树状图
     * @var type
     */
    public $arrIdRelationSimple = array();

    /**
     * 将原始数据转化成的_ID作键名的数组
     * @var type
     */
    public $arrIdAll = array();

    /**
     * 所有的父子关系
     * @var type
     */
    public $arrIdSon = array();

    /**
     * 叶子节点的_ID
     * @var type
     */
    public $arrIdLeaf = array();

    /**
     * 根节点的_ID
     * @var type
     */
    public $arrIdRoot = array();

    /**
     * 每个节点下的子孙后代_ID
     * @var type
     */
    public $arrIdChildren = array();

    /**
     * 每个节点回逆到根
     * @var type
     */
    public $arrIdBackPath = array();

    /**
     * 输出树的结构
     * @var type
     */
    public $strItem = '<br />{$strSep}{$name}';

    /**
     * 设置表格样式
     * @var type
     */
    public $tableClass  = 'items table table-striped table-bordered table-condensed';

    /**
     * 数据字段参数数组
     * @var type
     */
    public $dataKey   = array();

    /**
     * 指定需要格式化的字段
     * @var type
     */
    public $formatParam = 'name';

    /**
     * 表格列名称
     * @var type
     */
    public $tableHead   = array();

    /**
     * 父ID
     * @var type
     */
    public $pid = 'pid';

    /**
     * 指定树的类型
     * true 表格类型树
     * false 下拉框类型树
     * @var type
     */
    public $treeType = true;       

    /**
     * 绑定下拉框value值
     * @var type
     */
    public $optionValue = 'id';

    /**
     * 格式化时间
     * @var type
     */
    public $formatTime = array();

    /**
     * 下拉框样式
     * @var type
     */
    public $selectClass = 'class="span3"';

    /**
     * 设置下拉框的默认值和选项
     * @var type
     */
    public $defaultSelectValue = array(
        0,'≡ 作为一级栏目 ≡',
    );

    /**
     * 设置下拉框是否多选
     * true 多选
     * false 单选
     * @var type
     */
    public $isMultiple = false;

    /**
     * 绑定到下拉框的默认值
     * @var type
     */
    public $bindSelectValue = 0;
   

    /**
     * 运行
     */
    public function run() {               
            if (is_array($this->dataProvider) && count($this->dataProvider) > 0)
                    $data = $this->_run($this->dataProvider);
            else if (is_object($this->dataProvider) && count($this->dataProvider->rawData) > 0)
                    $data = $this->_run($this->dataProvider->rawData);                   

                               
            $this->render('tree' , array('data'=>$data));
    }

    /**
     *
     * @return type
     */
    private function _run($datas){           
            foreach ($datas as $data)
                    $this->arrAll[] = $data;
                    $this->dataKey = array_keys($data);

            $this->processData();
            if ($this->treeType === true)
                    $data = $this->getTable();
            else
                    $data = $this->getSelect($this->pid, $this->bindSelectValue, $this->isMultiple, $this->selectClass, $this->defaultSelectValue);

            return $data;
    }

    /**
     * 获得html
     * @return type
     */
    public function getHtml() {
            return $this->genHtml();
    }

    /**
     * 设置分层字段
     * 表格类型
     * @return string
     */
    public function getItemName(){           
            $html = '<tr>';
            foreach($this->dataKey as $v) {                   
                    if ($this->formatParam == $v)
                            $str = '{$strSep}';
                    else
                            $str = '';

                    $html .= '<td>'.$str.'{$'.$v.'}</td>';
            }
            $html .= '</tr>';
            return $html;
    }

    /**
     * 获取表格列名称
     * @return string
     */
    public function getTableHead(){
            $html = '<tr>';
            foreach($this->tableHead as $v)
                    $html .= '<th>'.$v.'</th>';

            $html .= '</tr>';
            return $html;
    }

    /**
     * 获得表格形式的树
     * @return string
     */
    public function getTable() {                   
            $this->strItem = $this->getItemName();
            $strRe = '<table class="'.$this->tableClass.'">';
            $strRe .= '<thead>'.$this->getTableHead().'</thead><tbody>';
            $strRe .= $this->genHtml();
            $strRe .= '</tbody></table>';
            return $strRe;
    }   

    /**
     * 获取下拉框形式的树
     * @param type $strName
     * @param array $arrValue
     * @param type $blmMulti
     * @param type $strExt
     * @param type $arrFirst
     * @return string
     */
    public function getSelect($strName = 'tree', $arrValue = array(), $blmMulti = false, $strExt = '', $arrFirst = null) {
            !is_array($arrValue) && $arrValue = array($arrValue);
            foreach ($this->arrIdAll as $strTemp => $arrTemp) {
                    $this->arrIdAll[$strTemp]['selected'] = '';

                    if (in_array($arrTemp['id'], $arrValue)) {
                            $this->arrIdAll[$strTemp]['selected'] = ' selected="selected"';
                    }
            }
            $this->strItem = '<option value=\"{$'.$this->optionValue.'}\"{$selected} id="code79980">
<?php  if (!empty($data)): ?>
<?php echo $data;  ?>
<?php else:  ?>
<tr><td colspan="4" class="empty"><span class="empty">没有找到数据.</span></td></tr>
<?php endif; ?>

 

延伸 · 阅读

精彩推荐