欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

yii框架分类树扩展示例

程序员文章站 2022-06-16 13:20:32
提供两种方式的分类树格式,表格和下拉框形式的树形结构可以自定义表格和下拉框的样式,自定义以哪一列的参数为格式化数据,自定义层级关系参数,自定义表格列名称,也可以设置时间的格...

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

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} title=\"{$'.$this->formatparam.'}\">{$strsep}{$'.$this->formatparam.'}</option>';
            $strre = '<select id="id_' . $strname . '" name="' . $strname . ($blmmulti ? '[]' : '') . '"';
            $strre .= ($blmmulti ? ' multiple="multiple"' : '') . (empty($strext) ? '' : ' ' . $strext) . '>';

            if (is_array($arrfirst) && count($arrfirst) == 2) {
                    $strre .= '<option value="' . $arrfirst[0] . '">' . $arrfirst[1] . '</option>';
            }

            $strre .= $this->gethtml() . '</select>';           
            return $strre;
    }

    /**
     * 数据处理
     * @param type $arrdata
     * @return type
     */
    private function helpforgetrelation($arrdata) {
            $arrre = array();
            foreach ($arrdata as $strtemp => $arrtemp) {
                    $arrre[$strtemp] = $arrtemp;
                    if (isset($this->arridrelation[$strtemp])) {
                            $arrre[$strtemp] = $this->arridrelation[$strtemp];
                    }
                    if (count($arrre[$strtemp]) > 0) {
                            $arrre[$strtemp] = $this->helpforgetrelation($arrre[$strtemp]);
                    } else {
                            array_push($this->arridleaf, $strtemp);
                    }
            }
            return $arrre;
    }

    /**
     * 数据处理
     * @param type $arrdata
     * @return type
     */
    private function helpforgetchildren($arrdata) {
            $arrre = array_keys($arrdata);
            foreach ($arrdata as $arrtemp) {
                    $arrre = array_merge($arrre, $this->helpforgetchildren($arrtemp));
            }
            return $arrre;
    }

    /**
     * 数据处理
     * @param type $str
     * @return type
     */
    private function helpforgetbackpath($str) {
            $arrre = array();
            $inttemp = $this->arridall[$str][$this->pid];
            if ($inttemp > 0) {
                    $inttemp = '_' . $inttemp;
                    array_push($arrre, $inttemp);
                    $arrre = array_merge($arrre, $this->helpforgetbackpath($inttemp));
            }
            return $arrre;
    }

    /**
     * 数据处理
     */
    private function processdata() {
            $count = count($this->arrall);
            foreach ($this->arrall as $arrtemp) {           
                    $strtemp = '_' . $arrtemp['id'];
                    $this->arridall[$strtemp] = $arrtemp;
                    if ($arrtemp[$this->pid] > 0 && $count > 1) {
                            $strtemp_ = '_' . $arrtemp[$this->pid];
                            !isset($this->arridrelation[$strtemp_]) && $this->arridrelation[$strtemp_] = array();
                            $this->arridrelation[$strtemp_][$strtemp] = array();
                            !isset($this->arridson[$strtemp_]) && $this->arridson[$strtemp_] = array();
                            array_push($this->arridson[$strtemp_], $strtemp);
                    } else {
                            !isset($this->arridrelation[$strtemp]) && $this->arridrelation[$strtemp] = array();
                            array_push($this->arridroot, $strtemp);
                    }
            }

            $this->arridrelation = $this->helpforgetrelation($this->arridrelation);
            $this->arridleaf = array_unique($this->arridleaf);
            foreach ($this->arridrelation as $strtemp => $arrtemp) {
                    $this->arridchildren[$strtemp] = $this->helpforgetchildren($arrtemp);
                    in_array($strtemp, $this->arridroot) && $this->arridrelationsimple[$strtemp] = $arrtemp;
            }
            $arrtemp = array_keys($this->arridall);
            foreach ($arrtemp as $strtemp) {
                    $this->arridbackpath[$strtemp] = $this->helpforgetbackpath($strtemp);
            }
    }

    /**
     * 数据处理
     * @param type $intlen
     * @return string
     */
    private function genseparator($intlen) {
            $strre = '';
            $i = 0;
            while ($i < $intlen) {
                    $strre .= ' ' . (($i + 1 == $intlen) ? '├' : '│');
                    $i++;
            }

            !empty($strre) && $strre .= '─';
            return $strre;
    }

    /**
     * 数据处理
     * @param type $arrrelation
     * @param type $intsep
     * @return type
     */
    private function genhtml($arrrelation = null, $intsep = 0) {
            $strre = '';
            null === $arrrelation && $arrrelation = $this->arridrelationsimple;
            foreach ($arrrelation as $strkey => $arrtemp) {
                    if (count($this->arridall[$strkey]) > 0) {
                            if (!empty($this->formattime) && count($this->formattime) > 0) {
                                    foreach($this->formattime as $formattime) {
                                            if ($this->arridall[$strkey][$formattime] > 0) {
                                                    $this->arridall[$strkey][$formattime] = date('y-m-d h:i:s' , $this->arridall[$strkey][$formattime]);
                                            }
                                    }                                   
                            }

                            $strsep = $this->genseparator($intsep);                       
                            extract($this->arridall[$strkey]);
                            eval('$strre .= "' . $this->stritem . '";');                                               
                            count($arrtemp) > 0 && $strre .= $this->genhtml($arrtemp, ($intsep + 1));
                    }
            }           
            return $strre;
    }
}
?>

tree.php

复制代码 代码如下:

<?php  if (!empty($data)): ?>
<?php echo $data;  ?>
<?php else:  ?>
<tr><td colspan="4" class="empty"><span class="empty">没有找到数据.</span></td></tr>
<?php endif; ?>