问题
This is not working:
<script type="text/javascript">
$('#btnCollapseAll').click(function()
{
$('#treeview').collapseAll();
});
</script>
回答1:
You can use this code
1: collapse
collapse kendoTreeView document
$("#treeview").kendoTreeView();
var treeview = $("#treeview").data("kendoTreeView");
treeview.collapse(document.getElementById("firstItem"));
$('#btn').click(function () {
// collapse the node with id="firstItem"
// collapse all nodes
treeview.collapse(".k-item");
});
2:expand
expand kendoTreeView document
$("#treeview").kendoTreeView();
var treeview = $("#treeview").data("kendoTreeView");
treeview.collapse(document.getElementById("firstItem"));
$('#btn').click(function () {
// expand the node with id="firstItem"
// expand all nodes
treeview.expand(".k-item");
});
回答2:
only need to set one property "expanded: false
"
$("#TreeList").kendoTreeList({
height: 400,
filterable: false,
sortable: true,
columns: [
{ field: "Name", title: " ", width: 235 },
{ field: "Outstanding", title: "Outstanding", width: 235, template: $("#outstanding-olor-div-template").html() }
],
dataSource: {
transport: {
read: {
url: ServiceUrl ,
dataType: "json"
}
},
schema: {
model: {
id: "Id",
parentId: "ParentId",
fields: {
ParentId: { field: "ParentId", nullable: true },
Id: { field: "Id", type: "number" },
Name: { field: "Name" }
},
expanded: false
}
}
}
});
回答3:
<script>
$(document).ready(function() {
$("#treeview").kendoTreeView();
var treeview = $("#treeview").data("kendoTreeView");
$("#expandAllNodes").click(function() {
treeview.expand(".k-item");
});
$("#collapseAllNodes").click(function() {
treeview.collapse(".k-item");
});
});
</script>
回答4:
None of the above answers worked for me because my tree had a lot of data and was set to loadOnDemand: true
. I included my solution below that recursively expands each node as it is loaded in the tree.
The use case for me was that I wanted to expand all nodes when filtering the tree, but leave them collapsed when browsing the full tree (thus the settings needed to stay loadOnDemad: true
)
Here it is:
// Expands entire tree
function expandTree() {
console.log("Expanding All...");
var tree = $("#treeview").data("kendoTreeView");
var nodes = document.getElementsByClassName('k-item');
for (var i = 0; i < nodes.length; i++) {
expandNode(nodes[i]);
}
}
// Expand nodes recursively
function expandNode(htmlElement) {
var tree = $("#treeview").data("kendoTreeView");
var dataItem = tree.dataItem(htmlElement);
dataItem.set("expanded", true);
var children = dataItem.items;
if (children) {
for (var i = 0; i < dataItem.items.length; i++) {
expandNode(tree.findByUid(dataItem.items[i].uid))
}
}
}
来源:https://stackoverflow.com/questions/17956894/how-do-i-collpase-and-expand-all-tree-nodes-in-a-kendo-ui-treeview-based-on-a-bu