using System;
|
using System.Collections.Generic;
|
using System.Data;
|
using System.Linq;
|
using System.Web;
|
|
namespace WebAPI.Models
|
{
|
public class ToHierarchy
|
{
|
public static List<Menu> ToHierarchyList(List<Menu> limits, string HPartentID/*int? HPartentID = 0*/)
|
{
|
List<Menu> newLimitList = new List<Menu>();
|
newLimitList = limits.Where(t => t.HPartentID == HPartentID).ToList();
|
for (int i = 0; i < newLimitList.Count; i++)
|
{
|
newLimitList[i].childMenus = ToHierarchyList(limits, newLimitList[i].HPartentID);//下一层级
|
}
|
return newLimitList;
|
}
|
|
|
public static void digui(DataTable dt, List<Menu> tree)
|
{
|
for (int m = 0; m < tree.Count; m++)
|
{
|
tree[m].childMenus = new List<Menu>();
|
for (int i = 0; i < dt.Rows.Count; i++)//第一次循环,得到所有根节点的子集
|
{
|
if (tree[m].HitemID.ToString() == dt.Rows[i]["HPartentID"].ToString() && dt.Rows[i]["HitemID"].ToString() != dt.Rows[i]["HPartentID"].ToString())
|
{
|
Menu tbjson = new Menu();
|
tbjson.HitemID = dt.Rows[i]["HitemID"].ToString();
|
tbjson.HPartentID = dt.Rows[i]["HPartentID"].ToString();
|
tbjson.HNumber = dt.Rows[i]["HNumber"].ToString();
|
tbjson.HName = dt.Rows[i]["HName"].ToString();
|
tbjson.HLevel = tree[m].HLevel;
|
tbjson.Hurl = dt.Rows[i]["Hurl"].ToString();
|
tree[m].childMenus.Add(tbjson);
|
}
|
}
|
for (int i = 0; i < tree[m].childMenus.Count; i++)
|
{
|
digui(dt, tree[m].childMenus);//再次用子集去循环,拿出子集的子集
|
}
|
}
|
|
}
|
}
|
}
|