AJAX CTreeView 实现动态加载无限级树 [ 技术分享 ]
首先建立表coverage
-- 表的结构 coverage
CREATE TABLE IF NOT EXISTS `coverage` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`pid` int(10) unsigned DEFAULT NULL,
`coverageName` varchar(100) DEFAULT NULL,
`coverageDesc` varchar(200) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `pid` (`pid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=20 ;
--
-- 转存表中的数据 coverage
INSERT INTO `coverage` (`id`, `pid`, `coverageName`, `coverageDesc`) VALUES
(16, NULL, '类别', ''),
(17, 16, '类别一', ''),
(18, NULL, '分类', ''),
(19, 17, '类别二', '');
--
-- 限制导出的表
--
-- 限制表 coverage
ALTER TABLE `coverage`
ADD CONSTRAINT `coverage_ibfk_1` FOREIGN KEY (`pid`) REFERENCES `coverage` (`id`) ON DELETE CASCADE;
然后才Controller中添加下面的代码:
public function actionAjaxFillTree()
{
if (!Yii::app()->request->isAjaxRequest) {
exit();
}
$parentId = 'null';
if (isset($_GET['root']) and $_GET['root']!='source') {
$parentId = (int) $_GET['root'];
}
$req = Yii::app()->db->createCommand(
"SELECT m1.id, m1.coverageName AS text, m2.id IS NOT NULL AS hasChildren "
. "FROM coverage AS m1 LEFT JOIN coverage AS m2 ON m1.id=m2.pid "
. "WHERE m1.pid <=> $parentId "
. "GROUP BY m1.id ORDER BY m1.coverageName ASC"
);
$children = $req->queryAll();
echo str_replace(
'"hasChildren":"0"',
'"hasChildren":false',
CTreeView::saveDataAsJson($children)
);
exit();
}
然后在views中添加如下代码:
<?php
$this->widget('CTreeView',array(
'persist'=>'cookie',
'animated'=>'fast',
'url' => array('ajaxFillTree'),
'htmlOptions'=>array(
'id'=>'coverageTree',
'class'=>'coverageTree'
)
));
?>
共 5 条回复
-
zhong21jia 回复于 2011-12-28 21:44 举报
没看出个所以然来!
netdak
注册时间:2011-06-27
最后登录:2019-01-30
在线时长:0小时51分
最后登录:2019-01-30
在线时长:0小时51分
- 粉丝1
- 金钱10
- 威望0
- 积分10