CTreeView

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.

I’m using something like this in the data array

‘htmlOptions’=>array( ‘onclick’=>" alert(this.id) "

then when i click on my treeview it returns the clicked item, but if it is a child item i returns all the ascendents of him…

So how can i get just the node that was really clicked and put the id of him in a variable to retrieve it later

thanks

Hi,

Here’s what I put in my view :




        // 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.

Cheers!

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.

So, something like this:


$('.special-treeview-node-class').click(function({alert($(this).attr('id')<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/wink.gif' class='bbc_emoticon' alt=';)' />}));

(written from memory, not tested, but should work)

What do you think about this?