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 ...
随机推荐
- PyQt5嵌入matplotlib动画
# -*- coding: utf-8 -*- import sys from PyQt5 import QtWidgets import numpy as np from matplotlib.ba ...
- JAVA线程及简单同步实现的原理解析
线程 一.内容简介: 本文主要讲述计算机中有关线程的相关内容,以及JAVA中关于线程的基础知识点,为以后的深入学习做铺垫.如果你已经是高手了,那么这篇文章并不适合你. 二.随笔正文: 1.计算机系统组 ...
- 图解:HTTP 范围请求,助力断点续传、多线程下载的核心原理
题图:by Charles Loyer 一.序 Hi,大家好,我是承香墨影! HTTP 协议在网络知识中占据了重要的地位,HTTP 协议最基础的就是请求和响应的报文,而报文又是由报文头(Header) ...
- NIO(生活篇)
今晚是个下雨天,写完今天最后一行代码,小鲁班起身合上电脑,用滚烫的开水为自己泡制了一桶老坛酸菜牛肉面.这大概是苦逼程序猿给接下来继续奋战的自己最好的馈赠.年轻的程序猿更偏爱坐在窗前,在夜晚中静静的享受 ...
- xamarin开发android收集的一些工具
xamarin开发android收集的一些工具 工欲善其事,必先利其器,从16年下半年开始做xamarin相关的开发,平时使用的一些工具和google插件给大家分享一下,都有下载地址,持续更新. Vi ...
- 更好用的css命名方式——BEM命名
一.什么是BEM? BEM代表块(Block),元素(Element),修饰符(Modifier).无论是什么网站页面,都可以拆解成这三部分. 二.带你认识网页 我们来看一下qq的官网,它可以由三个块 ...
- 使用myeclipse修改项目映射路径
这里主要做下记录,找到myeclipse中对项目路径进行查看以及修改的地址 如图 点击项目右键 实例url:http://localhost:8080/myFirstServlet/one 对应路径中 ...
- php 通过header下载中文文件名 压缩包损坏或文件不存在的问题
开发中大家都是使用的utf8编码,昨天遇到一个奇坑,本是一件很小的问题,解决也浪费了个吧小时.废话不多说,植入正题: 文件下载方式:通过header二进制流文件下载需求: 文件上传保留文件名不变数据字 ...
- RFI to RCE challenge
http://www.zixem.altervista.org/RCE/level1.php 构造payload: https://zixem.altervista.org/RCE/level1.ph ...
- github SSH配置
目录 github SSH配置 前言 ssh 配置 github SSH配置 前言 github有两种更新的渠道,一种是https的,一种是ssh的,其中https每次都要输入密码,非常烦.所以,最好 ...