首页 后端开发 php教程 分享一个功能强大的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;
  • }
  • }
  • ?>
  • 复制代码


    本站声明
    本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

    热AI工具

    Undresser.AI Undress

    Undresser.AI Undress

    人工智能驱动的应用程序,用于创建逼真的裸体照片

    AI Clothes Remover

    AI Clothes Remover

    用于从照片中去除衣服的在线人工智能工具。

    Undress AI Tool

    Undress AI Tool

    免费脱衣服图片

    Clothoff.io

    Clothoff.io

    AI脱衣机

    Video Face Swap

    Video Face Swap

    使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

    热工具

    记事本++7.3.1

    记事本++7.3.1

    好用且免费的代码编辑器

    SublimeText3汉化版

    SublimeText3汉化版

    中文版,非常好用

    禅工作室 13.0.1

    禅工作室 13.0.1

    功能强大的PHP集成开发环境

    Dreamweaver CS6

    Dreamweaver CS6

    视觉化网页开发工具

    SublimeText3 Mac版

    SublimeText3 Mac版

    神级代码编辑软件(SublimeText3)

    在PHP API中说明JSON Web令牌(JWT)及其用例。 在PHP API中说明JSON Web令牌(JWT)及其用例。 Apr 05, 2025 am 12:04 AM

    JWT是一种基于JSON的开放标准,用于在各方之间安全地传输信息,主要用于身份验证和信息交换。1.JWT由Header、Payload和Signature三部分组成。2.JWT的工作原理包括生成JWT、验证JWT和解析Payload三个步骤。3.在PHP中使用JWT进行身份验证时,可以生成和验证JWT,并在高级用法中包含用户角色和权限信息。4.常见错误包括签名验证失败、令牌过期和Payload过大,调试技巧包括使用调试工具和日志记录。5.性能优化和最佳实践包括使用合适的签名算法、合理设置有效期、

    会话如何劫持工作,如何在PHP中减轻它? 会话如何劫持工作,如何在PHP中减轻它? Apr 06, 2025 am 12:02 AM

    会话劫持可以通过以下步骤实现:1.获取会话ID,2.使用会话ID,3.保持会话活跃。在PHP中防范会话劫持的方法包括:1.使用session_regenerate_id()函数重新生成会话ID,2.通过数据库存储会话数据,3.确保所有会话数据通过HTTPS传输。

    描述扎实的原则及其如何应用于PHP的开发。 描述扎实的原则及其如何应用于PHP的开发。 Apr 03, 2025 am 12:04 AM

    SOLID原则在PHP开发中的应用包括:1.单一职责原则(SRP):每个类只负责一个功能。2.开闭原则(OCP):通过扩展而非修改实现变化。3.里氏替换原则(LSP):子类可替换基类而不影响程序正确性。4.接口隔离原则(ISP):使用细粒度接口避免依赖不使用的方法。5.依赖倒置原则(DIP):高低层次模块都依赖于抽象,通过依赖注入实现。

    如何在系统重启后自动设置unixsocket的权限? 如何在系统重启后自动设置unixsocket的权限? Mar 31, 2025 pm 11:54 PM

    如何在系统重启后自动设置unixsocket的权限每次系统重启后,我们都需要执行以下命令来修改unixsocket的权限:sudo...

    在PHPStorm中如何进行CLI模式的调试? 在PHPStorm中如何进行CLI模式的调试? Apr 01, 2025 pm 02:57 PM

    在PHPStorm中如何进行CLI模式的调试?在使用PHPStorm进行开发时,有时我们需要在命令行界面(CLI)模式下调试PHP�...

    解释PHP中的晚期静态绑定(静态::)。 解释PHP中的晚期静态绑定(静态::)。 Apr 03, 2025 am 12:04 AM

    静态绑定(static::)在PHP中实现晚期静态绑定(LSB),允许在静态上下文中引用调用类而非定义类。1)解析过程在运行时进行,2)在继承关系中向上查找调用类,3)可能带来性能开销。

    如何用PHP的cURL库发送包含JSON数据的POST请求? 如何用PHP的cURL库发送包含JSON数据的POST请求? Apr 01, 2025 pm 03:12 PM

    使用PHP的cURL库发送JSON数据在PHP开发中,经常需要与外部API进行交互,其中一种常见的方式是使用cURL库发送POST�...

    See all articles