栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 移动开发 > iOS

yii框架分类树扩展示例

iOS 更新时间:发布时间: 百科书网 趣学号

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



调用方式

表格方式:
复制代码 代码如下:
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',
                '创建时间',
        ),   
    )); ?>

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

TreeWidget.php

复制代码 代码如下:


class TreeWidget extends Widget {
   
    public $dataProvider;

   
    public $arrAll = array();

   
    public $arrIdRelation = array();

   
    public $arrIdRelationSimple = array();

   
    public $arrIdAll = array();

   
    public $arrIdSon = array();

   
    public $arrIdLeaf = array();

   
    public $arrIdRoot = array();

   
    public $arrIdChildren = array();

   
    public $arrIdBackPath = array();

   
    public $strItem = '
{$strSep}{$name}';

   
    public $tableClass  = 'items table table-striped table-bordered table-condensed';

   
    public $dataKey   = array();

   
    public $formatParam = 'name';

   
    public $tableHead   = array();

   
    public $pid = 'pid';

   
    public $treeType = true;       

   
    public $optionValue = 'id';

   
    public $formatTime = array();

   
    public $selectClass = 'class="span3"';

   
    public $defaultSelectValue = array(
        0,'≡ 作为一级栏目 ≡',
    );

   
    public $isMultiple = false;

   
    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));
    }

   
    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;
    }

   
    public function getHtml() {
            return $this->genHtml();
    }

   
    public function getItemName(){           
            $html = '';
            foreach($this->dataKey as $v) {                   
                    if ($this->formatParam == $v)
                            $str = '{$strSep}';
                    else
                            $str = '';

                    $html .= ''.$str.'{$'.$v.'}';
            }
            $html .= '';
            return $html;
    }

   
    public function getTableHead(){
            $html = '';
            foreach($this->tableHead as $v)
                    $html .= ''.$v.'';

            $html .= '';
            return $html;
    }

   
    public function getTable() {                   
            $this->strItem = $this->getItemName();
            $strRe = '

';
            $strRe .= ''.$this->getTableHead().'';
            $strRe .= $this->genHtml();
            $strRe .= '
';
            return $strRe;
    }   

   
    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 = '';
            $strRe = '';           
            return $strRe;
    }

   
    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;
    }

   
    private function helpForGetChildren($arrData) {
            $arrRe = array_keys($arrData);
            foreach ($arrData as $arrTemp) {
                    $arrRe = array_merge($arrRe, $this->helpForGetChildren($arrTemp));
            }
            return $arrRe;
    }

   
    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);
            }
    }

   
    private function genSeparator($intLen) {
            $strRe = '';
            $i = 0;
            while ($i < $intLen) {
                    $strRe .= ' ' . (($i + 1 == $intLen) ? '├' : '│');
                    $i++;
            }

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

   
    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

复制代码 代码如下:



没有找到数据.

转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/167611.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号