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

Walk the treeview folder names

2 Answers 172 Views
Hierarchical Data Source
This is a migrated thread and some comments may be shown as answers.
Josh
Top achievements
Rank 1
Josh asked on 17 Jul 2013, 05:41 AM
Hi,

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 Answers, 1 is accepted

Sort by
0
Josh
Top achievements
Rank 1
answered on 19 Jul 2013, 03:18 AM
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.    }
23.}
0
Alex Gyoshev
Telerik team
answered on 19 Jul 2013, 08:41 AM
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.

Regards,
Alex Gyoshev
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Tags
Hierarchical Data Source
Asked by
Josh
Top achievements
Rank 1
Answers by
Josh
Top achievements
Rank 1
Alex Gyoshev
Telerik team
Share this question
or