这不起作用:
<script type="text/javascript">
$('#btnCollapseAll').click(function()
{
$('#treeview').collapseAll();
});
</script>
这不起作用:
<script type="text/javascript">
$('#btnCollapseAll').click(function()
{
$('#treeview').collapseAll();
});
</script>
您可以使用此代码
1:崩溃
$("#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:展开
$("#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");
});
只需要设置一个属性“ 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
}
}
}
});
<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>
以上答案都不适合我,因为我的树有很多数据并且设置为loadOnDemand: true
. 我在下面包含了我的解决方案,该解决方案在每个节点加载到树中时递归地扩展它。
我的用例是我想在过滤树时展开所有节点,但在浏览整个树时让它们折叠(因此需要保留设置loadOnDemad: true
)
这里是:
// 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))
}
}
}