EasyUI List<T>转tree数据格式
2018-12-11 09:05:57来源:博客园 阅读 ()
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
namespace XHP
{
/// <summary>
///
/// </summary>
public class TreeDataHelper<T> where T:new()
{
public class TreeModelBase
{
public string id { get; set; }
public string text { get; set; }
/// <summary>
/// closed
/// </summary>
public string state { get; set; }
public List<TreeModelBase> children { get; set; }
}
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <typeparam name="TProperty"></typeparam>
/// <param name="list"></param>
/// <param name="idFieldExp"></param>
/// <param name="textFieldExp"></param>
/// <param name="rootValue"></param>
/// <param name="emptyRootName"></param>
/// <param name="expendLevel">树默认展开级别-1全不展开,0展开所有,1只展开到1级</param>
/// <returns></returns>
public static List<TreeModelBase> GetTreeDataFromList<TProperty1, TProperty2, TProperty3>(List<T> list, Expression<Func<T, TProperty1>> idFieldExp,
Expression<Func<T, TProperty2>> textFieldExp, Expression<Func<T, TProperty3>> 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<TreeModelBase> { rlt } ;
}
private static void GetTreeDataFromList_SetChild(TreeModelBase parent,List<T> 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<TreeModelBase>();
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 });
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- .NET下通过HttpListener实现简单的Http服务 2020-02-27
- 详解ASP.NET控件之RadioButtonList 2019-11-17
- listview里的button事件添加方法 2019-10-08
- asp.net中Datalist使用数字分页的实现方法 2019-09-17
- 高性能TcpServer - 3.命令通道(处理:掉 2019-07-24
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash
