我需要把一张表以easyui树的形式显示,这张表有parentID,看下图
前台我是这样写的:
jQuery(function() {
$('#tree').tree({
url: '../Handlers/ECTree.ashx',
method: 'get',
animate: true,
loadFilter: function (data) {
alert(data);
if (data.d) {
return data.d;
} else {
return data;
}
}
});
});
后台一般处理文件是这样写的,我已拿到该表的实体对象,需要拼接成Tree能识别的json格式的数据,该怎么写呢?
public void ProcessRequest(HttpContext context)
{
StringBuilder stringBuilder = new StringBuilder();
IList<EC> ec = ECBLL.GetList();
foreach (EC row in ec)
{
stringBuilder.Append("[{");
stringBuilder.Append("\"id\":\"" + row.ECID+"\",");
stringBuilder.Append("\"text\":\"" + row.ECName+"\",");
stringBuilder.Append("\"children\":\"" +row.ParentID);
stringBuilder.Append("}]");
}
JavaScriptSerializer jss = new JavaScriptSerializer();
string jsons = jss.Serialize(stringBuilder);
context.Response.Write(jsons);
}
自己遍历生成下面类型的数据输出就行
[{
"id":1,
"text":"My Documents",
"children":[{
"id":11,
"text":"Photos",
"state":"closed",
"children":[{
"id":111,
"text":"Friend"
},{
"id":112,
"text":"Wife"
},{
"id":113,
"text":"Company"
}]
},{
"id":12,
"text":"Program Files",
"children":[{
"id":121,
"text":"Intel"
},{
"id":122,
"text":"Java",
"attributes":{
"p1":"Custom Attribute1",
"p2":"Custom Attribute2"
}
},{
"id":123,
"text":"Microsoft Office"
},{
"id":124,
"text":"Games",
"checked":true
}]
},{
"id":13,
"text":"index.html"
},{
"id":14,
"text":"about.html"
},{
"id":15,
"text":"welcome.html"
}]
}]
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("[");
foreach (EC row in ec)
{
if(sb.length()!=1){
sb.append(",");
}
stringBuilder.Append("{");
stringBuilder.Append("\"id\":\"" + row.ECID+"\",");
stringBuilder.Append("\"text\":\"" + row.ECName+"\",");
stringBuilder.Append("\"children\":\"" +row.ParentID);
stringBuilder.Append("}");
}
stringBuilder.Append("]");
试试这个