树形结构数据_Stars
作者:互联网
一次性遍历返回树形
表模型
namespace ViewModels
{
public partial class Terr : BaseViewModel
{
/// <summary>
/// 树形ID
/// </summary>
public string Id { get; set; }
/// <summary>
/// 树形名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 树形父级ID
/// </summary>
public string ParentId { get; set; }
/// <summary>
/// 树形子级集合
/// </summary>
public List<Rank> RankList { get; set; }
}
}
功能实现
public async Task<MessageModel<List<Terr>>> GetTreeNode()
{
MessageModel<List<Terr>> result = new MessageModel<List<Terr>>();
try
{
//查询出所有数据
var terrList = await _treeNodeService.GetTreeNode();
//1找到一级父亲
var parents = terrList.FindAll(p => !string.IsNullOrWhiteSpace(ParentId));
//2给一级父元素递归赋值子元素
foreach (var item in parents)
{
item.TerrList = await ConvertToChild(terrList, item.Id);
}
result.response = parents;
}
catch (Exception ex)
{
result.msg = "查询异常" + ex.Message;
}
result.success = true;
result.msg = "查询成功";
return result;
}
//遍历子级数据
private async Task<MessageModel<List<Terr>>> ConvertToChild(List<Terr> source, string parentId)
{
MessageModel<List<Terr>> result = new MessageModel<List<Terr>>();
try
{
//当父级存在是继续遍历
var parents = source.FindAll(p => p.ParentId == parentId);
foreach (var item in parents)
{
item.TerrList = ConvertToChild(source, item.Id);
}
result.response = parents;
}
catch (Exception ex)
{
result.msg = "查询异常" + ex.Message;
}
result.success = true;
result.msg = "查询成功";
return result;
}
根据父级查询对应子级
返回数据模型
using System;
using SqlSugar;
using System.Text;
using ViewModels;
using System.Collections.Generic;
using Enums;
namespace ViewModels
{
///<summary>
///宿舍树形节点
///</summary>
///表模型
public partial class DormsterTreeListModel : BaseViewModel
{
public DormsterTreeListModel ()
{
ChildrenData = new List<Tree>();
}
/// <summary>
/// 树形ID
/// </summary>
public string Id { get; set; }
/// <summary>
/// 树形节点类型
/// </summary>
public TreeEnum? TreeType { get; set; }
/// <summary>
/// 树形名称
/// </summary>
public string TreeName { get; set; }
/// <summary>
/// “无”子级节点 ?
/// </summary>
public bool? IsLeaf { get; set; }
/// <summary>
/// 子节点集合
/// </summary>
public List<Tree> ChildrenData { get; set; }
}
/// <summary>
/// 子节点
/// </summary>
public class Tree
{
/// <summary>
/// 树形ID
/// </summary>
public string Id { get; set; }
/// <summary>
/// 树形节点类型
/// </summary>
public TreeEnum? TreeType { get; set; }
/// <summary>
/// 树形名称
/// </summary>
public string TreeName { get; set; }
/// <summary>
/// 是否存在子级节点
/// </summary>
public bool? IsLeaf { get; set; }
}
}
所需枚举
/// <summary>
///宿舍树形节点(校区 = 0,宿舍楼 = 1 ,宿舍楼层 = 2 ,宿舍 = 3)
/// </summary>
public enum TreeEnum
{
/// <summary>
/// 校区
/// </summary>
[Description("校区")]
SchoolArea = 0,
/// <summary>
/// 宿舍楼
/// </summary>
[Description("宿舍楼")]
DormFloor = 1,
/// <summary>
/// 宿舍楼层
/// </summary>
[Description("宿舍楼层")]
DormFloorLayer = 2,
/// <summary>
/// 宿舍
/// </summary>
[Description("宿舍")]
Dorm = 3,
}
功能实现
#region 宿舍树
/// <summary>
/// 创建宿舍树
/// </summary>
/// <param name="parentId">父级ID</param>
/// <param name="treeType"> 宿舍树形节点(校区 = 0,宿舍楼 = 1 ,宿舍楼层 = 2 ,宿舍 = 3)</param>
/// <returns></returns>
public async Task<List<DormsterTreeListModel>> GetDormTreeList(string parentId, TreeEnum? treeType = TreeEnum.SchoolArea)
{
List<DormsterTreeListModel> dormsterTreeMList = new List<DormsterTreeListModel>();
try
{
switch (treeType)
{
//节点为校区
case TreeEnum.SchoolArea:
List<SchoolArea> schoolAreaList = await _schoolAreaServices.GetSchoolAreaList(organizeId: _user.OrganizeId, deleted: false, enabled: true);
if (schoolAreaList == null || !schoolAreaList.Any())
{
return null;
}
foreach (var sItem in schoolAreaList)
{
dormsterTreeListModel treeListModel = new dormsterTreeListModel();
treeListModel.Id = sItem.SchoolAreaId;
treeListModel.TreeName = sItem.SchoolAreaName;
treeListModel.TreeType = TreeEnum.SchoolArea;
treeListModel.IsLeaf = false;
dormsterInfoTreeMList.Add(treeListModel);
}
break;
//节点为宿舍楼
case TreeEnum.DormFloor:
List<DormFloorInfo> dormFloorList = await _dormFloorService.GetDormFloorInfoList(organizeId: _user.OrganizeId, schoolAreaId: parentId, deleted: false, enabled: true);
if (dormFloorList == null || !dormFloorList.Any())
{
return null;
}
List<DormFloorInfo> dormFloorList = dormFloorList.Where(f => f.ParentId == null || f.ParentId == "").Select(fe => { return fe.Map<DormFloorInfo>(); }).ToList();
foreach (var sItem in dormFloorList)
{
dormsterTreeListModel treeListModel = new dormsterTreeListModel();
treeListModel.Id = sItem.DormFloorId;
treeListModel.TreeName = sItem.DormFloorName;
treeListModel.TreeType = TreeEnum.DormFloor;
treeListModel.IsLeaf = false;
dormsterInfoTreeMList.Add(treeListModel);
}
break;
//节点为宿舍楼层
case TreeEnum.DormFloorLayer:
List<DormFloorInfo> dormFloorLayerList = await _dormInfoServices.GetDormFloorInfoList(organizeId: _user.OrganizeId, parentId: parentId, deleted: false, enabled: true);
if (dormFloorLayerList == null || !dormFloorLayerList.Any())
{
return null;
}
foreach (var sItem in dormFloorLayerList)
{
DormsterTreeListModel treeListModel = new DormsterTreeListModel();
treeListModel.Id = sItem.DormFloorId;
treeListModel.TreeName = sItem.DormFloorName;
treeListModel.TreeType = TreeEnum.DormFloorLayer;
treeListModel.IsLeaf = false;
dormsterInfoTreeMList.Add(treeListModel);
}
break;
//节点为宿舍
case TreeEnum.Dorm:
List<DormInfo> dormInfoList = await _dormInfoServices.GetDormInfoList(organizeId: _user.OrganizeId, layerId: parentId, deleted: false, enabled: true);
if (dormInfoList == null || !dormInfoList.Any())
{
return null;
}
foreach (var sItem in dormInfoList)
{
DormsterTreeListModel treeListModel = new DormsterTreeListModel();
treeListModel.Id = sItem.DormId;
treeListModel.TreeName = sItem.DormName;
treeListModel.TreeType = TreeEnum.Dorm;
treeListModel.IsLeaf = true;
dormsterInfoTreeMList.Add(treeListModel);
}
break;
}
}
catch (Exception ex)
{
}
//将树形结构排序
return dormsterInfoTreeMList.OrderBy(d => d.TreeName).ToList();
}
#endregion
标签:treeListModel,set,get,树形,Stars,TreeEnum,public,结构 来源: https://www.cnblogs.com/Stars0121/p/16496965.html