Difference between #12 and #5 of adding class and links to Ctreeview node

unchanged
Title
adding class and links to Ctreeview node
unchanged
Category
Tips
unchanged
Tags
ctreeview, link, class
changed
Content
This article will show you how to add links and class to each node in ctreeview.
so you can attach events in js by obtaining handler on anchor or class.

I am new to Yii, butand I love this framewrok. lately I
have been playing a bit with ctreeview. and I find out there is a lack of
documentation about adding links to ctree node, so here, I would like to share
my experience with you, because I think this is a quite common practice if you
are using ctreeview. this article is based on this
[article](http://www.yiiframework.com/wiki/70/display-an-ajax-tree-from-your-db-using-ctreeview
"")
中文版请查看我的[博客](http://yjlblog.com "")

So I assume that you have something like this in your controller:

~~~
[php]
public function actionAjaxFillTree()
    {
        if (!Yii::app()->request->isAjaxRequest) {
            exit();
        }
        $parentId = "NULL";
        if (isset($_GET['root']) && $_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 tree AS m1 LEFT JOIN tree AS m2 ON m1.id=m2.parent_id
"
            . "WHERE m1.parent_id <=> $parentId "
            . "GROUP BY m1.id ORDER BY m1.name ASC"
        );
        $children = $req->queryAll();
        echo str_replace(
            '"hasChildren":"0"',
            '"hasChildren":false',
            CTreeView::saveDataAsJson($children)
        );
        exit();
    } 
~~~

after the query($children = $req->queryAll()) in the above code,
add the following loop to insert a link to each node, you can also add a class
or id along with it.

~~~
[php]
$treedata=array();
foreach($children as $child){
    
$options=array('href'=>'#','id'=>$child['id'],'class'=>'treenode');
     $nodeText = CHtml::openTag('a', $options);
	  $nodeText.= $child['text'];
	  $nodeText.= CHtml::closeTag('a')."\n";
	  $child['text'] = $nodeText;
	  $treedata[]=$child;
}
~~~

And replace 
~~~
[php]
CTreeView::saveDataAsJson($children)
~~~

with
~~~
[php]
CTreeView::saveDataAsJson($treedata)
~~~

So try it out now.