Extjs treePanel 后台Json的两种构建方法
public string json = "";
public string QueryMenuTreeJson(string ParentID, string userId)
{
json = "";
GetResourceTreeJson(ParentID, userId);
return json;
}
public string GetResourceTreeJson(string ParentID, string userId)
{
DataTable dt = new DataTable();
string sql = "select c.Title as 'text',c.ID,case when c.IsLeaf=0 then 'false' else 'true' end as 'leaf',c.Url as 'url'"
+ " from SysUserRole a,SysRoleResource b,SysResource c where a.UserID = " + userId +
" and a.IsDeleted = 0 and b.IsDeleted = 0 and c.IsDeleted = 0 "
+ " and b.RoleID = a.RoleID and b.ResourceID = c.ID and c.ParentID = " + ParentID + " order by c.ID";
using (End.DataCore.IDbAction db = End.DataCore.DbSessionFactory.GetDbSession())
{
dt = db.ExecuteQuery(sql);
}
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt.Rows[i][2].ToString() != "true")
{
json += "{'text':'" + dt.Rows[i][0] + "'";
json += ",'leaf':'false','id':'" + dt.Rows[i][1] + "','children':[";
GetResourceTreeJson(dt.Rows[i][1].ToString(), userId);
json += "]";
}
else
{
json += "{'text':'" + dt.Rows[i][0] + "','leaf':'true','id':'" + dt.Rows[i][1] + "','url':'" + dt.Rows[i][3] + "'";
}
json += "},";
}
return json;
}
上面的是小白自己的写的一种完全手动拼接的Json数据,希望可以给大家帮助。但是有一定的弊端(每次递归都去查询数据库,还有就是拼接存在一定的安全性因素),后来小白就改成了用类封装的方式,然后通过返回List的类型的数据,然后通过json类的帮助,自动将list转换成json数据,而且我上面的代码是每进行一次的递归操作,就去数据库中查询一次,这样涉及到数据库的开关,因此,小白将所有的相关的数据都一次性查了出来,然后将dt 作为参数,递归传入方法中,详情可以参照代码。
List<Model.ResourceNode> NodeList = null; public List<Model.ResourceNode> GetMenuTreeStore(string userId)
{
NodeList = new List<Model.ResourceNode>();
DataTable dt = new DataTable();
List<End.DataCore.ModelBase.Column> list = new List<End.DataCore.ModelBase.Column>();
list.Add(new End.DataCore.ModelBase.Column("@userID", (userId)));
string sql = "select c.Title as 'text',c.ID,case when c.IsLeaf=0 then 'false' else 'true' end as 'leaf',c.Url as 'url',c.ParentID as 'parentId' "
+ " from SysUserRole a,SysRoleResource b,SysResource c where userId = @userID"
+ " and a.IsDeleted = 0 and b.IsDeleted = 0 and c.IsDeleted = 0 "
+ " and b.RoleID = a.RoleID and b.ResourceID = c.ID order by c.ID";
using (End.DataCore.IDbAction db = End.DataCore.DbSessionFactory.GetDbSession())
{
dt = db.ExecuteQuery(sql, list);
}
string sqlSelect = "parentId = 0";
DataTable newTable = dt.Copy();
newTable.Rows.Clear();
DataRow[] rows = dt.Select(sqlSelect);
for (int i = 0; i < rows.Length; i++)
{
newTable.Rows.Add(rows[i].ItemArray);
}
for (int i = 0; i < newTable.Rows.Count; i++)
{
ResourceNode childrenNode = new ResourceNode();
childrenNode.text = newTable.Rows[i][0].ToString();
childrenNode.leaf = newTable.Rows[i][2].ToString();
childrenNode.ID = Convert.ToInt32(newTable.Rows[i][1]);
childrenNode.url = newTable.Rows[i][3].ToString();
GetResourceTreeList(dt, childrenNode);
NodeList.Add(childrenNode);
}
return NodeList;
}下面的为递归法调用的GetResourceTreeList方法,他的优点就在于不需要每次递归都去查询数据库,他是对整个的表格进行过滤筛选,代码如下:
public void GetResourceTreeList(DataTable dt, Model.MenuNode node)
{
string sqlSelect = "parentId = " + node.ID;
DataTable newTable = dt.Copy();
newTable.Rows.Clear();
DataRow[] rows = dt.Select(sqlSelect);
for (int i = 0; i < rows.Length; i++)
{
newTable.Rows.Add(rows[i].ItemArray);
}
node.children = new List<MenuNode>();
for (int i = 0; i < newTable.Rows.Count; i++)
{
if (newTable.Rows[i][2].ToString() != "true")
{
MenuNode resourceNode = new MenuNode();
resourceNode.text = newTable.Rows[i][0].ToString();
resourceNode.leaf = newTable.Rows[i][2].ToString();
resourceNode.ID = Convert.ToInt32(newTable.Rows[i][1]);
GetResourceTreeList(dt, resourceNode);
node.children.Add(resourceNode);
}
else
{
MenuNode childrenNode = new MenuNode();
childrenNode.text = newTable.Rows[i][0].ToString();
childrenNode.leaf = newTable.Rows[i][2].ToString();
childrenNode.ID = Convert.ToInt32(newTable.Rows[i][1]);
childrenNode.url = newTable.Rows[i][3].ToString();
node.children.Add(childrenNode);
}
} }下面的是代码是类的声明。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace OA.Core.Model
{
public class ResourceNode
{
private int Id;
private string Text;
private string Leaf;
private string Url;
private List<ResourceNode> Children; public Int32 ID
{
get
{
return this.Id;
}
set
{
this.Id = value;
}
} public string text
{
get
{
return this.Text;
}
set
{
this.Text = value;
}
} public string leaf
{
get
{
return this.Leaf;
}
set
{
this.Leaf = value;
}
} public string url
{
get
{
return this.Url;
}
set
{
this.Url = value;
}
} public List<ResourceNode> children
{
get
{
return this.Children;
}
set
{
this.Children = value;
}
}
}
}
在递归方法都写好的同时,小白还遇到一个问题,就是直接将获取List转换成的数据传入前台,然后直接调用的时候,发现一个问题,就是数据结构为:Children Text这种形式,然后最终发现竟然在界面上不显示,然后小白通过喊救兵,在@bom wu的引导下,发现Treepanel对数据都严格区分大小写,然后我后来又将类改成上面的样子,这一点大家要注意,而且,如果要加上treePanel Node不是默认的属性,例如Url,可以在Store里面加上Model或者加上fields属性,来解析。
最后,衷心的希望小白的这篇文章能对大家在接触树的时候有帮助!谢谢大家的阅读。
Extjs treePanel 后台Json的两种构建方法的更多相关文章
- jQuery ajax调用后台aspx后台文件的两种常见方法(不是ashx)
在asp.net webForm开发中,用Jquery ajax调用aspx页面的方法常用的有两种:下面我来简单介绍一下. [WebMethod] public static string SayHe ...
- angular2系列教程(十)两种启动方法、两个路由服务、引用类型和单例模式的妙用
今天我们要讲的是ng2的路由系统. 例子
- c#操作json的两种方式
总结一下C#操作json的两种方式,都是将对象和json格式相转. 1.JavaScriptSerializer,继承自System.Web.Script.Serialization private ...
- json的两种表示结构(对象和数组).。
JSON有两种表示结构,对象和数组.对象结构以”{”大括号开始,以”}”大括号结束.中间部分由0或多个以”,”分隔的”key(关键字)/value(值)”对构成,关键字和值之间以”:”分隔,语法结构如 ...
- 两种Ajax方法
两种Ajax方法 Ajax是一种用于快速创建动态网页的技术,他通过在后台与服务器进行少量的数据交换,可以实现网页的异步更新,不需要像传统网页那样重新加载页面也可以做到对网页的某部分作出更新,现在这项技 ...
- Service的两种启动方法
刚才看到一个ppt,介绍service的两种启动方法以及两者之间的区别. startService 和 bindService startService被形容为我行我素,而bindService被形容 ...
- visualvm远程监控jvm两种配置方法
参考:http://blog.itpub.net/17203031/viewspace-765810 一.Jstatd RMI远程监控方法 VisualVM在监控本地JVM的时候是很方便的.只要应用程 ...
- 两种js方法发起微信支付:WeixinJSBridge,wx.chooseWXPay区别
原文链接:https://www.2cto.com/weixin/201507/412752.html 1.为什么会有两种JS方法可以发起微信支付? 当你登陆微信公众号之后,左边有两个菜单栏,一个是微 ...
- git两种合并方法 比较merge和rebase
18:01 2015/11/18git两种合并方法 比较merge和rebase其实很简单,就是合并后每个commit提交的id记录的顺序而已注意:重要的是如果公司用了grrit,grrit不允许用m ...
随机推荐
- ubuntu navicat
接下来是从网络上下载Chrome对应是版本的包,小编的系统是64位的,因此,执行:wget https://dl.google.com/linux/direct/google-chrome-stabl ...
- Qt获取组合键
CTRL+Enter发送信息的实现 在现在的即时聊天程序中,一般都设置有快捷键来实现一些常用的功能,类似QQ可以用CTRL+Enter来实现信息的发送. 在QT4中,所有的事件都继承与QEvent这个 ...
- Makefile使用总结
1. Makefile 简介 Makefile 是和 make 命令一起配合使用的. 很多大型项目的编译都是通过 Makefile 来组织的, 如果没有 Makefile, 那很多项目中各种库和代码之 ...
- android 5.0 (lollipop)源码编译环境搭建(Mac OS X)
硬件环境:MacBook Pro Retina, 13-inch, Late 2013 处理器 2.4 GHz Intel Core i5 内存 8 GB 1600 MHz DDR3 硬盘60G以 ...
- EntityFramework更新数据
1.TryUpdateModel 使用很方便,但实际更新数据的过程还是先select,再update.另外发现一个问题,对于input的type类型file的字段,无法使用TryUpdateModel ...
- [LeetCode]题解(python):103 Binary Tree Zigzag Level Order Traversal
题目来源 https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Given a binary tree, re ...
- [LeetCode]题解(python):092 Reverse Linked List II
题目来源 https://leetcode.com/problems/reverse-linked-list-ii/ Reverse a linked list from position m to ...
- margin负值
一列li并排的时候,需要一些间距的时候,又不需要最右边或者最左边有间距. <!DOCTYPE html> <html lang="zh-CN"> <h ...
- c#导出excel(转)
C#导出Excel文件实例代码 2010-08-03 14:10:36| 分类: 软件编程 | 标签:excel c#导出excel |字号大中小 订阅 /// <summary> ...
- Selenium2学习-012-WebUI自动化实战实例-010-解决元素失效:StaleElementReferenceException: stale element reference: element is not attached to the page document
元素失效的想象提示信息如下图所示,此种问题通常是因为元素页面刷新之后,为重新获取元素导致的. 解决此类问题比较简单,只需要在页面刷新之后,重新获取一下元素,就可以消除此种错误了. 以下以易迅网搜索为例 ...