zTree简单实例与异步加载实例
Posted 青小稞
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了zTree简单实例与异步加载实例相关的知识,希望对你有一定的参考价值。
【zTree】简单实例与异步加载实例
我们在项目中经常会需要用到树,这次按照数据库存储的特点重新了做一个小demo,使用zTree来实现这个功能。
简单实例:
首先我们需要在界面中引入代码,很简单,但是很重要,它用来存放加载的树。
ps:添加ztree的引用,下载地址:http://www.ztree.me/v3/main.php#_zTreeInfo
- <span style="font-family:KaiTi_GB2312;font-size:18px;"><ul id="tree" class="ztree"></ul> </span>
其次是js代码:
- <span style="font-family:KaiTi_GB2312;font-size:18px;"> var tree = {
- /**
- * 所有的初始化的操作
- */
- pFunction: {
- zTree: ‘‘,
- setting:
- {
- view: {
- dblClickExpand: false,
- showLine: true, //是否显示节点间的连线
- selectedMulti: false,
- expandSpeed: "fast"
- },
- callback: {
- onDblClick: zTreeOnDblClick
- },
- check: {
- enable: true,
- chkStyle: "radio",
- radioType: "all"
- },
- data:
- {
- key:
- {
- name: "Name" //界面显示的名称参数
- },
- simpleData:
- {
- enable: true,
- idKey: "ID",
- pIdKey: "PID",
- rootPId: "null"
- }
- }
- },
- //加载树的资源
- loadTree: function () {
- $.post("/HomeIndex/Ztree", null, function (resourceInfo) {
- //核心代码,将查询到的信息存放到setting格式的tree中
- $.fn.zTree.init($("#tree"), tree.pFunction.setting, resourceInfo);
- });
- },
- }
- };</span>
因为项目需要,我造了一些与真实数据库类似的数据,这些数据大家可自行忽略,主要代码是将数据赋予id和Pid,返回list.,树主要就是通过父节点和子节点的id来进行匹配的。
- <span style="font-family:KaiTi_GB2312;font-size:18px;"> public JsonResult Ztree()
- {
- List<Tree> tree = new List<Tree>();
- Tree tree1 = new Tree();
- Tree tree2 = new Tree();
- Tree tree3 = new Tree();
- Tree tree4 = new Tree();
- Tree tree5 = new Tree();
- Tree tree6 = new Tree();
- Tree tree7 = new Tree();
- tree1.ID = "01";
- tree1.Name = "一";
- tree1.ID1 = "0101";
- tree1.Name1 = "一一";
- tree1.ID2 = "010104";
- tree1.Name2 = "一一一";
- tree1.ID3 = "01010406";
- tree1.Name3 = "一一一一";
- tree2.ID = "01";
- tree2.Name = "一";
- tree2.ID1 = "0102";
- tree2.Name1 = "一二";
- tree2.ID2 = "010204";
- tree2.Name2 = "一二一";
- tree2.ID3 = "01010407";
- tree2.Name3 = "一二一一";
- tree3.ID = "01";
- tree3.Name = "一";
- tree3.ID1 = "0102";
- tree3.Name1 = "一二";
- tree3.ID2 = "010205";
- tree3.Name2 = "一二二";
- tree3.ID3 = "01020501";
- tree3.Name3 = "一二二一";
- tree4.ID = "02";
- tree4.Name = "二";
- tree4.ID1 = "0201";
- tree4.Name1 = "二一";
- tree4.ID2 = "020101";
- tree4.Name2 = "二一一";
- tree4.ID3 = "02010103";
- tree4.Name3 = "二一一一";
- tree5.ID = "02";
- tree5.Name = "二";
- tree5.ID1 = "0201";
- tree5.Name1 = "二一";
- tree5.ID2 = "020101";
- tree5.Name2 = "二一一";
- tree5.ID3 = "02010104";
- tree5.Name3 = "二一一二";
- tree6.ID = "02";
- tree6.Name = "二";
- tree6.ID1 = "0201";
- tree6.Name1 = "二一";
- tree6.ID2 = "020102";
- tree6.Name2 = "二一二";
- tree6.ID3 = "02010203";
- tree6.Name3 = "二一二一";
- tree7.ID = "02";
- tree7.Name = "二";
- tree7.ID1 = "0202";
- tree7.Name1 = "二二";
- tree7.ID2 = "020201";
- tree7.Name2 = "二二一";
- tree7.ID3 = "02020103";
- tree7.Name3 = "二二一一";
- //将测试数据存放到tree的list集合中
- tree.Add(tree1); tree.Add(tree2); tree.Add(tree3); tree.Add(tree4); tree.Add(tree5); tree.Add(tree6); tree.Add(tree7);
- List<Tree> treelist = new List<Tree>();
- //根据需要查询每个单位字段的个数,并附id和pid
- //一级菜单
- var AllCount1 = from p in tree
- group p by new
- {
- p.ID,
- p.Name
- } into g
- select g;
- foreach (var item in AllCount1)
- {
- Tree treea = new Tree();
- treea.PID = null;
- treea.ID=item.Key.ID;
- treea.Name = item.Key.Name;
- treelist.Add(treea);
- }
- //二级菜单
- var AllCount2 = from p in tree
- group p by new
- {
- p.ID1,
- p.Name1
- } into g
- select g;
- foreach (var item in AllCount2)
- {
- Tree treeb = new Tree();
- treeb.PID = item.Key.ID1.Substring(0,2);
- treeb.ID = item.Key.ID1;
- treeb.Name = item.Key.Name1;
- treelist.Add(treeb);
- }
- //三级菜单
- var AllCount3 = from p in tree
- group p by new
- {
- p.ID2,
- p.Name2
- } into g
- select g;
- foreach (var item in AllCount3)
- {
- Tree treec = new Tree();
- treec.PID = item.Key.ID2.Substring(0, 4);
- treec.ID = item.Key.ID2;
- treec.Name = item.Key.Name2;
- treelist.Add(treec);
- }
- //四级菜单
- var AllCount4 = from p in tree
- group p by new
- {
- p.ID3,
- p.Name3
- } into g
- select g;
- foreach (var item in AllCount4)
- {
- Tree treed = new Tree();
- treed.PID = item.Key.ID3.Substring(0, 6);
- treed.ID = item.Key.ID3;
- treed.Name = item.Key.Name3;
- treelist.Add(treed);
- }
- return Json(treelist, JsonRequestBehavior.AllowGet);
- }
- public class Tree
- {
- public string PID { get; set; }
- public string ID { get; set; }
- public string Name { get; set; }
- public string ID1 { get; set; }
- public string Name1 { get; set; }
- public string ID2 { get; set; }
- public string Name2 { get; set; }
- public string ID3 { get; set; }
- public string Name3 { get; set; }
- }</span>
效果图:
改进版:异步加载
用这种写法在数据量小的情况下一次性把所有节点的信息查询出来是没有问题的,但这次项目中实践中发现查询出的数据量有三万多,这样一次性快速显示是不容易的,还会造成浏览器卡死的情况。所以不得不采用zTree的异步加载来实现,初次加载显示父节点,每次点击节点后再查询加载子节点。不得不说通过查阅API文档发现zTree真的很强大。如果想了解详细内容可以看文档。
需要在代码中添加异步加载的方法:
- var setting = {
- data: {
- key: {
- name:"name"
- },
- simpleData: {
- enable: true,
- idKey: "ID",
- pIdKey: "PID",
- isParent: "isParent",
- rootPId:null
- }
- },
- view: {
- dblClickExpand: false,
- showLine: true,
- selectedMulti: false,
- expandSpeed:"fast"
- },
- async: {
- enable: true,
- url: "",
- autoParam: ["ID", "PID", "index"],
- type:"post"
- },
- check:{
- enable: true,
- chkStyle: "radio",
- radioType:"all"
- },
- callback: {
- onClick:reLoadOpenURL //节点被点击时调用的方法
- }
- }
- function reLoadOpenURL(event, treeId, treeNode) {
- if (treeNode.isParent) {
- }
- else {
- if (confirm("确定选择此商品吗?")) {
- window.opener.document.getElementById("pCode").value = treeNode.ID;
- window.close();
- }
- else { }
- }
- };
- $(document).ready(function () {
- $.fn.zTree.init(("#tree"), setting);
- });
后台的方法根据传递的参数再查询我们需要的每个节点信息,而不是一次性的全部查询出来。
小结:
zTree的核心就是父节点和子节点以及一些特定的格式,比如setting、界面ul存放,最后将查询到的信息存放到setting格式tree中的代码。
实现一个功能首先要了解需求,然后考虑实现思路,公司开发讲究的是效率,首先选择自己最擅长的方式来实现。
最近做项目的感受就是,没有什么难与不难,会用了就不难了,凭借很多工具和途径我们总会找到解决办法。在工作中遇到什么困难不要畏惧,最后都会解决的,先去想实现再说。
每天进步一点点~
摘自:http://blog.csdn.net/ww130929/article/details/51873392
以上是关于zTree简单实例与异步加载实例的主要内容,如果未能解决你的问题,请参考以下文章