提供兩種方式的分類樹格式,表格和下拉框形式的樹形結(jié)構(gòu)
可以自定義表格和下拉框的樣式,自定義以哪一列的參數(shù)為格式化數(shù)據(jù),自定義層級關(guān)系參數(shù),自定義表格列名稱,也可以設(shè)置時間的格式化。
調(diào)用方式
表格方式:
下拉框方式
TreeWidget.php
/*
?* To change this template, choose Tools | Templates
?* and open the template in the editor.
?*/
/**
?* Description of Tree
?*
?* @author 汪嘉誠
?* @email [email protected]
?*?
?* 表格方式調(diào)用
??? widget('ext.tree.widgets.TreeWidget',array(
??????? 'dataProvider'? => $dataProvider,?????????? // 傳遞數(shù)據(jù)
??????? 'pid'?????????? => 'pid',?????????????????? // 設(shè)置層級關(guān)系id
??????? 'tableClass'??? => 'items table table-striped table-bordered table-condensed',? // 表格樣式
??????? 'formatParam'?? => 'name',????????????????? // 設(shè)置格式化字段???
??????? 'formatTime'??? => array(?????????????????? // 設(shè)置格式化的時間參數(shù)
??????????? 'created'
??????? ),??????????????
??????? 'tableHead'???? => array(?????????????????? // 設(shè)置表格列頭信息
??????????????? '分類ID',
??????????????? '頻道',
??????????????? '中文名',
??????????????? '英文名',
??????????????? '首字母',
??????????????? '排序',
??????????????? '分類級別',
??????????????? '父ID',
??????????????? '創(chuàng)建時間',
??????? ),???
??? )); ?>
?*
?* 下拉框方式調(diào)用
?* widget('ext.tree.widgets.TreeWidget',array(
??????????? 'dataProvider'? => $cate,?????????? // 傳遞數(shù)據(jù)
??????????? 'pid'?????????? => 'pid',?????????????????? // 設(shè)置父ID???????????
??????????? 'formatParam'?? => 'name',????????????????? // 設(shè)置格式化字段
??????????? 'treeType'????? => false,?????????????????? // 輸出樹格式
??????????? 'selectClass'? => 'class="span11"',???????? // 設(shè)置下拉框樣式
???????????? 'defaultSelectValue' => array(???????????? // 設(shè)置下拉框的默認(rèn)值和選項
??????????????????? 0 , '≡ 作為一級欄目 ≡'
???????????? ),
??????? )); ?>
?*/
class TreeWidget extends Widget {
??? /**
???? * CArrayDataProvider 數(shù)據(jù)對象或數(shù)組數(shù)據(jù)
???? * 組件數(shù)據(jù)接收參數(shù)
???? * @var Object || array
???? */
??? public $dataProvider;
??? /**
???? * 賦值接收數(shù)據(jù)
???? * @var type
???? */
??? public $arrAll = array();
??? /**
???? * 按_ID作鍵名的多維關(guān)系
???? * @var type
???? */
??? public $arrIdRelation = array();
??? /**
???? * 按_ID作鍵名的多維關(guān)系的簡化,用來輸出樹狀圖
???? * @var type
???? */
??? public $arrIdRelationSimple = array();
??? /**
???? * 將原始數(shù)據(jù)轉(zhuǎn)化成的_ID作鍵名的數(shù)組
???? * @var type
???? */
??? public $arrIdAll = array();
??? /**
???? * 所有的父子關(guān)系
???? * @var type
???? */
??? public $arrIdSon = array();
??? /**
???? * 葉子節(jié)點(diǎn)的_ID
???? * @var type
???? */
??? public $arrIdLeaf = array();
??? /**
???? * 根節(jié)點(diǎn)的_ID
???? * @var type
???? */
??? public $arrIdRoot = array();
??? /**
???? * 每個節(jié)點(diǎn)下的子孫后代_ID
???? * @var type
???? */
??? public $arrIdChildren = array();
??? /**
???? * 每個節(jié)點(diǎn)回逆到根
???? * @var type
???? */
??? public $arrIdBackPath = array();
??? /**
???? * 輸出樹的結(jié)構(gòu)
???? * @var type
???? */
??? public $strItem = '
{$strSep}{$name}';
??? /**
???? * 設(shè)置表格樣式
???? * @var type
???? */
??? public $tableClass? = 'items table table-striped table-bordered table-condensed';
??? /**
???? * 數(shù)據(jù)字段參數(shù)數(shù)組
???? * @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"';
??? /**
???? * 設(shè)置下拉框的默認(rèn)值和選項
???? * @var type
???? */
??? public $defaultSelectValue = array(
??????? 0,'≡ 作為一級欄目 ≡',
??? );
??? /**
???? * 設(shè)置下拉框是否多選
???? * true 多選
???? * false 單選
???? * @var type
???? */
??? public $isMultiple = false;
??? /**
???? * 綁定到下拉框的默認(rèn)值
???? * @var type
???? */
??? public $bindSelectValue = 0;
???
??? /**
???? * 運(yùn)行
???? */
??? 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();
??? }
??? /**
???? * 設(shè)置分層字段
???? * 表格類型
???? * @return string
???? */
??? public function getItemName(){???????????
??????????? $html = '
??? /**
???? * 獲得表格形式的樹
???? * @return string
???? */
??? public function getTable() {???????????????????
??????????? $this->strItem = $this->getItemName();
??????????? $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 = '';
??????????? $strRe = '';???????????
??????????? return $strRe;
??? }
??? /**
???? * 數(shù)據(jù)處理
???? * @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;
??? }
??? /**
???? * 數(shù)據(jù)處理
???? * @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;
??? }
??? /**
???? * 數(shù)據(jù)處理
???? * @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;
??? }
??? /**
???? * 數(shù)據(jù)處理
???? */
??? 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);
??????????? }
??? }
??? /**
???? * 數(shù)據(jù)處理
???? * @param type $intLen
???? * @return string
???? */
??? private function genSeparator($intLen) {
??????????? $strRe = '';
??????????? $i = 0;
??????????? while ($i ??????????????????? $strRe .= ' ' . (($i + 1 == $intLen) ? '├' : '│');
??????????????????? $i++;
??????????? }
??????????? !empty($strRe) && $strRe .= '─';
??????????? return $strRe;
??? }
??? /**
???? * 數(shù)據(jù)處理
???? * @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
新聞熱點(diǎn)
疑難解答
圖片精選