ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

树形结构数据_Stars

2022-07-20 11:05:25  阅读:64  来源: 互联网

标签:treeListModel set get 树形 Stars TreeEnum public 结构


一次性遍历返回树形

表模型

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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有