search
HomeBackend DevelopmentPHP TutorialShare a powerful classification tree extension of the Yii framework

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 $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;
  • }
  • }
  • ?>
  • 复制代码


    Statement
    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
    The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

    What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

    PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

    PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

    PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

    PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

    PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

    PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

    Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

    PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

    PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

    PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

    PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

    PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

    How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

    PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    SublimeText3 English version

    SublimeText3 English version

    Recommended: Win version, supports code prompts!

    mPDF

    mPDF

    mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    MinGW - Minimalist GNU for Windows

    MinGW - Minimalist GNU for Windows

    This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

    Atom editor mac version download

    Atom editor mac version download

    The most popular open source editor