Home > Backend Development > PHP Tutorial > Share a powerful classification tree extension of the Yii framework

Share a powerful classification tree extension of the Yii framework

WBOY
Release: 2016-07-25 08:48:14
Original
877 people have browsed it
< td colspan="4" class="empty">No data found.
  • Copy code
    1. /*
    2. * To change this template, choose Tools | Templates
    3. * and open the template in the editor.
    4. */
    5. /**... id' => '7'
    6. 'zone' => 'Clothing'
    7. 'name' => 'Clothing'
    8. 'ename' => 'nanzhuang'
    9. 'first' => 'l'
    10. ' sort_order' => '8'
    11. 'level' => '1'
    12. 'pid' => '6'
    13. 'created' => '0'
    14. )
    15. )
    16. *
    17. * Table mode call
    18. widget('ext.tree.widgets.TreeWidget',array(
    19. 'dataProvider' => $dataProvider, // Pass data
    20. 'pid' => 'pid', // Set parent ID
    21. 'tableClass' => 'items table table-striped table-bordered table-condensed', // Table style
    22. 'formatParam' => 'name', // Set formatting field
    23. 'formatTime' = > array( // Set formatted time parameters
    24. 'created'
    25. ),
    26. 'action' => array(
    27. array(
    28. 'label' => 'Edit', // Link name
    29. 'url' => array(
    30. 'edit' => 'Yii::app()->controller->createUrl("/manage/taosearch/createProduct")', // Generate connection
    31. ),
    32. 'urlParams' => array('id','name'), // Set the parameter fields that need to be passed after the url
    33. ),
    34. array(
    35. 'label' => 'Add', // Link name
    36. 'url' = > array(
    37. 'add' => 'Yii::app()->controller->createUrl("/manage/taosearch/createProduct")', // Generate connection
    38. ),
    39. 'urlParams' = > array('id','name'), // Set the parameter fields that need to be passed after the url
    40. ),
    41. ),
    42. 'tableHead' => array( // Set the table column header information
    43. 'Category ID' ,
    44. 'Channel',
    45. 'Chinese name',
    46. 'English name',
    47. 'Initial letter',
    48. 'Sort',
    49. 'Classification level',
    50. 'Parent ID',
    51. 'Creation time',
    52. 'Operation ',
    53. ),
    54. )); ?>
    55. *
    56. * Called in drop-down box mode
    57. * widget('ext.tree.widgets.TreeWidget',array(
    58. 'dataProvider' = > $cate, // Pass data
    59. 'pid' => 'pid', // Set parent ID
    60. 'formatParam' => 'name', // Set formatting field
    61. 'treeType' => false , // Output tree format
    62. 'selectClass' => 'class="span11"', // Set the drop-down box style
    63. 'defaultSelectValue' => array( // Set the default value and options of the drop-down box
    64. 0 , ' ≡ As a first-level column≡'
    65. ),
    66. )); ?>
    67. */
    68. class TreeWidget extends Widget {
    69. /**
    70. * CArrayDataProvider data object or array data
    71. * Component data receiving parameters (associative array)
    72. * @var Object || array
    73. */
    74. public $dataProvider;
    75. /**
    76. * Assignment to receive data
    77. * @var type
    78. */
    79. public $arrAll = array();
    80. /**
    81. * Multidimensional relationship with _ID as key name
    82. * @var type
    83. */
    84. public $arrIdRelation = array();
    85. /**
    86. * Simplification of multi-dimensional relationships using _ID as key name, used to output tree diagram
    87. * @var type
    88. */
    89. public $arrIdRelationSimple = array();
    90. /**
    91. * Convert the original data into an array with _ID as the key name
    92. * @var type
    93. */
    94. public $arrIdAll = array();
    95. /**
    96. * All parent-child relationships
    97. * @var type
    98. */
    99. public $arrIdSon = array();
    100. /**
    101. *_ID of leaf node
    102. * @var type
    103. */
    104. public $arrIdLeaf = array();
    105. /**
    106. *_ID of the root node
    107. * @var type
    108. */
    109. public $arrIdRoot = array();
    110. /**
    111. * 每个节点下的子孙后代_ID
    112. * @var type
    113. */
    114. public $arrIdChildren = array();
    115. /**
    116. * Each node goes back to the root
    117. * @var type
    118. */
    119. public $arrIdBackPath = array();
    120. /**
    121. * Output tree structure
    122. * @var type
    123. */
    124. public $strItem = '
      {$strSep}{$name}';
    125. /**
    126. * Set table style
    127. * @var type
    128. */
    129. public $tableClass = 'items table table-striped table-bordered table-condensed';
    130. /**
    131. * Data field parameter array
    132. * @var type
    133. */
    134. public $dataKey = array();
    135. /**
    136. * Specify the fields that need to be formatted
    137. * @var type
    138. */
    139. public $formatParam = 'name';
    140. /**
    141. * Table column name
    142. * @var type
    143. */
    144. public $tableHead = array();
    145. /**
    146. * Father ID
    147. * @var type
    148. */
    149. public $pid = 'pid';
    150. /**
    151. * Specify the type of tree
    152. * true table type tree
    153. * false drop-down box type tree
    154. * @var type
    155. */
    156. public $treeType = true;
    157. /**
    158. * Bind the drop-down box value
    159. * @var type
    160. */
    161. public $optionValue = 'id';
    162. /**
    163. * Format time
    164. * @var type
    165. */
    166. public $formatTime = array();
    167. /**
    168. * Drop-down box style
    169. * @var type
    170. */
    171. public $selectClass = 'class="span3"';
    172. /**
    173. * Set the default value and options of the drop-down box
    174. * @var type
    175. */
    176. public $defaultSelectValue = array(
    177. 0,'≡ 作为一级栏目 ≡',
    178. );
    179. /**
    180. * Set whether the drop-down box has multiple selections
    181. * true for multiple selections
    182. * false for single selection
    183. * @var type
    184. */
    185. public $isMultiple = false;
    186. /**
    187. * Default value bound to the drop-down box
    188. * @var type
    189. */
    190. public $bindSelectValue = 0;
    191. /**
    192. * Operation column
    193. * @var type
    194. */
    195. public $action = array();
    196. /**
    197. * Run
    198. */
    199. public function run() {
    200. if (is_array($this->dataProvider) && count($this->dataProvider) > 0)
    201. $data = $this->_run($this->dataProvider);
    202. else if (is_object($this->dataProvider) && count($this->dataProvider->rawData) > 0)
    203. $data = $this->_run($this->dataProvider->rawData);
    204. $this->render('tree' , array('data'=>$data));
    205. }
    206. /**
    207. * Run
    208. * @param type $datas
    209. * @return type
    210. * @param type $datas
    211. * @return type
    212. */
    213. private function _run($datas){
    214. foreach ($datas as $data) {
    215. if (!empty($this->action) && count($this->action) > 0) {
    216. foreach ($this->action as $key => $action) {
    217. $k = array_keys($action['url']);
    218. $data[$k[0]] = '';
    219. }
    220. }
    221. $this->arrAll[] = $data;
    222. $this->dataKey = array_keys($data);
    223. }
    224. $this->processData();
    225. if ($this->treeType === true)
    226. $data = $this->getTable();
    227. else
    228. $data = $this->getSelect($this->pid, $this->bindSelectValue, $this->isMultiple, $this->selectClass, $this->defaultSelectValue);
    229. return $data;
    230. }
    231. /**
    232. * Get html
    233. * @return type
    234. */
    235. public function getHtml() {
    236. return $this->genHtml();
    237. }
    238. /**
    239. * Set hierarchical fields
    240. * Table type
    241. * @return string
    242. */
    243. public function getItemName(){
    244. $html = '
  • ';
  • foreach($this->dataKey as $v) {
  • if ($this->formatParam == $v)
  • $str = '{$strSep}';
  • else
  • $str = '';
  • $html .= '
  • ';
  • }
  • $html .= '
  • ';🎜 return $html;🎜 }
  • /**
  • * Get table column name
  • * @return string
  • */
  • public function getTableHead(){
  • $html = '
  • ';
  • foreach($this->tableHead as $v)
  • $html .= '
  • ';
  • $html .= '
  • ';
  • return $html;
  • }
  • /**
  • * Get the tree in tabular form
  • * @return string
  • */
  • public function getTable() {
  • $this->strItem = $this->getItemName();
  • $strRe = '
  • 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... Share a powerful classification tree extension of the Yii framework Share a powerful classification tree extension of the Yii framework
    1. Calling method
    2. Calling in form
    3. widget('ext.tree.widgets.TreeWidget',array(
    4. 'dataProvider' => $dataProvider, // Pass data
    5. ' pid' => 'pid', // Set the parent ID
    6. 'tableClass' => 'items table table-striped table-bordered table-condensed', // Table style
    7. 'formatParam' => 'name', // Set the formatting field
    8. 'formatTime' => array( // Set the formatted time parameter
    9. 'created'
    10. ),
    11. 'action' => array(
    12. array(
    13. 'label' => ' Edit', // Link name
    14. 'url' => array(
    15. 'edit' => 'Yii::app()->controller->createUrl("/manage/taosearch/createProduct")', // Generate a connection
    16. ),
    17. 'urlParams' => array('id','name'), // Set the parameter fields that need to be passed after the url
    18. ),
    19. array(
    20. 'label' => 'Add ', // Link name
    21. 'url' => array(
    22. 'add' => 'Yii::app()->controller->createUrl("/manage/taosearch/createProduct")', / / Generate a connection
    23. ),
    24. 'urlParams' => array('id','name'), // Set the parameter fields that need to be passed after the url
    25. ),
    26. ),
    27. 'tableHead' => array( / / Set table column header information
    28. 'Category ID',
    29. 'Channel',
    30. 'Chinese name',
    31. 'English name',
    32. 'Initial letter',
    33. 'Sort',
    34. 'Category level',
    35. 'Parent ID ',
    36. 'Creation time',
    37. 'Operation',
    38. ),
    39. )); ?>
    40. Drop-down box method
    41. widget('ext.tree.widgets.TreeWidget', array(
    42. 'dataProvider' => $cate, // Pass data
    43. 'pid' => 'pid', // Set parent ID
    44. 'formatParam' => 'name', // Set formatting field
    45. 'treeType' => false, // Output tree format
    46. 'selectClass' => 'class="span11"', // Set the drop-down box style
    47. 'defaultSelectValue' => array( // Set the default of the drop-down box Values ​​and options
    48. 0 , '≡ as a first-level column ≡'
    49. ),
    50. )); ?>
    Copy code
    '.$str.'{$'.$v.'}
    '.$v.'
    ';
  • $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 = '';
  • $strRe = '';
  • 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;
  • }
  • }
  • ?>
  • 复制代码


    source:php.cn
    Previous article:PHP calculates current program execution time Next article:I found a ThoughtWorks interview question at work today. Let me share it.
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Latest Articles by Author
    Latest Issues
    Related Topics
    More>
    Popular Recommendations
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template