Expand and Select Node with ID in TreeView

2 posts, 1 answers
  1. Answer
    Dietmar
    Dietmar avatar
    13 posts
    Member since:
    May 2012

    Posted 12 Jul 2013 Link to this post

    Hi,

    I'm almost done with the code from the subject. The tree expands already to the given ID, but unfortunately it does not select the node given by ID

    Edit: Just to clarify, because I read a few threads regarding this issue - I don't get any error messages nor do I get 'undefined' for any variables. The node just doesn't get selected.

    function expandAndSelectNode(id, treeViewName) {
        // get the Kendo TreeView widget by it's ID given by treeviewName
        var treeView = $(treeViewName).data('kendoTreeView');
         
        // find node with data-id = id
        var item = $(treeViewName).find("li[data-id='" + id + "']").find(".k-in");
         
        // expand all parent nodes
        $(item).parentsUntil('.k-treeview').filter('.k-item').each(
                    function (index, element) {
                        $(treeViewName).data('kendoTreeView').expand($(this));
                    }
                );
         
        // get DataSourceItem by given id
        var nodeDataItem = treeView.dataSource.get(id);
     
        //get node within treeview widget by uid
        var node = treeView.findByUid(nodeDataItem.uid);

        $(treeView).select(node);
        //treeView.select(".k-item:last");
    }

    I also tried $(treeView).select(item), since it should already point to the <span> item I want to select anyway. This didn't work.
    Then I tried tried $(treeView).select(item.closest('.k-item')), since it points to the parent <li> element, but this didn't work either.
     The uncommented line (treeView.select(".k-item:last")) was just for testing and works.

    I'm a little bit stuck and glad about any hints.

  2. Daniel
    Admin
    Daniel avatar
    2118 posts

    Posted 16 Jul 2013 Link to this post

    Hi,

    The treeview object should not be wrapped in order to use the treeview methods and not the jQuery methods e.g.

    treeView.select(node);
    Regards,
    Daniel
    Telerik
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
  3. UI for ASP.NET MVC is VS 2017 Ready
Back to Top