1 js上需要的树形数据 示例:
data = [{
title: '一级1'
,id: 1
,field: 'name1'
,checked: true
,spread: true
,children: [{
title: '二级1-1 可允许跳转'
,id: 3
,field: 'name11'
,href: 'https://www.layui.com/'
}]
}]
2 C#后台获取的数据是多条的,并且有parentID的数据。
1 直接获取list形式的数据
List<CategoryInfo> categoryInfo = Categories.GetCategoryList();
2 获取一个datatable数据
DataTable dt = bll.GetDataTable(intBaseId);
//将datatable转换成list
IList<resModel> resultList = ModelConvertHelper<resModel>.ConvertToModel(dt);
3 调用转换树形数据的方法
//data 是转换后的树形数据
var data= fun1(resultList);
//转换数据的一系列方法
(1)先定义一个树形数据结构的model
public class TreeModel
{
public string title { set; get; }
public int id { set; get; }
public int ParentId { set; get; }
public List<TreeModel> children { set; get; }
}
(2)fun1方法
public List<TreeModel> fun1(IList<resModel> m)
{
//"CateId", "ParentId", "Name"是要返回的树形结构上的字段对应 list数据的数据库字段
var aa = ConversionList(m, "CateId", "ParentId", "Name");
return aa;
}
/// <summary>
/// 公用递归(反射转换List)
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="allList">数据列表</param>
/// <param name="parentId">父级ID</param>
/// <param name="idField">id字段名称</param>
/// <param name="parentIdField">父级id字段名称</param>
/// <param name="nameField">name字段名称</param>
/// <returns></returns>
public List<TreeModel> ConversionList<T>(IList<T> allList, string CateId, string ParentId, string Name)
{
List<TreeModel> list = new List<TreeModel>();
TreeModel model = null;
foreach (var item in allList)
{
model = new TreeModel();
foreach (PropertyInfo p in item.GetType().GetProperties())
{
//对比list数据
if (p.Name == CateId)
{
//将对应的字段赋值给 树形返回值的字段上
model.id = int.Parse(p.GetValue(item).ToString());
}
if (p.Name == ParentId)
{
model.ParentId = int.Parse(p.GetValue(item).ToString());
}
else if (p.Name == Name)
{
model.title = p.GetValue(item).ToString();
}
else
{
}
}
list.Add(model);
}
// "0" 是一级栏目的父id,一般一级栏目的父id都是0
return OperationParentData(list, "0");
}
public List<TreeModel> ConversionList<T>(IList<T> allList, string CateId, string ParentId, string Name)
{
List<TreeModel> list = new List<TreeModel>();
TreeModel model = null;
foreach (var item in allList)
{
model = new TreeModel();
foreach (PropertyInfo p in item.GetType().GetProperties())
{
if (p.Name == CateId)
{
model.id = int.Parse(p.GetValue(item).ToString());
}
if (p.Name == ParentId)
{
model.ParentId = int.Parse(p.GetValue(item).ToString());
}
else if (p.Name == Name)
{
model.title = p.GetValue(item).ToString();
}
else
{
}
}
list.Add(model);
}
return OperationParentData(list, "0");
}
/// <summary>
/// 公用递归(处理递归最父级数据)
/// </summary>
/// <param name="treeDataList">树形列表数据</param>
/// <param name="parentId">父级Id</param>
/// <returns></returns>
public List<TreeModel> OperationParentData(List<TreeModel> treeDataList, string parentId)
{
var data = treeDataList.Where(x => x.ParentId == int.Parse(parentId));
List<TreeModel> list = new List<TreeModel>();
//TreeModel model = new TreeModel();
//model.CategoryId = "0";
//model.ParentId = 0;
//model.name = "一级栏目";
//model.children = null;
//list.Add(model);
foreach (var item in data)
{
OperationChildData(treeDataList, item);
list.Add(item);
}
return list;
}
/// <summary>
/// 公用递归(递归子级数据)
/// </summary>
/// <param name="treeDataList">树形列表数据</param>
/// <param name="parentItem">父级model</param>
public void OperationChildData(List<TreeModel> treeDataList, TreeModel parentItem)
{
var subItems = treeDataList.Where(ee => ee.ParentId == parentItem.id).ToList();
if (subItems.Count != 0)
{
parentItem.children = new List<TreeModel>();
parentItem.children.AddRange(subItems);
foreach (var subItem in subItems)
{
OperationChildData(treeDataList, subItem);
}
}
}