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="../../js/jquery-1.6.min.js" type="text/javascript"></script>
    <script src="../../js/jquery.ztree.core-3.5.min.js" type="text/javascript"></script>
    <script type="text/javascript">
		<!--
        var setting = {};
//        var zNodes = [
//			{ name: "1楼", open: true,
//			    children: [
//					{ name: "A区", click: "loadzw('1-A')" },
//					{ name: "B区", click: "loadzw('1-B')" },
//					{ name: "C区", click: "loadzw('1-C')" }
//				]
//			},
//            { name: "2楼", open: true,
//                children: [
//					{ name: "A区", click: "loadzw('2-A')" },
//					{ name: "B区", click: "loadzw('2-B')" },
//					{ name: "C区", click: "loadzw('2-C')" }
//				]
//            },
//            { name: "3楼", open: true,
//                children: [
//					{ name: "A区", click: "loadzw('3-A')" },
//					{ name: "B区", click: "loadzw('3-B')" },
//					{ name: "C区", click: "loadzw('3-C')" }
//				]
//            }
//		];
        var zNodes = <%=GetTree() %>

        $(document).ready(function () {
            $.fn.zTree.init($("#treeDemo"), setting, zNodes);
        });
		//-->

        function loadzw(kk) {
            alert(kk);
        }
	</script>

效果

dapper+linq+json+ztree构建树_第1张图片



你可能感兴趣的:(ztree,json,LINQ,dapper)