欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

EasyUI List转tree数据格式

程序员文章站 2022-05-14 10:35:24
今天在用到EasyUI 的Tree,TreeGrid,每次转出这个数据格式非常不爽,就自己写了段HELPER 输出到前端: JsonConvert.SerializeObject(TreeDataHelper.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 });