.net前端后台两种方式处理树形结构(tree)

数据库:

.net前端后台两种方式处理树形结构(tree)_第1张图片

1.前端处理方式:

//后台返回的json字符串:
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]);
        }
    }
}


 
   


2.后台处理方式:

//后台代码:webservice
        [WebMethod]
        public string getTree()
        {
            string msg = "";
            try
            {
                string sql = "select * from Trees";
                DataTable dt = SqlHelper.ExecuteDataTable(sql);
                IList 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()
                };
                string name = "";
               tree= GetTree(list, tree, name);
               msg = JsonConvert.SerializeObject(tree);
            }
            catch (Exception ex)
            {
                msg = ex.Message;
            }
            return msg;
        }
        public Tree GetTree(IList 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()
                     };
                    _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()
                     };
                   pnode.children.Add(temp);
                   GetTree(list, pnode.children[pnode.children.Count - 1], _name);
                }
            }
            return pnode;
        }
        public static IList Table2List(DataTable dt)
        {
            IList ts = new List();// 定义集合
            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;
        }

    public class Tree
    {
        public int id { get;set;}

        public int pid { get;set;}

        public string text { get;set;}

        public List children { get;set;}

    }

 
   
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":[]}]}]}";

.net前端后台两种方式处理树形结构(tree)_第2张图片
 
   



你可能感兴趣的:(asp.net,.net,web,service,json,tree)