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",
});
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
**
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.
**
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');
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With