CTreeView how to retrieve the element clicked

I have a CTreeView, I want to do a different action according to the element has been clicked, but how can I intercept this event?

I see that CtreeView is a list with <UL> <LI> <LI>… So I think I should insert the event "onclick" foreach <LI>

but how can I do it in CTreeView?

In DropDownList I can specify in htmloptions differents options for example:




dropDownList ($name, $select, $data, $htmlOptions=array ( 

'options'=>'value1'=>array('onclick'=>'xxxx'),

           'value2'=>array('onclick'=>'yyyy')))



For CtreeView I tried this solution but it seems not possible.

Another solution should be insert the "onclick" in the UL tag and then according to the index clicked, with Javascript, do the right action, for example:




$this->widget('CTreeView',array('data'=>$data,'animated'=>'fast','collapsed'=>true,

'htmlOptions'=>array('onclick'=>'if (indexclicked==xxxx)...')));



the problem is how can I understand wich index is clicked with javascript? For dropdownlist I can use




if (this.options[this.selectedIndex])=='xxxx' ...



there is a similar syntax for UL list?

I solved this problem with a trick,

the perfect should be that the array $data that is used to generate the tree, besides the keys ‘text’, ‘expanded’, ‘id’ etc…, had also a new key for example ‘options’ that should be properly used in CTreeView::saveDataAsHtml() when it creates the <li> tag, now it is:


$html.="<li{$id}{$css}>{$node['text']}";

by adding these options, it should be for example:


$html.="<li{$id}{$css}{$options}>{$node['text']}";

One solution is to override CTreeView::saveDataAsHtml() and to modify as explained above, instead I use the following trick:

when I define $data[‘id’] I add also the onclick instructions, so before I had:


$data['id'] = 'options1';

now I have for example:


$data['id'] = 'options1" onclick="alert (\'this is options1\');';

There are " missing before options1 and at the end of the onclick statement because they are added in saveDataAsHtml() (trick not very beautyful to see, but effective)

Hi there,

Sorry for reopening over one year old post, but I think it is worth doing so.

First of all, thank you for your elegant (yet tricky ;]) and quick solution. I added it to my own collection here. I also opened bug-tracker issue with feature request to incorporate your idea in core framework code. Hope to see it there soon! ;]

Thanks and Cheers,

Trejder

EDIT: I came out with another idea for solving this problem.

Instead of only passing text:


$item['text'] ='Something';

pass HTML code with HTML tag a or div surrounding text. Inside newly added tag you can do whatever, you want - add onClick handler, declare on class or style, etc.:


$nodeText = CHtml::openTag('a', $options);

$nodeText.= $item['text'];

$nodeText.= CHtml::closeTag('a')."\n";

$item['text'] = $nodeText;

where $options variable is a standard Yii-htmlOptions, where you can pass, what you want.

Just wanted to notice you and other readers of this topis, that described problem has just been solved and incorporated into next comming release of Yii - check this link for more information (Google Code, therefore you need Google Account to log in).