This is a migrated thread and some comments may be shown as answers.

Find node by id

5 Answers 1946 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
OfficeHeart
Top achievements
Rank 1
OfficeHeart asked on 27 Aug 2012, 09:56 AM

Hi,

Is it possible to find a node by id (clientside)?

Here is how we populate the treeview.

public JsonResult _AjaxLoading(string id)
{
IEnumerable nodes = from item in GetDirectorys()
where (id == null ? item.parent == "" : item.parent == id)
select new
{
id = item.directory,
Name = item.name,
hasChildren = item.aantal > 0,
};
return Json(nodes, JsonRequestBehavior.AllowGet);
}

5 Answers, 1 is accepted

Sort by
0
Accepted
Alex Gyoshev
Telerik team
answered on 27 Aug 2012, 01:27 PM
Hello Martin,

You can use the HierarchicalDataSource get method if you use a build newer than 2012.2.725. It will return the dataItem that contains the given id. After that, you can get the node reference through the treeview getByUid, supplying the dataItem uid:

    var dataSource = treeview.dataSource;
    var dataItem = dataSource.get(5); // find item with id = 5
    var node = treeview.findByUid(dataItem.uid);

Regards,
Alex Gyoshev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Horacio
Top achievements
Rank 1
answered on 21 Nov 2012, 01:13 PM
Hello Alex

I'm new with Kendo. I'm trying to do something like you said but it doesn't work for me. I think that I have the build 2012.2.710 but I have not yet paid the license because I'm testing Kendo to develop my app. Should I get the commercial license to get the build you said?

Thanks in advance,

H
0
Chris H.
Top achievements
Rank 1
answered on 04 Dec 2012, 11:18 AM
I've tried to use the 'get' method of HierarchicalDataSource on version v2012.3.1114 without success.

It seems that the child-items haven't been properly loaded with it's properties

The only way I've made this work, is by calling
treeview.expand(".k-item"); (expand the whole tree) BEFORE calling  dataSource.get(); on a child-item


EDIT:
I finally managed to get it working.
The trick was to set 'loadOnDemand' property to 'false':
Seriously: These kinds of things really need to be clearly documentet . It's not the first time I've spent a couple of days trying to figure out why kendoUI isn't behaving as expected, then just to find out it was a matter of configuration
var faqMenu=$("#menutree");
faqMenu.kendoTreeView({
    dataSource: dsFaqCategories,
    loadOnDemand:false
});
0
Andrii
Top achievements
Rank 1
answered on 30 May 2013, 03:23 PM
Hi! Method of Alex Gyoshev helps only when i looking for root items, but nested elements i can't find by dataSource.get(id).
Does anybody knows how to get nested node by id? Thanks!

Edited:
Sorry, it works)) It was my fault.
0
Shruthika
Top achievements
Rank 1
answered on 12 Jun 2013, 03:02 PM
Hi have the same issue. can anyone help me resolve the issue?
var inlineDefault = new kendo.data.HierarchicalDataSource({
                transport: {
                    read: {
                        cache: false,
                        url: "/menu/GetTree",
                        dataType: "json",
                        data: { 'id':  $('#menuId').val(), 'netId': $('#networkId').val() }
                    }
                },
                schema: {
                    model: {
                        id: "id",
                        children: "Items",
                        hasChildren: "hasChildren"
                    }
                }
            });
 
 
            $("#treeview").kendoTreeView({
                template: '# if(item.isOvr) { # <b>#=item.txt#</b># } else { ##=item.txt## }#',
                select: function (e) {
                    //new node selected clear previous messages
                    if (this.dataItem(e.node) != null) {
                        this.expand(e.node);
                        handleTreeSelection(this.dataItem(e.node));
                    }
                },
                dataSource: inlineDefault
            });
and I select a specific node using
function setTreeSelectedNode(nodeId) {
            var dataItem = $("#treeview").data("kendoTreeView").dataSource.get(nodeId);
            var node = $("#treeview").data("kendoTreeView").findByUid(dataItem.uid); // Error : dataitem is null if I pass the child Id
            $("#treeview").data("kendoTreeView").select(node);
            $("#treeview").data("kendoTreeView").expand(node);
             
        }
Tags
TreeView
Asked by
OfficeHeart
Top achievements
Rank 1
Answers by
Alex Gyoshev
Telerik team
Horacio
Top achievements
Rank 1
Chris H.
Top achievements
Rank 1
Andrii
Top achievements
Rank 1
Shruthika
Top achievements
Rank 1
Share this question
or