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 ...
随机推荐
- 带你了解SDL
SDL(英语:Simple DirectMedia Layer)是一套开放源代码的跨平台多媒体开发库,使用C语言写成.SDL提供了数种控制图像.声音.输出入的函数,让开发者只要用相同或是相似的代码就可 ...
- Python档案袋(生成器、迭代器、队列 )
生成器: 简单的生成器实现: #生成器,将for循环的变量传递到前面的式子进行处理 #生成的并不是一个列表,而是一个存在算数规则的对象 #不能通过下标直接取值,必须一个一个从头到尾取 va=(i*2 ...
- 我眼中的 Nginx(四):是什么让你的 Nginx 服务退出这么慢?
张超:又拍云系统开发高级工程师,负责又拍云 CDN 平台相关组件的更新及维护.Github ID: tokers,活跃于 OpenResty 社区和 Nginx 邮件列表等开源社区,专注于服务端技术的 ...
- 利用策略模式优化过多 if else 代码
前言 不出意外,这应该是年前最后一次分享,本次来一点实际开发中会用到的小技巧. 比如平时大家是否都会写类似这样的代码: if(a){ //dosomething }else if(b){ //dosh ...
- Mybatis之旅第六篇-关联查询
一.引言 通过动态SQL我们可以进行复杂SQL的编写,但之前的例子都是单表查询,在实际开发中,当然不可能都是单表,很多时候我们需要进行关联多表查询(有些公司为了性能还是尽量的使用单表查询),表与表之间 ...
- java游戏开发杂谈 - 创建一个窗体
package game1; import javax.swing.JFrame; /** * java游戏开发杂谈 * ---demo1:创建一个窗体 * * @author 台哥 * @date ...
- 无敌简单快速的文件服务器sgfs
前言 想要构建一个Linux文件服务器?看看下面几个要求是不是你想要的? 1.只需要单节点部署就够了 2.部署启动简单,下载之后,一键启动,一键关闭 3.不需要任何其他的依赖安装,而且运行时占用内存资 ...
- Java - 静态代理详讲
Java - 静态代理详讲 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 写在前面:*此章内容比较抽象,所以需要结合实际操作进行讲解* *需要有 ...
- 软件开发架构介绍||OSI七层协议之物理层、数据链路层、网络层、传输层(mac地址、ip协议、断开协议、tcp协议之三次握手四次挥手)
一.网络编程 软件开发架构 C/S架构 C:客户端 想体验服务的时候才会去找服务端体验服务 S:服务端 24小时不间断的提供服务,即时监听,随时待命 B/S架构 B:浏览器 想体验服务的时候 ...
- C# 23种设计模式
目录 0).简单工厂模式 1).工厂方法模式 2).抽象工厂模式 3).单例模式 4).构建者模式 5).原型模式 6).适配器模式 7).修饰者模式 8).代理模式 9).外观模式 10).桥接模式 ...