asp.net返回json 动态生成jQuery Easyui tree
来源:互联网 发布:步步高录音电话软件 编辑:程序博客网 时间:2024/04/29 20:23
在角色授权管理中用到
效果图
HTML代码
<form id="form1" runat="server">
<div>
<asp:HiddenField ID="HiddenField1" runat="server" />
<ul id="tt" class="easyui-tree" >
</ul>
</form>
<div>
<asp:HiddenField ID="HiddenField1" runat="server" />
<ul id="tt" class="easyui-tree" >
</ul>
</form>
js代码
$('#tt').tree({
url:'Handler2.ashx',
method:'get',
animate:true,
checkbox: true,
onClick: function (node) {
url:'Handler2.ashx',
method:'get',
animate:true,
checkbox: true,
onClick: function (node) {
//添加代码
alert(node.target);
},
formatter: function (node) {
var s = node.text;
if (node.children) {
s += ' <span style=\'color:blue\'>(' + node.children.length + ')</span>';
} else {
if ($('#tree').tree('isLeaf', node.target) && node.checked) {
s = ' <span style=\'color:red\'>' + s + '</span>';
}
}
return s;
}
});
alert(node.target);
},
formatter: function (node) {
var s = node.text;
if (node.children) {
s += ' <span style=\'color:blue\'>(' + node.children.length + ')</span>';
} else {
if ($('#tree').tree('isLeaf', node.target) && node.checked) {
s = ' <span style=\'color:red\'>' + s + '</span>';
}
}
return s;
}
});
Handler2.ashx
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
DataTable dt = new UserInfoManager().getRoleMenu("1");
string strjson = DataTable2Json(dt, "0", new StringBuilder(""));
context.Response.Write(strjson);
}
{
context.Response.ContentType = "text/plain";
DataTable dt = new UserInfoManager().getRoleMenu("1");
string strjson = DataTable2Json(dt, "0", new StringBuilder(""));
context.Response.Write(strjson);
}
/// <summary>
/// 拼接jQuery EasyUi tree接受的json字符串
/// </summary>
/// <param name="dt">查询的结果</param>
/// <param name="parentid">树根的parentid</param>
/// <param name="jsonBuilder">返回的字符串</param>
/// <returns>jQuery EasyUi tree接受的json字符串</returns>
/// 拼接jQuery EasyUi tree接受的json字符串
/// </summary>
/// <param name="dt">查询的结果</param>
/// <param name="parentid">树根的parentid</param>
/// <param name="jsonBuilder">返回的字符串</param>
/// <returns>jQuery EasyUi tree接受的json字符串</returns>
public static string DataTable2Json(DataTable dt, string parentid, StringBuilder jsonBuilder)
{
jsonBuilder.Append("[");
DataView dv = new DataView(dt);
dv.RowFilter = "parentid=" + parentid;
foreach (DataRowView row in dv)
{
jsonBuilder.Append("{");
jsonBuilder.Append("\"id\":");
jsonBuilder.Append("\"");
jsonBuilder.Append(row["id"].ToString());
jsonBuilder.Append("\",");
jsonBuilder.Append("\"checked\":");
//jsonBuilder.Append("\"");
jsonBuilder.Append(row["checked"].ToString());
//jsonBuilder.Append("\",");
jsonBuilder.Append(",");
jsonBuilder.Append("\"text\":");
jsonBuilder.Append("\"");
jsonBuilder.Append(row["text"].ToString());
jsonBuilder.Append("\",");
jsonBuilder.Append("\"attributes\":{");
jsonBuilder.Append("\"url\":");
jsonBuilder.Append("\"");
jsonBuilder.Append(row["url"].ToString());
jsonBuilder.Append("\"");
jsonBuilder.Append("}");
DataView dv2 = new DataView(dt);
dv2.RowFilter = "parentid=" + row["id"].ToString();
DataTable newTable = dv2.ToTable();
if (newTable.Rows.Count > 0)
{
jsonBuilder.Append(",\"children\":");
DataTable2Json(dt, row["id"].ToString(), jsonBuilder);
}
jsonBuilder.Append("},");
}
jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
jsonBuilder.Append("]");
return jsonBuilder.ToString();
}
{
jsonBuilder.Append("[");
DataView dv = new DataView(dt);
dv.RowFilter = "parentid=" + parentid;
foreach (DataRowView row in dv)
{
jsonBuilder.Append("{");
jsonBuilder.Append("\"id\":");
jsonBuilder.Append("\"");
jsonBuilder.Append(row["id"].ToString());
jsonBuilder.Append("\",");
jsonBuilder.Append("\"checked\":");
//jsonBuilder.Append("\"");
jsonBuilder.Append(row["checked"].ToString());
//jsonBuilder.Append("\",");
jsonBuilder.Append(",");
jsonBuilder.Append("\"text\":");
jsonBuilder.Append("\"");
jsonBuilder.Append(row["text"].ToString());
jsonBuilder.Append("\",");
jsonBuilder.Append("\"attributes\":{");
jsonBuilder.Append("\"url\":");
jsonBuilder.Append("\"");
jsonBuilder.Append(row["url"].ToString());
jsonBuilder.Append("\"");
jsonBuilder.Append("}");
DataView dv2 = new DataView(dt);
dv2.RowFilter = "parentid=" + row["id"].ToString();
DataTable newTable = dv2.ToTable();
if (newTable.Rows.Count > 0)
{
jsonBuilder.Append(",\"children\":");
DataTable2Json(dt, row["id"].ToString(), jsonBuilder);
}
jsonBuilder.Append("},");
}
jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
jsonBuilder.Append("]");
return jsonBuilder.ToString();
}
public DataTable getRoleMenu(string roleid)
{
string sql = "select case when a.id=b.id and b.url is not null then 'true' when a.id is null then 'false' else 'false' end as checked, b.* from rolemenu a right join menutab b on a.id=b.id and a.roleid ='" + roleid + "' group by b.parentid,b.id,a.id,b.text,b.url";
return DBHelper.getData(sql);
}
{
string sql = "select case when a.id=b.id and b.url is not null then 'true' when a.id is null then 'false' else 'false' end as checked, b.* from rolemenu a right join menutab b on a.id=b.id and a.roleid ='" + roleid + "' group by b.parentid,b.id,a.id,b.text,b.url";
return DBHelper.getData(sql);
}
- asp.net返回json 动态生成jQuery Easyui tree
- jquery easyui tree动态加载-服务端struts返回json数据
- .net 生成 easyui tree树的json
- 用JQuery 访问JSON动态生成ASP.NET控件
- 用JQuery 访问JSON动态生成ASP.NET控件
- 使用JQuery解析asp.net MVC控制器返回的json数据并用动态生成的表格显示解析的数据
- Json+jquery+asp.net 返回数据库数据
- ASP返回JQuery EasyUI树形要求的JSON格式
- Asp.net返回json数据(配合easyui的使用)
- 服务器返回 json数据 给 easyui tree
- Asp.net生成Extjs-Tree数据格式的json类
- 用json格式返回自定义对象。(jQuery asp.net)
- 用json格式返回自定义对象。(jQuery asp.net)
- jquery访问asp.net 返回的json对象
- 生成符合EasyUI-Tree的JSON数据
- jquery easyui tree后台组装json
- Jquery easyUI datagrid 服务器端动态生成列配置返回到前台显示
- Jquery easyUI datagrid 服务器端动态生成列配置返回到前台显示
- Hibernate中用hql查询部分字段 可解决异常java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to
- delphi copy 字符串部分复制
- 基于Qt的图像采集系统
- 基于MVC设计模式的WEB应用框架研究
- 设计模式(命令模式)
- asp.net返回json 动态生成jQuery Easyui tree
- C++ AMP: 在C++中Lambda表达式
- Android获取手机屏幕分辨率
- matlab 函数零散记录
- iOS学习之UINavigationController详解与使用(一)
- 让VisualSVN可以在客户端修改密码
- 提高工作效率的8个小方法
- java 线程池(ThreadPoolExecutor)
- 解决fatal: unable to connect to github.com问题