I know the exact route to find a nested node in a Kendo tree view. So I wish to expand all nodes along this route to go and get there.
I know how to find a node and make Kendo expand that node.
My Treeview uses a web-api for its data and all works well. The schema is defined as follows:
schema: {
model: {
id: "CodeList",
hasChildren: "HasKids"
}
}
What I want to achieve is for the tree to expand automatically to a nested level.
Say I know the path to get to a node is ‘Level1CodeA|Level2CodeA|Level3CodeA’ so first I wish to find the code ‘Level1CodeA’ which I can do.
I then wish to expand this node (internally it retrieves the data under this node and it expands ok) after which I want to find ‘Level2CodeA’, repeat the process so also find and then select Level3CodeA.
How would I go about this? I was looking for an ‘AfterExpanded’ event that I could use to start the next search and expand operation but I can’t find any event I can use. I tried the ‘change’ event on my datasource but this is fired many times and I can’t seem to narrow it down to the correct item..
Many thanks.
EDIT: more code
<script id="treeII-template" type="text/kendo-ui-template">
<img id="explorerItemImg" src="#: item.Image #" />
<span id="explorerItemCode">#: item.Code #</span> -
<span id="explorerItemFullName">#: item.FullName #</span>
# if (item.Level < (item.Levels - 1)) { #
[<span id="explorerItemLCount">#: item.LCount #</span>]
# } #
# if (item.HasKids) { #
[<span id="explorerItemPCount">#: item.PCount #</span>]
# } #
</script>
The code to create the HierarchicalDataSource and assigning this to the treeview:
// -----------------
// set the datasource for the bottom explorer...
// -----------------
var loadDataForExplorerGroup = function (context, groupid, groupsequence) {
// hang on to this context/groupid
sessionStorage.setItem(context + "_explorer_groupId", groupid);
sessionStorage.setItem(context + "_explorer_groupSequence", groupsequence);
// define the datasource and attach it to the explorer-tree
explorerGroupData = new kendo.data.HierarchicalDataSource({
transport: {
read: {
url: "/api/explorerapi/GetExplorerData?",
data: {
context: "portfolio",
groupid: groupid,
groupsequence: groupsequence,
userid: sessionStorage.getItem("symUserID")
}
}
},
schema: {
model: {
id: "CodeList",
hasChildren: "HasKids"
}
}
});
// simply assign the data source again to the tree
$("#idExplBottomTree").data("kendoTreeView").setDataSource(explorerGroupData);
};
That
AfterExpandedevent might bedataBound. What I would propose is given the followingTreeViewdefinition:I’ve added
dataBoundhandler that invokestreeNavigatedefined as:Where
searchis an array containingdataTextFieldof each node that we want to navigate.Example:
Defines that we want to
expanda node which text ist_3, thent_32, nextt_321and finallyt_3214.So
treeNavigateNextchecks that there is still something pending (search.length > 0) and if so, find the element by itstext(var first = tree.findByText(search[0]);), removes the element fromsearch(search.shift();) and finallyexpandthe node (tree.expand(first)). This causes the next level to be loaded and when receiveddataBoundevent is triggered and I will go to the next level.See it running here
EDIT: If instead of searching by text you prefer to navigate by
idthen add tosearcharray theidof the different nodes and use the followingtreeNavigateNextfunction instead:New running example here