Provides two types of classification tree formats, tree structures in the form of tables and drop-down boxes You can customize the styles of tables and drop-down boxes, customize which column of parameters is used as formatted data, customize hierarchical relationship parameters, and customize tables Column name, you can also set the time format. All this can be done automatically for you. If you think it’s good, don’t forget to give it a like...
Calling method
Calling in form
widget('ext.tree.widgets.TreeWidget',array(
'dataProvider' => $dataProvider, // Pass data
' pid' => 'pid', // Set the parent ID
'tableClass' => 'items table table-striped table-bordered table-condensed', // Table style
'formatParam' => 'name', // Set the formatting field
'formatTime' => array( // Set the formatted time parameter
'created'
),
'action' => array(
array(
'label' => ' Edit', // Link name
'url' => array(
'edit' => 'Yii::app()->controller->createUrl("/manage/taosearch/createProduct")', // Generate a connection
),
'urlParams' => array('id','name'), // Set the parameter fields that need to be passed after the url
),
array(
'label' => 'Add ', // Link name
'url' => array(
'add' => 'Yii::app()->controller->createUrl("/manage/taosearch/createProduct")', / / Generate a connection
),
'urlParams' => array('id','name'), // Set the parameter fields that need to be passed after the url
),
),
'tableHead' => array( / / Set table column header information
'Category ID',
'Channel',
'Chinese name',
'English name',
'Initial letter',
'Sort',
'Category level',
'Parent ID ',
'Creation time',
'Operation',
),
)); ?>
Drop-down box method
widget('ext.tree.widgets.TreeWidget', array(
'dataProvider' => $cate, // Pass data
'pid' => 'pid', // Set parent ID
'formatParam' => 'name', // Set formatting field
'treeType' => false, // Output tree format
'selectClass' => 'class="span11"', // Set the drop-down box style
'defaultSelectValue' => array( // Set the default of the drop-down box Values and options
0 , '≡ as a first-level column ≡'
),
)); ?>
Copy code
< td colspan="4" class="empty">No data found.
Copy code
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /**... id' => '7'
'zone' => 'Clothing'
'name' => 'Clothing'
'ename' => 'nanzhuang'
'first' => 'l'
' sort_order' => '8'
'level' => '1'
'pid' => '6'
'created' => '0'
)
)
*
* Table mode call
widget('ext.tree.widgets.TreeWidget',array(
'dataProvider' => $dataProvider, // Pass data
'pid' => 'pid', // Set parent ID
'tableClass' => 'items table table-striped table-bordered table-condensed', // Table style
'formatParam' => 'name', // Set formatting field
'formatTime' = > array( // Set formatted time parameters
'created'
),
'action' => array(
array(
'label' => 'Edit', // Link name
'url' => array(
'edit' => 'Yii::app()->controller->createUrl("/manage/taosearch/createProduct")', // Generate connection
),
'urlParams' => array('id','name'), // Set the parameter fields that need to be passed after the url
),
array(
'label' => 'Add', // Link name
'url' = > array(
'add' => 'Yii::app()->controller->createUrl("/manage/taosearch/createProduct")', // Generate connection
),
'urlParams' = > array('id','name'), // Set the parameter fields that need to be passed after the url
),
),
'tableHead' => array( // Set the table column header information
'Category ID' ,
'Channel',
'Chinese name',
'English name',
'Initial letter',
'Sort',
'Classification level',
'Parent ID',
'Creation time',
'Operation ',
),
)); ?>
*
* Called in drop-down box mode
* widget('ext.tree.widgets.TreeWidget',array(
'dataProvider' = > $cate, // Pass data
'pid' => 'pid', // Set parent ID
'formatParam' => 'name', // Set formatting field
'treeType' => false , // Output tree format
'selectClass' => 'class="span11"', // Set the drop-down box style
'defaultSelectValue' => array( // Set the default value and options of the drop-down box
0 , ' ≡ As a first-level column≡'
),
)); ?>
*/
class TreeWidget extends Widget {
/**
* CArrayDataProvider data object or array data
* Component data receiving parameters (associative array)
* @var Object || array
*/
public $dataProvider;
/**
* Assignment to receive data
* @var type
*/
public $arrAll = array();
/**
* Multidimensional relationship with _ID as key name
* @var type
*/
public $arrIdRelation = array();
/**
* Simplification of multi-dimensional relationships using _ID as key name, used to output tree diagram
* @var type
*/
public $arrIdRelationSimple = array();
/**
* Convert the original data into an array with _ID as the key name
* @var type
*/
public $arrIdAll = array();
/**
* All parent-child relationships
* @var type
*/
public $arrIdSon = array();
/**
*_ID of leaf node
* @var type
*/
public $arrIdLeaf = array();
/**
*_ID of the root node
* @var type
*/
public $arrIdRoot = array();
/**
* 每个节点下的子孙后代_ID
* @var type
*/
public $arrIdChildren = array();
/**
* Each node goes back to the root
* @var type
*/
public $arrIdBackPath = array();
/**
* Output tree structure
* @var type
*/
public $strItem = ' {$strSep}{$name}';
/**
* Set table style
* @var type
*/
public $tableClass = 'items table table-striped table-bordered table-condensed';
/**
* Data field parameter array
* @var type
*/
public $dataKey = array();
/**
* Specify the fields that need to be formatted
* @var type
*/
public $formatParam = 'name';
/**
* Table column name
* @var type
*/
public $tableHead = array();
/**
* Father ID
* @var type
*/
public $pid = 'pid';
/**
* Specify the type of tree
* true table type tree
* false drop-down box type tree
* @var type
*/
public $treeType = true;
/**
* Bind the drop-down box value
* @var type
*/
public $optionValue = 'id';
/**
* Format time
* @var type
*/
public $formatTime = array();
/**
* Drop-down box style
* @var type
*/
public $selectClass = 'class="span3"';
/**
* Set the default value and options of the drop-down box
* @var type
*/
public $defaultSelectValue = array(
0,'≡ 作为一级栏目 ≡',
);
/**
* Set whether the drop-down box has multiple selections
* true for multiple selections
* false for single selection
* @var type
*/
public $isMultiple = false;
/**
* Default value bound to the drop-down box
* @var type
*/
public $bindSelectValue = 0;
/**
* Operation column
* @var type
*/
public $action = array();
/**
* Run
*/
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));
}
/**
* Run
* @param type $datas
* @return type
* @param type $datas
* @return type
*/
private function _run($datas){
foreach ($datas as $data) {
if (!empty($this->action) && count($this->action) > 0) {
foreach ($this->action as $key => $action) {
$k = array_keys($action['url']);
$data[$k[0]] = '';
}
}
$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;
}
/**
* Get html
* @return type
*/
public function getHtml() {
return $this->genHtml();
}
/**
* Set hierarchical fields
* Table type
* @return string
*/
public function getItemName(){
$html = '';
foreach($this->dataKey as $v) {
if ($this->formatParam == $v)
$str = '{$strSep}';
else
$str = '';
$html .= ''.$str.'{$'.$v.'} ';
}
$html .= ' ';🎜 return $html;🎜 }
/**
* Get table column name
* @return string
*/
public function getTableHead(){
$html = '';
foreach($this->tableHead as $v)
$html .= ''.$v.' ';
$html .= ' ';
return $html;
}
/**
* Get the tree in tabular form
* @return string
*/
public function getTable() {
$this->strItem = $this->getItemName();
$strRe = '';
$strRe .= ''.$this->getTableHead().' ';
$strRe .= $this->genHtml();
$strRe .= '
';
return $strRe;
}
/**
* Get the tree in the form of drop-down box
* @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 = '{$strSep}{$'.$this->formatParam.'} ';
$strRe = ' $strRe .= ($blmMulti ? ' multiple="multiple"' : '') . (empty($strExt) ? '' : ' ' . $strExt) . '>';
if (is_array($arrFirst) && count($arrFirst) == 2) {
$strRe .= '' . $arrFirst[1] . ' ';
}
$strRe .= $this->getHtml() . '';
return $strRe;
}
/**
* Data processing
* @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;
}
/**
* Data processing
* @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;
}
/**
* Data processing
* @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;
}
/**
*Data processing
*/
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);
}
}
/**
* Data processing
* @param type $intLen
* @return string
* @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; } /** * Data processing * @param type $arrRelation * @param type $intSep * @return type * @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]);
}
}
}
if (!empty($this->action) && count($this->action) > 0) {
foreach ($this->action as $key => $action) {
$k = array_keys($action['url']);
$url = eval('return '.$action['url'][$k[0]].';'); 🎜 if (isset($action['urlParams']) && count($action['urlParams']) > 0) { 🎜 foreach($action['urlParams'] as $urlParams) { 🎜 $url .= '/'.$urlParams.'/'.$this->arrIdAll[$strKey][$urlParams];🎜 }🎜 }
$this->arrIdAll[$strKey][$k[0]] = CHtml::link($action['label'], $url, $action['options']);;
}
}
$strSep = $this->genSeparator($intSep);
extract($this->arrIdAll[$strKey]);
eval('$strRe .= "' . $this->strItem . '";');
count($arrTemp) > 0 && $strRe .= $this->genHtml($arrTemp, ($intSep + 1));
}
}
return $strRe;
}
}
?>
复制代码