EasyUI List<T>转tree数据格式
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 });
EasyUI List<T>转tree数据格式的更多相关文章
- Easyui实用视频教程系列---Tree点击打开tab页面
Easyui实用视频教程系列---Tree点击打开tab页面 首先 我们 要搭建环境 easyui 环境 然后 把tree 给创建出来 在某个位置 粘贴 下面代码 <ul id="tt ...
- 【整理】iview Tree数据格式问题,无限递归树处理数据
iview Tree数据格式问题,无限递归树处理数据 https://juejin.im/post/5b51a8a4e51d455d6825be20
- PHP+Mysql+easyui点击左侧tree菜单对应表名右侧动态生成datagrid加载表单数据(二)
关于tree菜单生成,参考我的另一篇博文地址tree 菜单 实现功能:点击左侧tree菜单中的table,右侧通过datagrid加载出该表对用的所有数据 难点:获取该表的所有列名,动态生成datag ...
- easyui struts后台实现tree返回json数据
首先jsp页面有一ul用于展现tree <ul id="trueULid"></ul> 加载tree <script type="text/ ...
- EasyUI实现异步载入tree(整合Struts2)
首先jsp页面有一ul用于展现Tree <ul id="mytree"></ul> 载入Tree <script type="text/ja ...
- EasyUI之树形结构tree
转自:https://blog.csdn.net/ya_1249463314/article/details/70305730 版权声明:本文为博主原创文章,未经博主允许不得转载. https://b ...
- jQuery easyui datagrid pagenation 的分页数据格式
{"total":28,"rows":[ {"productid":"FI-SW-01","unitco ...
- EasyUI学习笔记(1)----Tree控件实现过程中.NET下无法访问json数据的解决办法
直接调用官网的Demo中的方法 , 将json数据存储在同目录下,但是在运行之后树没有出现,用FireBug调试,错误如下 不允许访问json数据,刚开始以为是权限不够,然后又给解决方案所在的文件夹设 ...
- easyui tree 的数据格式转换
一般用来储存树数据的数据库表都含有两个整型字段:id pid,所以我们查询出来的List一般是这样的(约定pId为-1的节点为根节点): var serverList = [ {id : 2,pid ...
随机推荐
- python yield用法 (tornado, coroutine)
yield关键字用来定义生成器(Generator),其具体功能是可以当return使用,从函数里返回一个值,不同之处是用yield返回之后,可以让函数从上回yield返回的地点继续执行.也就是说,y ...
- 文本分布式表示(二):用tensorflow和word2vec训练词向量
看了几天word2vec的理论,终于是懂了一些.理论部分我推荐以下几篇教程,有博客也有视频: 1.<word2vec中的数学原理>:http://www.cnblogs.com/pegho ...
- Python中collections模块
目录 Python中collections模块 Counter defaultdict OrderedDict namedtuple deque ChainMap Python中collections ...
- cannot be cast to java.lang.Comparable
Exception in thread "main" java.lang.ClassCastException: com.myradio.People cannot be cast ...
- asp.net core系列 43 Web应用 Session分布式存储(in memory与Redis)
一.概述 HTTP 是无状态的协议. 默认情况下,HTTP 请求是不保留用户值或应用状态的独立消息. 本文介绍了几种保留请求间用户数据和应用状态的方法.下面以表格形式列出这些存储方式,本篇专讲Sess ...
- KnockoutJS-与服务端交互
几乎所有Web应用程序都要和服务器端交换数据,交换数据时最方便的就是使用JSON格式.Knockout可以实现很复杂的客户端交互,对于前后端交互使用的技术最为基本且常用的是Ajax,本次利用Ajax和 ...
- springbatch的封装与使用
springbatch 主要实现批量数据的处理,我对batch进行的封装,提出了jobBase类型,具体job需要实现它即可.Spring Batch 不仅提供了统一的读写接口.丰富的任务处理方式.灵 ...
- 【Python3爬虫】常见反爬虫措施及解决办法(二)
这一篇博客,还是接着说那些常见的反爬虫措施以及我们的解决办法.同样的,如果对你有帮助的话,麻烦点一下推荐啦. 一.防盗链 这次我遇到的防盗链,除了前面说的Referer防盗链,还有Cookie防盗链和 ...
- SLAM+语音机器人DIY系列:(五)树莓派3开发环境搭建——1.安装系统ubuntu_mate_16.04
摘要 通过前面一系列的铺垫,相信大家对整个miiboo机器人的DIY有了一个清晰整体的认识.接下来就正式进入机器人大脑(嵌入式主板:树莓派3)的开发.本章将从树莓派3的开发环境搭建入手,为后续ros开 ...
- 如何让div中的table水平居中
<div style="text-align:center"> <table border="1" cellpadding="3&q ...