博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
EasyUI List<T>转tree数据格式
阅读量:4986 次
发布时间:2019-06-12

本文共 3212 字,大约阅读时间需要 10 分钟。

using System;using System.Collections;using System.Collections.Generic;using System.Linq;using System.Linq.Expressions;using System.Reflection;namespace XHP{    ///     ///     ///     public class TreeDataHelper
where T:new() { public class TreeModelBase { public string id { get; set; } public string text { get; set; } ///
/// closed /// public string state { get; set; } public List
children { get; set; } } ///
/// /// ///
///
///
///
///
///
///
///
树默认展开级别-1全不展开,0展开所有,1只展开到1级 ///
public static List
GetTreeDataFromList
(List
list, Expression
> idFieldExp, Expression
> textFieldExp, Expression
> pidFieldExp, string rootValue, string emptyRootName="==全部==",int expendLevel=1) { Hashtable tb = new Hashtable(); var idProp = typeof(T).GetProperty(((MemberExpression)idFieldExp.Body).Member.Name); var textProp = typeof(T).GetProperty(((MemberExpression)textFieldExp.Body).Member.Name); var pidProp = typeof(T).GetProperty(((MemberExpression)pidFieldExp.Body).Member.Name); T parent = default(T); if(!string.IsNullOrWhiteSpace(rootValue)) { parent = list.FirstOrDefault(x => idProp.GetValue(x)?.ToString() == rootValue); } TreeModelBase rlt = new TreeModelBase(); if (parent == null) { rlt.id = rootValue??""; rlt.text = emptyRootName; } else { rlt.id = idProp.GetValue(parent).ToString(); rlt.text = textProp.GetValue(parent).ToString(); } rlt.state = expendLevel > -1 ? null : "closed"; var currentLevel = 1; GetTreeDataFromList_SetChild(rlt, list, idProp, textProp, pidProp, expendLevel, currentLevel); return new List
{ rlt } ; } private static void GetTreeDataFromList_SetChild(TreeModelBase parent,List
list,PropertyInfo idProp,PropertyInfo textProp, PropertyInfo pidProp, int expendLevel,int currentLevel) { var childs = list.Where(x => (pidProp.GetValue(x)?.ToString() ?? "") == (parent.id ?? "") &&!string.IsNullOrWhiteSpace(idProp.GetValue(x)?.ToString())); if (childs.Count() == 0) { parent.state = null; return; } else { parent.children = new List
(); foreach (var item in childs) { var tempChild = new TreeModelBase(); tempChild.id = idProp.GetValue(item).ToString(); tempChild.text = textProp.GetValue(item).ToString(); if (expendLevel == 0) tempChild.state = null; else if (expendLevel == -1) tempChild.state = "closed"; else { tempChild.state = currentLevel < expendLevel ? null : "closed"; } currentLevel++; GetTreeDataFromList_SetChild(tempChild, list, idProp, textProp, pidProp, expendLevel, currentLevel); parent.children.Add(tempChild); } } } }}

  

今天在用到EasyUI 的Tree,TreeGrid,每次转出这个数据格式非常不爽,就自己写了段HELPER

输出到前端:

JsonConvert.SerializeObject(TreeDataHelper<T>.GetTreeDataFromList(tList, x1 => x1.Id, x1 => x1.Name, x1 => x1.ParentId, "root", "==所有分类==", 0),

new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });

转载于:https://www.cnblogs.com/xvan/p/10097599.html

你可能感兴趣的文章
一些常用的js,jquerry 样例
查看>>
Oracle PL/SQL 多重选择句
查看>>
dorado中的creationType选择类型
查看>>
C++11 数值类型和字符串的相互转换
查看>>
无锡盈达聚力科技有限公司
查看>>
tyvj1659中中救援队
查看>>
kubernetes学习:CKA考试题
查看>>
LINUX samba的安装使用
查看>>
CSS border 生成三角
查看>>
asp.net(c#)开发中的文件上传组件uploadify的使用方法(带进度条)
查看>>
7.STM32中GPIO理解
查看>>
base64 json
查看>>
在vim中搜索单词
查看>>
设置定点数学属性
查看>>
自动化测试工具 Test Studio入门教程
查看>>
Python之进程线程
查看>>
排序算法(一) —— 冒泡排序
查看>>
No.026:Remove Duplicates from Sorted Array
查看>>
SpringBoot项目的几种创建方式,启动、和访问
查看>>
窗外【1】
查看>>