Walk the treeview folder names

3 posts, 0 answers
  1. Josh
    Josh avatar
    72 posts
    Member since:
    Jun 2013

    Posted 17 Jul 2013 Link to this post


    I have an array of folder names and I'm trying to figure out how to walk the treeview to select the nested folder. How can I do that? I do not have ID set in the model of the Hierarchical Datasource - I'm just trying to do it by the text of the nodes. The tricky part is that some nodes can have the same name, like "ppt > _rels > slideMaster > _rels"
  2. Josh
    Josh avatar
    72 posts
    Member since:
    Jun 2013

    Posted 18 Jul 2013 Link to this post

    Well, I managed to get something working - but, it's ugly and feels like a hack. Not to mention, if the structure of TreeView rendered by Kendo changes, this will probably break. Is there a better way to do this?

    01.// node is the starting tree node - e.g. tree.root[0]
    02.// folders is an array of folder names - e.g. ["documents", "pictures", "kittens"]
    03.function treeSelectFolder(node, folders) {
    04.    if (folders.length > 0) {
    05.        var txt = folders.shift();
    06.        console.log("tree node ", node);
    07.        var $node = $(node);
    08.        $node.children("li.k-item").find("span.k-in").each(function (){
    09.            var $this = $(this);
    10.            if ($this.text().toLowerCase() == txt) {
    11.                var $el = $this.closest("li.k-item");
    12.                efTree.expand($el);
    13.                if (folders.length == 0) {
    14.                    efTree.select($el);
    15.                    efTree.trigger( 'select', {node: $el} );
    16.                }
    17.                else {
    18.                    treeSelectFolder($($el.get()[0]).find("ul.k-group").get()[0], folders);
    19.                }
    20.            }
    21.        });
    22.    }
  3. Alex Gyoshev
    Alex Gyoshev avatar
    2527 posts

    Posted 19 Jul 2013 Link to this post

    Hello Josh,

    You can use the text() method to be sure that you are getting the correct node text. Other than that, the approach is correct. Alternatively, you might want to use an iterative algorithm over the datasource itself, like shown in this jsbin.

    Alex Gyoshev
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Back to Top