Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read selected node's values from kendo tree view?

I have a kendo treeview having a node with {id, value}. and I want to get selected node's id and value when I click on a button.

How can I get it? Is there any inbuilt functions there to get it?

Here is my sample code:

$("mytree").kendoTreeView({
                dataSource: mydata,
                dataTextField: "Name",
                dataValueField: "Id",
            });
like image 960
jestges Avatar asked Aug 30 '13 12:08

jestges


3 Answers

Use the .select() method. Be sure to look at the other methods available as well.

var tv = $('.mytree').data('kendoTreeView'),
    selected = tv.select(),
    item = tv.dataItem(selected);
if (item) {
  alert('Selected item: ' + item.Name + ' : ' + item.Id + ' (uid: ' + item.uid + ')');
} else {
  alert('Nothing selected');
}

Fiddle here

like image 136
ryan Avatar answered Oct 26 '22 18:10

ryan


**

var tv = $("#treeview-right").data("kendoTreeView");
    var selectedNode = tv.select();
    var item = tv.dataItem(e.node);
    item.text will give you the text of the selected node.

**

like image 5
satish Avatar answered Oct 26 '22 18:10

satish


I disagree with the selected answer because depending on what you actually DO, you can be 1 step behind the actually selected value.

If you had some simple delete function then this type of code works fine

var treeview = $("#treeview").data("kendoTreeView");
var selectedNode = treeview.select(),
item = treeview.dataItem(selectedNode);

However, once you start playing with the treeview more you will end up regretting that as I have.

Best practice is to tie to the event handler

e.g.

var treeview = $("#treeview").kendoTreeView({
    expanded: true,
    select: onSelect,
    ....        
}).data("kendoTreeView");

select function

function onSelect(e) {

    var treeview = $("#treeview").data("kendoTreeView");
    var item = treeview.dataItem(e.node);

     if (item) {
         console.log('Selected item: ' + item.whatever + ' | Id = ' + item.Id + ' | Type = ' + item.Type);
         var someVariable = item.whatever;
     } else{
        console.log('nothing selected');
     }
like image 3
Tom Stickel Avatar answered Oct 26 '22 18:10

Tom Stickel