I am trying to implement a search feature on a TreeView. For my search, I am receiving a list of paths to the correct nodes on the tree.
The challenge comes in that I need to use lazy loading for my remote data source. I need to know when the data is loaded and the node has finished expanding or I need the ability to trigger the expansion of a node once the data for that node has loaded.
I've tried doing this with the fetch function but that doesn't seem to work properly. Since the search returns multiple results, I have a loop call my search function iteratively. What appears to happen is that the loop progresses without finishing a call until the last element because the data has not loaded from the remote data source until that time. I believe it may have something to do with calling both fetch and then expand afterward.
Does the expand function implement the Promise API? Is there another way to make this work?
My code is the following:
//the following function is initially called with:
// - the full path (including the element)
// - the first level dataItem
// - the number 1
function findNode(path, dataItem, currentLevel){
var
treeview = $(
"#treeview"
).data(
"kendoTreeView"
);
var
ds = treeview.dataSource;
var
node = treeview.findByUid(dataItem.uid);
if
(currentLevel < path.length) {
dataItem.children.fetch().always(
function
(){
treeview.expand(node);
$.each(dataItem.children.data(),
function
(index, value){
if
(value.id == path[currentLevel])
var
di = ds.getByUid(value.uid);
findNode(path, di, currentLevel + 1);
}
});
});
}
else
{
//do things with the found item
}
}