I’m using a treeview to let the user select an option from a hierarchy… The data loading it’s working fine, but I’m having trouble to the functional part of it.
I have a form where the user will input a lot of data and one of them is the item that he’s supose to select from my TreeView.
So what i would like to know is how to ‘detect’ the item that has been selected.
// create the tree
$this->widget('CTreeView', array(
'id'=>'tree',
'url'=>array('ajaxFillTree'),
));
// bind each node of the tree with something
Yii::app()->getClientScript()->registerScript(
'bindTreeNode',
// select every <span> that is inside a <li>, inside the <ul> tree, i.e. every node of the tree
'$("ul#tree").find("li# span").live("click",function() {
// do whatever you need to do with the node and its parameters
alert($(this).parent("li[id]").attr("id"));
$(this).toggleClass("foo");
return false;
});',
CClientScript::POS_READY
);
This should alert the ID and toggle the ‘foo’ class of the node you click, when you click it.
fleuryc, your code seems to be perfect. I’m only thinking myself, how about a bit different approach?
Give every treeview node specific, the same class (like .special-treeview-node-class) and each node unique ID. Then attach jQuery’s click event to that class and inside its code simply check for unique ID, to get exact information, which tree node has been clicked.