.net前端后台两种方式处理树形结构(tree)
来源:互联网 发布:什么鼠标垫好用 知乎 编辑:程序博客网 时间:2024/05/19 14:56
数据库:
1.前端处理方式:
//后台返回的json字符串:
<pre name="code" class="javascript">var data ="[{"fatherId":0,"sonId":9999,"name":"中国"},{"fatherId":1,"sonId":0,"name":"湖北省"},{"fatherId":2,"sonId":1,"name":"武汉市"},{"fatherId":3,"sonId":1,"name":"咸宁市"},{"fatherId":4,"sonId":3,"name":"崇阳县"},{"fatherId":5,"sonId":3,"name":"温泉区"},{"fatherId":6,"sonId":2,"name":"江岸区"},{"fatherId":7,"sonId":2,"name":"江夏区"},{"fatherId":8,"sonId":0,"name":"湖南省"},{"fatherId":9,"sonId":8,"name":"长沙市"}]";
//前端处理:var node = { id: _data[0].fatherId, pid: _data[0].sonId, text: _data[0].name, children: [] };getAreaTree(_data, node);function getAreaTree(data,pnode){ var _Name = []; var temp = {id:"",pid:"",text:"",children:[]}; for (var i = 0; i < data.length; i++) { if (data[i].sonId == 0 && pnode.id == 0) { temp = {}; if (_Name.indexOf(data[i].name) >= 0) { continue; } _Name.push(data[i].name); temp = { id: data[i].fatherId,pid:data[i].sonId, text: data[i].name, children: [] }; pnode.children.push(temp); getAreaTree(data, pnode.children[pnode.children.length - 1]); } else if (data[i].sonId == pnode.id) { temp = { id: data[i].fatherId, pid: data[i].sonId, text: data[i].name, children: [] }; pnode.children.push(temp); getAreaTree(data, pnode.children[pnode.children.length - 1]); } }}
<span style="font-family:Arial;"><img src="http://img.blog.csdn.net/20151105160433969?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /></span>
<span style="font-family:Arial;"></span>
2.后台处理方式:
<span style="font-family:Arial;">//后台代码:webservice</span>
<span style="font-family:Arial;"></span>
[WebMethod] public string getTree() { string msg = ""; try { string sql = "select * from Trees"; DataTable dt = SqlHelper.ExecuteDataTable(sql); IList<Area> list = null; if (dt.Rows.Count > 0) { list = Table2List(dt); } Tree tree = new Tree() { id = list[0].fatherId, pid = list[0].sonId, text = list[0].name, children=new List<Tree>() }; string name = ""; tree= GetTree(list, tree, name); msg = JsonConvert.SerializeObject(tree); } catch (Exception ex) { msg = ex.Message; } return msg; } public Tree GetTree(IList<Area> list ,Tree pnode,string _name) { Tree temp = new Tree(); foreach (Area model in list) { if (model.sonId == 0 && pnode.id == 0) { if (_name==model.name) { continue; } temp = new Tree() { id = model.fatherId, pid = model.sonId, text = model.name, children = new List<Tree>() }; _name =model.name; pnode.children.Add(temp); GetTree(list, pnode.children[pnode.children.Count - 1], _name); } else if (model.sonId == pnode.id) { temp = new Tree() { id = model.fatherId, pid = model.sonId, text = model.name, children = new List<Tree>() }; pnode.children.Add(temp); GetTree(list, pnode.children[pnode.children.Count - 1], _name); } } return pnode; } public static IList<Area> Table2List(DataTable dt) { IList<Area> ts = new List<Area>();// 定义集合 Type type = typeof(Area); // 获得此模型的类型 string tempName = ""; foreach (DataRow dr in dt.Rows) { Area t = new Area(); PropertyInfo[] propertys = t.GetType().GetProperties();// 获得此模型的公共属性 foreach (PropertyInfo pi in propertys) { tempName = pi.Name; if (dt.Columns.Contains(tempName)) { if (!pi.CanWrite) continue; object value = dr[tempName]; if (value != DBNull.Value) pi.SetValue(t, value, null); } } ts.Add(t); } return ts; }
<pre name="code" class="csharp"> public class Tree { public int id { get;set;} public int pid { get;set;} public string text { get;set;} public List<Tree> children { get;set;} }
<span style="font-family:Arial;"></span><pre name="code" class="javascript">var data ="{"id":0,"pid":9999,"text":"中国","children":[{"id":1,"pid":0,"text":"湖北省","children":[{"id":2,"pid":1,"text":"武汉市","children":[]},{"id":3,"pid":1,"text":"咸宁市","children":[]}]},{"id":8,"pid":0,"text":"湖南省","children":[{"id":9,"pid":8,"text":"长沙市","children":[]}]}]}";
1 0
- .net前端后台两种方式处理树形结构(tree)
- 多表关联前端生成json传值给后台(类似树形结构)
- 树形结构数据后台处理:一次循环生成树
- java 生成 树形结构数据(tree)
- 前后端分离,前端跨域访问后台的两种方式
- 前后端分离,前端跨域访问后台的两种方式
- java树形结构 两种算法
- 保证前端请求方式和后台处理方式一致
- html中转义字符的处理 前端和后端两种处理方式
- 前端页面实现树形结构
- asp.net 从后台得到htmlinput的值的两种方式
- EasyUI之树形结构tree
- EasyUI之树形结构tree
- java访问后台的两种方式
- Struts2后台校验两种方式
- 树形结构后台数据代码拼装(基于上一篇)
- unity树形结构(Script Based Runtime Tree-View Control)
- php tree类的使用(树形结构)
- 构造器、重载和this关键字
- mysql 添加用户 可以远程访问
- 阿里云服务器配置VPN服务
- 模拟量信号滤波算法
- MFC的字符串分解函数AfxExtractSubString
- .net前端后台两种方式处理树形结构(tree)
- 自定义控件其实很简单 八
- 网桥和交换机的区别
- start_with connect_by_prior
- ssh登录navicat mysql报错问题
- Mybatis的if test字符串比较问题
- jQuery源码分析之load方法
- Android学习笔记之AndroidManifest.xml文件解析
- 为什么我们可以在非UI线程中更新UI