残缺丶
2015-06-01 07:19
采纳率: 56%
浏览 1.7k

我需要把一张表以easyui树的形式显示,求教

我需要把一张表以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);
        }
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • 自己遍历生成下面类型的数据输出就行

    
    [{
        "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"
        }]
    }]
    
    
    评论
    解决 无用
    打赏 举报
  • 逝_去_的_光_阴 2015-06-01 07:30
     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("]");
    

    试试这个

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题