今天在用到EasyUI 的Tree,TreeGrid,每次轉出這個數據格式非常不爽,就自己寫了段HELPER 輸出到前端: JsonConvert.SerializeObject(TreeDataHelper<T>.GetTreeDataFromList(tList, x1 => x1.Id, x1 ...
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 });