Problems implementing CTreeView

Hello everyone. Still fairly new to Yii and loving nearly everything about it, but I am trying to get a grasp on all the things it can do. So I started a demo application where I am plugging in a lot the non-standard (for me) elements such as CTreeView.

I have found and read a number of examples and documentation pages, but I am still having trouble. My big issue (which seems simple) is setting a branch to start collapsed. I don’t know what restriction there are to do this but I can’t even seem to get the simplest example working. In fact, I download an app that demoed these and got the same problem. The error I am getting is as follows.




PHP Error


Undefined index: css


C:\...\yiiframework\framework\web\widgets\CTreeView.php(211)


199                 if(isset($node['hasChildren']) && $node['hasChildren'])

200                 {

201                     if($css!=='')

202                         $css.=' ';

203                     $css.='hasChildren';

204                 }

205 

206                 $options=isset($node['htmlOptions']) ? $node['htmlOptions'] : array();

207                 if($css!=='')

208                 {

209                     if(isset($options['css']))

210                         $options['css'].=' ';

211                     $options['css'].=$css; // HERE IS WHERE THE ERROR IS AT

212                 }

213 

214                 if(isset($node['id']))

215                     $options['id']=$node['id'];

216 

217                 $html.=CHtml::tag('li',$options,$node['text'],false);

218                 if(!empty($node['children']))

219                 {

220                     $html.="\n<ul>\n";

221                     $html.=self::saveDataAsHtml($node['children']);

222                     $html.="</ul>\n";

223                 }



but I have determined that it happens ONLY when I try setting the ‘expanded’ setting on a branch. Here is my data tree and CTreeView widget run.




$dataArray = array(

    array('text'=>'First','children'=>array(

        array('text'=>'Some Point', ),

        array('text'=>'Another Point', 'children'=>array(

            array('text'=>'Sub points are good',  'children'=>array(

                 array('text'=>'Like this'),

                 array('text'=>'Or even like this'),

            )),

            array('text'=>'Sub points are bad'),

            array('text'=>'Sub points are sub points'),

        )),

        array('text'=>'More stuff'),

        array('text'=>'Also this'),

    )),

    array('text'=>'Second'),

    array('text'=>'Third', 'children'=>array(

        array('text'=>'It goes on', ),

        array('text'=>'and on...', 'children'=>array(

            array('text'=>'and on...', 'children'=>array(

                array('text'=>'and on...', 'expanded'=>false, 'children'=>array( // HERE - 'expanded'=>false - IS WHAT CAUSES THE ERROR

                    array('text'=>'and then stops'),

                )),

            )),

        )),

    )),

);


$this->widget('CTreeView', array(

    'data'=>$dataArray,

    'htmlOptions'=>array('class'=>'treeview-red'),

));



as you can see, I am not trying to do much of anything fancy here, I just need to be able to set these from starting in the open position.

What am I doing wrong?

There is a bug in the CTreeView in Yii 1.1.8… get the latest version from the google code or just copy/paste the trunk code in your CTreeView file

http://code.google.com/p/yii/source/browse/trunk/framework/web/widgets/CTreeView.php

You just saved the day! Thank you very much. That fixed the initial issue. Was this posted somewhere I should have check first?

Also, not sure if you would consider this a bug but just so you know, if you set a child to appear expanded and the parent to appear collapsed (so that opening a parent may show all the branches under it) the child’s (and its children’s) toggle status gets messed up. So you see a + AND its expanded; click the plus, it becomes a minus and the content is collapsed. Seems like there is no solid logic connecting the image with the status because as I said, initially they show and were set to… its just the image was a +. Regardless… I can make this work for now. Thank you again. :D

It’s always good to check the current changelog to see if something is already fixed in the SVN trunk - http://code.google.c…trunk/CHANGELOG

For the child problem… CTreeView is is just a wrapper for the jQuery plugin TreeView - http://bassistance.d…lugin-treeview/

If the problem is in the Yii code this should be fixed… but if the problem is in the jQuery plugin code it’s more complicated to fix it…

If you find why this happens let us know so we can fix it…

I certainly will. Thank you for your help. :D