Heim Backend-Entwicklung PHP-Tutorial 分享一个功能强大的yii框架的分类树扩展

分享一个功能强大的yii框架的分类树扩展

Jul 25, 2016 am 08:48 AM

  • 复制代码
    1. /*
    2. * To change this template, choose Tools | Templates
    3. * and open the template in the editor.
    4. */
    5. /**
    6. * Description of Tree
    7. *
    8. * @author 汪嘉诚
    9. * @email 819434425@qq.com
    10. *
    11. * 传递的数组格式,关联数组就可以
    12. * * array
    13. (
    14. 0 => array
    15. (
    16. 'id' => '7'
    17. 'zone' => '服装'
    18. 'name' => '服装'
    19. 'ename' => 'nanzhuang'
    20. 'first' => 'l'
    21. 'sort_order' => '8'
    22. 'level' => '1'
    23. 'pid' => '6'
    24. 'created' => '0'
    25. )
    26. )
    27. *
    28. * 表格方式调用
    29. widget('ext.tree.widgets.TreeWidget',array(
    30. 'dataProvider' => $dataProvider, // 传递数据
    31. 'pid' => 'pid', // 设置父ID
    32. 'tableClass' => 'items table table-striped table-bordered table-condensed', // 表格样式
    33. 'formatParam' => 'name', // 设置格式化字段
    34. 'formatTime' => array( // 设置格式化的时间参数
    35. 'created'
    36. ),
    37. 'action' => array(
    38. array(
    39. 'label' => '编辑', // 链接名称
    40. 'url' => array(
    41. 'edit' => 'Yii::app()->controller->createUrl("/manage/taosearch/createProduct")', // 生成连接
    42. ),
    43. 'urlParams' => array('id','name'), // 设置url后面需要传递的参数字段
    44. ),
    45. array(
    46. 'label' => '添加', // 链接名称
    47. 'url' => array(
    48. 'add' => 'Yii::app()->controller->createUrl("/manage/taosearch/createProduct")', // 生成连接
    49. ),
    50. 'urlParams' => array('id','name'), // 设置url后面需要传递的参数字段
    51. ),
    52. ),
    53. 'tableHead' => array( // 设置表格列头信息
    54. '分类ID',
    55. '频道',
    56. '中文名',
    57. '英文名',
    58. '首字母',
    59. '排序',
    60. '分类级别',
    61. '父ID',
    62. '创建时间',
    63. '操作',
    64. ),
    65. )); ?>
    66. *
    67. * 下拉框方式调用
    68. * widget('ext.tree.widgets.TreeWidget',array(
    69. 'dataProvider' => $cate, // 传递数据
    70. 'pid' => 'pid', // 设置父ID
    71. 'formatParam' => 'name', // 设置格式化字段
    72. 'treeType' => false, // 输出树格式
    73. 'selectClass' => 'class="span11"', // 设置下拉框样式
    74. 'defaultSelectValue' => array( // 设置下拉框的默认值和选项
    75. 0 , '≡ 作为一级栏目 ≡'
    76. ),
    77. )); ?>
    78. */
    79. class TreeWidget extends Widget {
    80. /**
    81. * CArrayDataProvider 数据对象或数组数据
    82. * 组件数据接收参数(关联数组)
    83. * @var Object || array
    84. */
    85. public $dataProvider;
    86. /**
    87. * 赋值接收数据
    88. * @var type
    89. */
    90. public $arrAll = array();
    91. /**
    92. * 按_ID作键名的多维关系
    93. * @var type
    94. */
    95. public $arrIdRelation = array();
    96. /**
    97. * 按_ID作键名的多维关系的简化,用来输出树状图
    98. * @var type
    99. */
    100. public $arrIdRelationSimple = array();
    101. /**
    102. * 将原始数据转化成的_ID作键名的数组
    103. * @var type
    104. */
    105. public $arrIdAll = array();
    106. /**
    107. * 所有的父子关系
    108. * @var type
    109. */
    110. public $arrIdSon = array();
    111. /**
    112. * 叶子节点的_ID
    113. * @var type
    114. */
    115. public $arrIdLeaf = array();
    116. /**
    117. * 根节点的_ID
    118. * @var type
    119. */
    120. public $arrIdRoot = array();
    121. /**
    122. * 每个节点下的子孙后代_ID
    123. * @var type
    124. */
    125. public $arrIdChildren = array();
    126. /**
    127. * 每个节点回逆到根
    128. * @var type
    129. */
    130. public $arrIdBackPath = array();
    131. /**
    132. * 输出树的结构
    133. * @var type
    134. */
    135. public $strItem = '
      {$strSep}{$name}';
    136. /**
    137. * 设置表格样式
    138. * @var type
    139. */
    140. public $tableClass = 'items table table-striped table-bordered table-condensed';
    141. /**
    142. * 数据字段参数数组
    143. * @var type
    144. */
    145. public $dataKey = array();
    146. /**
    147. * 指定需要格式化的字段
    148. * @var type
    149. */
    150. public $formatParam = 'name';
    151. /**
    152. * 表格列名称
    153. * @var type
    154. */
    155. public $tableHead = array();
    156. /**
    157. * 父ID
    158. * @var type
    159. */
    160. public $pid = 'pid';
    161. /**
    162. * 指定树的类型
    163. * true 表格类型树
    164. * false 下拉框类型树
    165. * @var type
    166. */
    167. public $treeType = true;
    168. /**
    169. * 绑定下拉框value值
    170. * @var type
    171. */
    172. public $optionValue = 'id';
    173. /**
    174. * 格式化时间
    175. * @var type
    176. */
    177. public $formatTime = array();
    178. /**
    179. * 下拉框样式
    180. * @var type
    181. */
    182. public $selectClass = 'class="span3"';
    183. /**
    184. * 设置下拉框的默认值和选项
    185. * @var type
    186. */
    187. public $defaultSelectValue = array(
    188. 0,'≡ 作为一级栏目 ≡',
    189. );
    190. /**
    191. * 设置下拉框是否多选
    192. * true 多选
    193. * false 单选
    194. * @var type
    195. */
    196. public $isMultiple = false;
    197. /**
    198. * 绑定到下拉框的默认值
    199. * @var type
    200. */
    201. public $bindSelectValue = 0;
    202. /**
    203. * 操作列
    204. * @var type
    205. */
    206. public $action = array();
    207. /**
    208. * 运行
    209. */
    210. public function run() {
    211. if (is_array($this->dataProvider) && count($this->dataProvider) > 0)
    212. $data = $this->_run($this->dataProvider);
    213. else if (is_object($this->dataProvider) && count($this->dataProvider->rawData) > 0)
    214. $data = $this->_run($this->dataProvider->rawData);
    215. $this->render('tree' , array('data'=>$data));
    216. }
    217. /**
    218. * 运行
    219. * @param type $datas
    220. * @return type
    221. */
    222. private function _run($datas){
    223. foreach ($datas as $data) {
    224. if (!empty($this->action) && count($this->action) > 0) {
    225. foreach ($this->action as $key => $action) {
    226. $k = array_keys($action['url']);
    227. $data[$k[0]] = '';
    228. }
    229. }
    230. $this->arrAll[] = $data;
    231. $this->dataKey = array_keys($data);
    232. }
    233. $this->processData();
    234. if ($this->treeType === true)
    235. $data = $this->getTable();
    236. else
    237. $data = $this->getSelect($this->pid, $this->bindSelectValue, $this->isMultiple, $this->selectClass, $this->defaultSelectValue);
    238. return $data;
    239. }
    240. /**
    241. * 获得html
    242. * @return type
    243. */
    244. public function getHtml() {
    245. return $this->genHtml();
    246. }
    247. /**
    248. * 设置分层字段
    249. * 表格类型
    250. * @return string
    251. */
    252. public function getItemName(){
    253. $html = '
  • ';
  • foreach($this->dataKey as $v) {
  • if ($this->formatParam == $v)
  • $str = '{$strSep}';
  • else
  • $str = '';
  • $html .= '
  • ';
  • }
  • $html .= '
  • ';
  • return $html;
  • }
  • /**
  • * 获取表格列名称
  • * @return string
  • */
  • public function getTableHead(){
  • $html = '
  • ';
  • foreach($this->tableHead as $v)
  • $html .= '
  • ';
  • $html .= '
  • ';
  • return $html;
  • }
  • /**
  • * 获得表格形式的树
  • * @return string
  • */
  • public function getTable() {
  • $this->strItem = $this->getItemName();
  • $strRe = '
  • 提供两种方式的分类树格式,表格和下拉框形式的树形结构
    可以自定义表格和下拉框的样式,自定义以哪一列的参数为格式化数据,自定义层级关系参数,自定义表格列名称,也可以设置时间的格式化。
    这一切都可以帮你自动搞定,如果大家觉得好,别忘了点个赞呦... 分享一个功能强大的yii框架的分类树扩展 分享一个功能强大的yii框架的分类树扩展
    1. 调用方式
    2. 表格方式调用
    3. widget('ext.tree.widgets.TreeWidget',array(
    4. 'dataProvider' => $dataProvider, // 传递数据
    5. 'pid' => 'pid', // 设置父ID
    6. 'tableClass' => 'items table table-striped table-bordered table-condensed', // 表格样式
    7. 'formatParam' => 'name', // 设置格式化字段
    8. 'formatTime' => array( // 设置格式化的时间参数
    9. 'created'
    10. ),
    11. 'action' => array(
    12. array(
    13. 'label' => '编辑', // 链接名称
    14. 'url' => array(
    15. 'edit' => 'Yii::app()->controller->createUrl("/manage/taosearch/createProduct")', // 生成连接
    16. ),
    17. 'urlParams' => array('id','name'), // 设置url后面需要传递的参数字段
    18. ),
    19. array(
    20. 'label' => '添加', // 链接名称
    21. 'url' => array(
    22. 'add' => 'Yii::app()->controller->createUrl("/manage/taosearch/createProduct")', // 生成连接
    23. ),
    24. 'urlParams' => array('id','name'), // 设置url后面需要传递的参数字段
    25. ),
    26. ),
    27. 'tableHead' => array( // 设置表格列头信息
    28. '分类ID',
    29. '频道',
    30. '中文名',
    31. '英文名',
    32. '首字母',
    33. '排序',
    34. '分类级别',
    35. '父ID',
    36. '创建时间',
    37. '操作',
    38. ),
    39. )); ?>
    40. 下拉框方式
    41. widget('ext.tree.widgets.TreeWidget',array(
    42. 'dataProvider' => $cate, // 传递数据
    43. 'pid' => 'pid', // 设置父ID
    44. 'formatParam' => 'name', // 设置格式化字段
    45. 'treeType' => false, // 输出树格式
    46. 'selectClass' => 'class="span11"', // 设置下拉框样式
    47. 'defaultSelectValue' => array( // 设置下拉框的默认值和选项
    48. 0 , '≡ 作为一级栏目 ≡'
    49. ),
    50. )); ?>
    复制代码
    没有找到数据.
    '.$str.'{$'.$v.'}
    '.$v.'
    ';
  • $strRe .= '
  • '.$this->getTableHead().'';
  • $strRe .= $this->genHtml();
  • $strRe .= '
  • ';
  • 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 = '';
  • $strRe = '';
  • 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 $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]);
  • }
  • }
  • }
  • 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;
  • }
  • }
  • ?>
  • 复制代码


    Erklärung dieser Website
    Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

    Heiße KI -Werkzeuge

    Undresser.AI Undress

    Undresser.AI Undress

    KI-gestützte App zum Erstellen realistischer Aktfotos

    AI Clothes Remover

    AI Clothes Remover

    Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

    Undress AI Tool

    Undress AI Tool

    Ausziehbilder kostenlos

    Clothoff.io

    Clothoff.io

    KI-Kleiderentferner

    Video Face Swap

    Video Face Swap

    Tauschen Sie Gesichter in jedem Video mühelos mit unserem völlig kostenlosen KI-Gesichtstausch-Tool aus!

    Heiße Werkzeuge

    Notepad++7.3.1

    Notepad++7.3.1

    Einfach zu bedienender und kostenloser Code-Editor

    SublimeText3 chinesische Version

    SublimeText3 chinesische Version

    Chinesische Version, sehr einfach zu bedienen

    Senden Sie Studio 13.0.1

    Senden Sie Studio 13.0.1

    Leistungsstarke integrierte PHP-Entwicklungsumgebung

    Dreamweaver CS6

    Dreamweaver CS6

    Visuelle Webentwicklungstools

    SublimeText3 Mac-Version

    SublimeText3 Mac-Version

    Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

    Wie funktioniert die Session -Entführung und wie können Sie es in PHP mildern? Wie funktioniert die Session -Entführung und wie können Sie es in PHP mildern? Apr 06, 2025 am 12:02 AM

    Die Hijacking der Sitzung kann in den folgenden Schritten erreicht werden: 1. Erhalten Sie die Sitzungs -ID, 2. Verwenden Sie die Sitzungs -ID, 3. Halten Sie die Sitzung aktiv. Zu den Methoden zur Verhinderung der Sitzung der Sitzung in PHP gehören: 1. Verwenden Sie die Funktion Session_regenerate_id (), um die Sitzungs -ID zu regenerieren. 2. Store -Sitzungsdaten über die Datenbank, 3. Stellen Sie sicher, dass alle Sitzungsdaten über HTTPS übertragen werden.

    Erklären Sie JSON Web Tokens (JWT) und ihren Anwendungsfall in PHP -APIs. Erklären Sie JSON Web Tokens (JWT) und ihren Anwendungsfall in PHP -APIs. Apr 05, 2025 am 12:04 AM

    JWT ist ein offener Standard, der auf JSON basiert und zur sicheren Übertragung von Informationen zwischen Parteien verwendet wird, hauptsächlich für die Identitätsauthentifizierung und den Informationsaustausch. 1. JWT besteht aus drei Teilen: Header, Nutzlast und Signatur. 2. Das Arbeitsprinzip von JWT enthält drei Schritte: Generierung von JWT, Überprüfung von JWT und Parsingnayload. 3. Bei Verwendung von JWT zur Authentifizierung in PHP kann JWT generiert und überprüft werden, und die Funktionen und Berechtigungsinformationen der Benutzer können in die erweiterte Verwendung aufgenommen werden. 4. Häufige Fehler sind Signaturüberprüfungsfehler, Token -Ablauf und übergroße Nutzlast. Zu Debugging -Fähigkeiten gehört die Verwendung von Debugging -Tools und Protokollierung. 5. Leistungsoptimierung und Best Practices umfassen die Verwendung geeigneter Signaturalgorithmen, das Einstellen von Gültigkeitsperioden angemessen.

    Beschreiben Sie die soliden Prinzipien und wie sie sich für die PHP -Entwicklung anwenden. Beschreiben Sie die soliden Prinzipien und wie sie sich für die PHP -Entwicklung anwenden. Apr 03, 2025 am 12:04 AM

    Die Anwendung des soliden Prinzips in der PHP -Entwicklung umfasst: 1. Prinzip der Einzelverantwortung (SRP): Jede Klasse ist nur für eine Funktion verantwortlich. 2. Open and Close Principle (OCP): Änderungen werden eher durch Erweiterung als durch Modifikation erreicht. 3.. Lischs Substitutionsprinzip (LSP): Unterklassen können Basisklassen ersetzen, ohne die Programmgenauigkeit zu beeinträchtigen. 4. Schnittstellen-Isolationsprinzip (ISP): Verwenden Sie feinkörnige Schnittstellen, um Abhängigkeiten und nicht verwendete Methoden zu vermeiden. 5. Abhängigkeitsinversionsprinzip (DIP): Hoch- und niedrige Module beruhen auf der Abstraktion und werden durch Abhängigkeitsinjektion implementiert.

    Wie setze ich nach dem Neustart des Systems automatisch Berechtigungen von Unixsocket fest? Wie setze ich nach dem Neustart des Systems automatisch Berechtigungen von Unixsocket fest? Mar 31, 2025 pm 11:54 PM

    So setzen Sie die Berechtigungen von Unixsocket automatisch nach dem Neustart des Systems. Jedes Mal, wenn das System neu startet, müssen wir den folgenden Befehl ausführen, um die Berechtigungen von Unixsocket: sudo ...

    Wie debugge ich den CLI -Modus in PhpStorm? Wie debugge ich den CLI -Modus in PhpStorm? Apr 01, 2025 pm 02:57 PM

    Wie debugge ich den CLI -Modus in PhpStorm? Bei der Entwicklung mit PHPSTORM müssen wir manchmal den PHP im CLI -Modus (COMS -Zeilenschnittstellen) debuggen ...

    Erklären Sie die späte statische Bindung in PHP (statisch: :). Erklären Sie die späte statische Bindung in PHP (statisch: :). Apr 03, 2025 am 12:04 AM

    Statische Bindung (statisch: :) implementiert die späte statische Bindung (LSB) in PHP, sodass das Aufrufen von Klassen in statischen Kontexten anstatt Klassen zu definieren. 1) Der Analyseprozess wird zur Laufzeit durchgeführt.

    Wie sende ich eine Postanforderung mit JSON -Daten mithilfe der Curl -Bibliothek von PHP? Wie sende ich eine Postanforderung mit JSON -Daten mithilfe der Curl -Bibliothek von PHP? Apr 01, 2025 pm 03:12 PM

    Senden von JSON -Daten mithilfe der Curl -Bibliothek von PHP in der PHP -Entwicklung müssen häufig mit externen APIs interagieren. Eine der gängigen Möglichkeiten besteht darin, die Curl Library zu verwenden, um Post � ...

    See all articles