Reputation: 4174
I'm trying to search for an item inside a Kendo Hierarchical Datasource. It is needed to get the uid of that item and make that item node on Kendo Treeview gets programmatically selected.
Here is the code. Forgive me for the sloppy algorithm.
function findTreeviewNodeById(haystack, needle) {
var uid = null;
for (var i = 0; i < haystack.length; i++) {
if (haystack[i].id == needle) {
uid = haystack[i];
}
else if (haystack[i].hasChildren) {
uid = findTreeviewNodeById(haystack[i].children.data(), needle);
}
if (uid != null)
break;
}
return uid;
}
The above code only works for hierarchical datasource with 2 level of depth. If I try to feed it with deeper hierarchical datasource when it reaches the 3rd level, this line haystack[i].children.data() returns empty children (it is supposed to be not empty). Why is the 3rd level of the datasource is empty? Even though the Treeview displayed all the data contained inside the Hierarchical datasource perfectly. Am I missing something here?
Upvotes: 0
Views: 1969
Reputation: 4174
I have to call load() to the haystack before I recursive, so that the haystack's children are loaded.
function findTreeviewNodeById(haystack, needle) {
var uid = null;
for (var i = 0; i < haystack.length; i++) {
haystack[i].load();
if (haystack[i].id == needle) {
uid = haystack[i];
}
else if (haystack[i].hasChildren) {
uid = findTreeviewNodeById(haystack[i].children.data(), needle);
}
if (uid != null)
break;
}
return uid;
}
Upvotes: 2