Posted on 2018-04-18 11:08
viery 阅读(97)
评论(0) 编辑 收藏 所属分类:
.net
1.在视图引入相关css js,页面载入时提交getJSON异步请求到后台控制器
js
<link href="~/Content/zTree_v3/css/demo.css" rel="stylesheet" />
<link rel="stylesheet" href="~/Content/zTree_v3/css/zTreeStyle/zTreeStyle.css" type="text/css">
<script type="text/javascript" src="~/Content/zTree_v3/js/jquery.ztree.core.js"></script>
<script type="text/javascript">
<!--
var setting = {
data: {
simpleData: {
enable: true
}
},
callback: {
onClick: zTreeOnClick
}
};
function zTreeOnClick(event, treeId, treeNode) {
alert(treeNode.tId + ", " + treeNode.name);
};
</script>
<script type="text/javascript">
$(function () {
$("#layout1").ligerLayout({ leftWidth: 220, allowBottomResize: false, allowLeftResize: false });
$.getJSON("/Home/GetMenu", function (data) {
//alert(data);
$.fn.zTree.init($("#treeDemo"), setting, data);
});
});
</script 2.控制器后台,数据库有已建好的id pid菜单表
1 public ActionResult GetMenu()
2 {
3 string json = null;
4
5 List<Menu> menuList = bll.GetEntites(u => true).ToList();
6 List<MenuJson> jsonList = new List<MenuJson>();
7 for (int i = 0; i < menuList.Count; i++)
8 {
9 MenuJson modelJson = new MenuJson();
10 modelJson.id = menuList[i].id;
11 modelJson.pId = menuList[i].pid;
12 modelJson.name = menuList[i].name;
13 if (menuList[i].open!=null)
14 {
15 modelJson.open = menuList[i].open;
16 }
17 else
18 {
19 modelJson.open = false;
20 }
21 if (menuList[i].isParent!=null)
22 {
23 modelJson.isParent = menuList[i].isParent;
24 }
25 else
26 {
27 modelJson.isParent = false;
28 }
29 jsonList.Add(modelJson);
30 }
31 JavaScriptSerializer ssjs = new JavaScriptSerializer();
32 string data=ssjs.Serialize(jsonList);
33 return Content(data);
34 }
35
36
37 }
38
39 class MenuJson
40 {
41 public int id { get; set; }
42 public int pId { get; set; }
43 public string name { get; set; }
44 public Nullable<bool> open { get; set; }
45 public Nullable<bool> isParent { get; set; }
46