首页 > 代码库 > 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>>
效果
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。