yii框架分類樹擴充套件示例

yii框架分類樹擴充套件示例
1 Star2 Stars3 Stars4 Stars5 Stars 給文章打分!
Loading...

提供兩種方式的分類樹格式,表格和下拉框形式的樹形結構
可以自定義表格和下拉框的樣式,自定義以哪一列的引數為格式化資料,自定義層級關係引數,自定義表格列名稱,也可以設定時間的格式化。

呼叫方式

表格方式:
複製程式碼 程式碼如下:
<?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 [email protected]
 * 
 * 表格方式呼叫
    <?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; ?>

您可能感興趣的文章:

Yii檢視CGridView實現操作按鈕定義地址示例Yii CGridView用法例項詳解Yii檢視CGridView列表用法例項分析Yii中CGridView禁止列排序的設定方法Yii中CGridView實現批量刪除的方法Yii中CGridView關聯表搜尋排序方法例項詳解Yii把CGridView文字框換成下拉框的方法Yii2分頁的使用及其擴充套件方法詳解PHP的Yii框架中YiiBase入口類的擴充套件寫法示例YII CLinkPager分頁類擴充套件增加顯示共多少頁YII檢視整合kindeditor擴充套件的方法Yii框架擴充套件CGridView增加匯出CSV功能的方法

相關文章

IOS開發 最新文章