EasyUITree实现树形结构Json串


// 方法一
// 此方法可多级部门使用
        public string Bingztree()
        {
            string where = "";
            List<Dept> deptList = DeptService.GetInstance().Select("SelectDept", where).ToList();
            StringBuilder sb = new StringBuilder();
            string departmentTree = null;
            foreach (Dept d in deptList)
            {
                if (Convert.ToInt32(d.ParentId) == 0)
                {
                    string temp = null;
                    temp = "\"id\":" + d.DetpId + ",\"text\":\"" + d.DeptName + "\",";
                    string child = FindChild(Convert.ToInt32(d.DetpId), deptList);
                    if (child != null)
                    {
                        temp += child;
                        departmentTree = "[{" + temp + "}]";
                        sb.Append(departmentTree);
                    }
                }
            }
            string finalTree = sb.ToString();
            finalTree = finalTree.Replace("][",",");
            return finalTree;
        }


        private string FindChild(int id, List<Dept> deptList)
        {
            bool flag = false;
            string departmentChild = null;
            foreach (Dept d in deptList)
            {
                string anotherChild = null;
                if (Convert.ToInt32(d.ParentId) == id)//寻找到子节点
                {
                    anotherChild = "\"id\":" + d.DetpId + ",\"text\":\"" + d.DeptName + "\",";
                    string child = FindChild(Convert.ToInt32(d.DetpId), deptList);
                    if (child != null)
                    {
                        anotherChild = anotherChild + child;
                    }
                    if (anotherChild[anotherChild.Length - 1] == ',')
                    {
                        anotherChild = anotherChild.Remove(anotherChild.Length - 1);
                    }
                    anotherChild = "{" + anotherChild + "}";
                    departmentChild += anotherChild + ",";
                }
                else
                {
                    flag = false;
                }
            }
            if (departmentChild != null)
            {
                departmentChild = departmentChild.Remove(departmentChild.Length - 1);
                departmentChild = "\"children\":[" + departmentChild + "]";
            }
            return departmentChild;
        }




















/// 方法二
/// 备注:此方法仅限两级部门使用
/// <summary>
        /// 查询出核实单位,返回json格式
        /// </summary>
        /// <returns></returns>
        public string Bingztree()
        {
            string where = "";
            IList<Dept> deptList = DeptService.GetInstance().Select("SelectDept", where);
            StringBuilder sb = new StringBuilder();
            sb.Append("[");
            List<object> list = new List<object>();
            for (int i = 0; i < deptList.Count; i++)
            {
                list.Add(deptList[i].ParentId);
            }
            for (int i = 0; i < deptList.Count; i++)
            {
                if ((int)deptList[i].ParentId == 0)
                {
                    sb.Append("{");
                    sb.Append(" \"id\": \"" + deptList[i].DetpId + "\",\"text\": \"" + deptList[i].DeptName + "\"");
                    if (list.Contains(deptList[i].DetpId))
                    {
                        sb.Append(",\"children\":[{");
                        for (int j = 0; j < deptList.Count; j++)
                        {
                            if ((int)deptList[j].ParentId == (int)deptList[i].DetpId)
                            {
                                sb.Append("\"id\": \"" + deptList[j].DetpId + "\",\"text\": \"" + deptList[j].DeptName + "\"},{");
                            }
                        }
                        sb.Append("]},");
                    }
                    else if ((int)deptList[i].ParentId == 0 && !list.Contains(deptList[i].DetpId))
                    {
                        sb.Append("},");
                    }
                }
            }
            string str1 = sb.ToString().Substring(0, sb.Length - 1) + "]";
            str1 = str1.Replace("},{]}", "}]}");
            return str1;
        }



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值