zoukankan      html  css  js  c++  java
  • 树呈现

     public static IList<EasyUiTree> LoadEntityTree(string valueField, string tableName, string nodeSourceKey, string parentKey, string textField, string where = "", string levelField = "", string StrucIdKey = "")
            {
                var Dao = GetDao();
                string sql = "select * from " + tableName + " where 1=1 " + where;
                System.Data.DataTable dt = Dao.ExecuteDataSet(new QueryInfo { CustomSQL = sql }).Tables[0];
                List<EasyUiTree> trees = new List<EasyUiTree>();
                List<string> roots = new List<string>();//第一层节点,即parentid为null的节点
                Dictionary<string, EasyUiTree> allnodes = new Dictionary<string, EasyUiTree>();
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    object parentObj = dt.Rows[i][parentKey];
                    string id = dt.Rows[i][valueField].ToString();
                    string text = dt.Rows[i][textField].ToString();
                    EasyUiTree child = new EasyUiTree();
                    child.id = id;//#code01
                    child.text = text;
                    if (allnodes.ContainsKey(id))//非叶节点
                    {
                        allnodes[id].text = text;
                    }
                    else
                    {
                        allnodes[id] = child;
                        child.children = new List<EasyUiTree>();
                    }
                    //没上级的为根元素
                    if (parentObj == null || string.IsNullOrEmpty(parentObj.ToString()))
                    {
                        if (!roots.Contains(id))
                        {
                            child.children = new List<EasyUiTree>();
                            roots.Add(id);
                        }
    
                    }
                    else
                    {
                        //#code02
                        string parentId = parentObj.ToString();
                        if (!allnodes.ContainsKey(parentId))
                        {
    
                            EasyUiTree e = null;
                            e = new EasyUiTree();
                            e.id = parentId;
                            e.children = new List<EasyUiTree>();
                            allnodes.Add(parentId, e);
    
                        }
                        if (!allnodes.ContainsKey(child.id))
                        {
                            allnodes[parentId].children.Add(child);
                        }
                        else
                        {
                            allnodes[parentId].children.Add(allnodes[child.id]);
                        }
                    }
                }
                foreach (var e in allnodes)
                {
                    if (roots.Contains(e.Key))
                    {
                        trees.Add(e.Value);
                    }
                }
                return trees;
            }
  • 相关阅读:
    MinIO:客户端mc
    jenkins:修改默认工作目录
    Shell:cut工具
    使用ArrayList时代码内部发生了什么(jdk1.7)?
    Heap Sorting 总结 (C++)
    我的第一篇——nginx+naxsi总结篇1
    PHP学习-验证用户名密码
    springboot使用事务
    springboot数据格式验证(二)——自定义日期格式验证
    springboot数据格式验证(一)
  • 原文地址:https://www.cnblogs.com/kexb/p/10132905.html
Copyright © 2011-2022 走看看