.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
原创粉丝点击