当前位置: 移动技术网 > IT编程>开发语言>.net > EasyUI List<T>转tree数据格式

EasyUI List<T>转tree数据格式

2018年12月12日  | 移动技术网IT编程  | 我要评论

mpv推荐,液晶显示器维修,修真帝

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 });

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网