我Controller中的代码如下:
public ActionResult LoadAllCat()
{
List<Category> list = null;
list = new List<Category>();
List<Category> lists = categoryService.GetCategory();
foreach (var listcat in lists)
{
Category category = new Category();
category.Id = listcat.Id;
category.ParentsId = listcat.ParentsId;
category.CategoryName = listcat.CategoryName;
list.Add(category);
}
//JavaScriptSerializer jss = new JavaScriptSerializer();
//string ResultDt = jss.Serialize(list);
//return ResultDt;
return Json(list, JsonRequestBehavior.AllowGet);
}
Index.cshtml代码如下:
var setting = {
check: {
enable: true
},
//chkboxType: { "Y": "ps", "N": "ps" },//默认这个
data: {
simpleData: {
enable: true,
idKey: "Id", // id编号命名
pIdKey: "ParentsId", // 父id编号命名
rootPId: 0
}
},
async: {
enable: true,
contentType: "application/json",
dataType: "text",
url: "/CategoryList/LoadAllCat",
type:"GET",
autoParam: ["Id", "CategoryName","ParentsId"]
}
};
function checkNode(e) {
var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
type = e.data.type,
nodes = zTree.getSelectedNodes();
if (type.indexOf("All") < 0 && nodes.length == 0) {
alert("请先选择一个节点");
}
if (type == "checkAllTrue") {
zTree.checkAllNodes(true);
} else if (type == "checkAllFalse") {
zTree.checkAllNodes(false);
}
}
$(document).ready(function () {
$.fn.zTree.init($("#treeDemo"), setting);
$("#checkAllTrue").bind("click", { type: "checkAllTrue" }, checkNode);
$("#checkAllFalse").bind("click", { type: "checkAllFalse" }, checkNode);
});
//-->
</script>