首页 > 代码库 > dapper+linq+json+ztree构建树

dapper+linq+json+ztree构建树

dapper获取集合实体

/// <summary>
        /// 获取表tb_sys_zhuowei全部数据
        /// </summary>
        public IEnumerable<Model_tb_sys_zhuowei> SelectAll_tb_sys_zhuowei()
        {
            using (IDbConnection conn = OpenConnection())
            {
                const string query = @"select * from tb_sys_zhuowei order by addtime desc";
                return conn.Query<Model_tb_sys_zhuowei>(query, null);
            }
        }

linq处理集合,并通过Newtonsoft处理成json

protected string GetTree()
    {
        MyProject.DataOrm dao = new MyProject.DataOrm();
        IEnumerable<MyProject.Model_tb_sys_zhuowei> list = dao.SelectAll_tb_sys_zhuowei();
        var q = from x in list
                orderby x.louceng
                group x by x.louceng into g //按楼层分组
                select new
                {
                    name = g.Key.ToString() + "楼",
                    open = true,
                    children = from x2 in g
                               orderby x2.quyu
                               //where x2.louceng == g.Key
                               group x2 by x2.quyu into k //按区域分组
                               select new
                               {
                                   name = k.Key.Length == 2 ? "包间" : k.Key + "区",
                                   click = "loadzw('" + g.Key.ToString() + "-" + k.Key + "')"
                               }
                    //select new
                    //{
                    //    name = x2.quyu.Length == 2 ? "包间" : x2.quyu + "区",
                    //    click = "loadzw('" + g.Key.ToString() + "-" + x2.quyu + "-" + x2.number.ToString() + "')"
                    //}
                };

        var templist = q.ToList();
        return Newtonsoft.Json.JsonConvert.SerializeObject(templist, Newtonsoft.Json.Formatting.Indented);
    }
[{
    "name": "1楼",
    "open": true,
    "children": [{
        "name": "A区",
        "click": "loadzw('1-A')"
    },
    {
        "name": "B区",
        "click": "loadzw('1-B')"
    },
    {
        "name": "包间",
        "click": "loadzw('1-BJ')"
    }]
},
{
    "name": "2楼",
    "open": true,
    "children": [{
        "name": "A区",
        "click": "loadzw('2-A')"
    }]
}]

前端ztree构建树

<script src=http://www.mamicode.com/js/jquery-1.6.min.js" type="text/javascript"></script>>
效果