码迷,mamicode.com
首页 > 移动开发 > 详细

dapper+linq+json+ztree构建树

时间:2015-07-12 20:22:06      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:

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构建树

标签:

原文地址:http://www.cnblogs.com/bhlsheji/p/4641428.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!