Yii CTreeView用法举例

分常规方法,ajax动态加载
首先建表(yii wiki)

[sql]
--
-- 表的结构 `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;
[/sql]


方法一,直接写死在view文件里

[php]
$this->widget('system.web.widgets.CTreeView', array(
    'animated' => 'normal',
    'data' => array(
        array(
            'text' => '<span>AAAA</span>',
            'children' => array(array(
                    'text' => '<a href="' . $this->createUrl('/site/index') . '">BBBB</a>',
                    'text' => '<a href="' . $this->createUrl('/site/page') . '">BBB</a>',
                    ), ),
            ),
        array(
            'expanded' => false,
            'text' => '<span>CCCC</span>',
            'children' => array(
                array('text' => 'CCC'),
                array('text' => 'CCC', ),
                ),
            ),
        ),
    ));
[/php]


优点:简单明了,缺点:新增或修改需要修改文件

方法2:ajax动态加载
在view中写入

[php]
$this->widget('CTreeView', array(
    'persist' => 'cookie',
    'animated' => 'fast',
    'url' => array('ajaxFillTree'),
    'htmlOptions' => array('id' => 'coverageTree', 'class' => 'coverageTree')));
[/php]

在Controller.php中写入

[php]
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.name 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.name ASC"
);
//<=>叫做安全等于,至于什么情况下使用<=>有知道的告诉哈
$children=$req->queryAll();
echo str_replace('"hasChildren":"0"', '"hasChildren":false', CTreeView::saveDataAsJson($children)
        );
//AAA:如果要在节点处增加链接,在$children=$req->queryAll()后面增加下面的
 $treedata=array();
foreach ($children as $child)
{
$options=array('href'=>'#','id'=>$child['id'],'class'=>'treenode');
               /*BBB:如果是只在叶子节点上增加链接
$child['text'] = ($child['hasChildren'] == true ? $child['text'] : CHtml::openTag('a', $options).$child['text'].CHtml::closeTag('a')."\n");
             *--BBB
              */
             /*CCC:如果父节点也要链接
            $nodeText = CHtml::openTag('a', $options);
                   $nodeText .= $child['text'];
                   $nodeText .= CHtml::closeTag('a') . "\n";
                    $child['text'] = $nodeText;
           *--CCC
            */
$treedata[]=$child;
}

//AAA:修改echo部分
echo str_replace(
'"hasChildren":"0"','"hasChildren":false',CTreeView::saveDataAsJson($treedata)
);

exit();
}
[/php]

至于要链接的动作可新建表记录相应节点对应的动作;
扩展:可加入权限控制,只显示用户用户有权限的节点。

你可能感兴趣的:(yii)