I am using the treeview of KendoUI and want to give the user the possibility to filter it. There is even a demo that does what I want (http://demos.kendoui.com/web/treeview/api.html)
The Problem is that the filter is only applied to the 1st hierarchy of the TreeView, so if the filter-text is present in a child but not the parent, then the child won't be displayed.
Example:
- Item 1
- Item 2
- Item xzy
- Item abc
If the search text would be "abc", no item would be displayed. Instead I would like to have the following result:
- Item 2
- Item abc
Does anyone know how to do this? This is the code I am using:
var tree_view_data = new kendo.data.HierarchicalDataSource({
transport: {
read: {
url: "getall/items",
dataType: "json"
}
},
schema: {
model: {
children: "ChildItems"
}
}
});
//init tree view itself
var $treeview = $("#div-treeview").kendoTreeView({
dataSource: tree_view_data,
dataTextField: [ "Text", "ChildrenText" ]
});
//allow filter of navigation tree
var refreshTree = function () {
tree_view_data.filter({
field: "Text", //if I would use "ChildrenText" here nothing be displayed at all if filtertext is set
operator: "contains",
value: $("#tree-text-search").val()
});
};
$("#tree-text-search").change(refreshTree).keyup(refreshTree);
If I read the question well, it is about filtering the data in the view and not the treeview itself. It could be done by recursion.
Recursion example that works:
This version searches the whole tree, is case insensitive, and hides nodes that do not contain the search query (jQuery 1.8+).
I found a way to make this happen just using jQuery selectors to hide and show the necessary child nodes.
First thing is when you are creating your tree view, add this parameter to your options:
loadOnDemand: false
This way the tree will render out all the HTML of your child nodes before being requested, thereby allowing you to use jQuery to navigate through.
Here is the jQuery code I have working that filters the nodes out that don't match, opens the group of nodes that do match, and shows them.
First of all. KendoTreeView is very low-level control compared to Teleriks RadDropDownTree from ASP.NET http://www.telerik.com/help/aspnet-ajax/dropdowntree-overview.html (i mean the js of course!) The should have taken this to jquery/kendo... it needed to improve this filter, so if you prefer proper filtering on dataitem instead of "findByText", this does:
.1) finds all dataitems .2) checks your conditions (here lowercase contains on value/text) .3) flag item, flag parents .4) clean up, remove nodes left in tree by parent
Update 2016-01-13: There is now a help topic that shows how to perform TreeView filtering based on a user string.
You need to manually filter the child DataSources, so that only the necessary nodes are shown. Having different
dataTextField
s for the different levels makes it harder to grasp, so this code uses thetext
field only. Also, as this filtering is performed on the client-side, it assumes that you have loaded all nodes.For more than 4 levels traverse all parents of type UL and LI and call show().