这不起作用:
<script type="text/javascript">
$('#btnCollapseAll').click(function()
{
$('#treeview').collapseAll();
});
</script>
答案 0 :(得分:9)
您可以使用此代码
1:崩溃
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:扩展
$("#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");
});
答案 1 :(得分:3)
只需要设置一个属性&#34; expanded: false
&#34;
$("#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
}
}
}
});
答案 2 :(得分:1)
<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>
答案 3 :(得分:0)
以上答案对我都不起作用,因为我的树有很多数据,并且设置为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))
}
}
}