2012-03-14 42 views
1

我有一个方法返回一个角色列表,我想把这些角色放在jstree中,但我不知道如何。有效的json动态创建一个jstree节点

我试着做以下,但我只是不知道如何做一个有效的JSON用于jstree

function createNodeList() { 
     $('#processRoleTree').jstree({ 
      "json_data": { 

       "ajax": { 
        "type": "POST", 
        "url": "/TreeLoader.aspx?Action=GetProcessRoles", 
        "dataType": "json", 
        "data": function (n) { return { id: n.attr ? n.attr("id") : 0} } 

       } 

      }, 
      "plugins": ["json_data", "themes", "ui"] 


     }).bind("select_node.jstree", function (e, data) { 
      var selectedObj = data.rslt.obj; 
      alert(selectedObj.attr("id")); 
     }); 
    } 

在TreeLoader.aspx页面加载我有:

protected void Page_Load(object sender, EventArgs e) 
    { 
     if (Request.QueryString["Action"].Equals("GetProcessRoles")) 

     { 
      GetProcessRoles(); 
     } 

    } 

GetProcessRoles是我的方法,返回一个ProcessRole对象的列表。

回答

0
{ 
    "data" : "node_title", 
    // omit `attr` if not needed; the `attr` object gets passed to the jQuery `attr` function 
    "attr" : { "id" : "node_identificator", "some-other-attribute" : "attribute_value" }, 
    // `state` and `children` are only used for NON-leaf nodes 
    "state" : "closed", // or "open", defaults to "closed" 
    "children" : [ /* an array of child nodes objects */ ] 
} 

如果您正在使用AJAX来从服务器获取JSON确保你创建JSON结构这样

[ 
    { "data" : "A node", "children" : [ { "data" : "Only child", "state" : "closed" } ], "state" : "open" }, 
    "Ajax node" 
] 

你可以看到本作的细节

http://www.jstree.com/documentation/json_data

+0

好的,谢谢你!只是我不知道该回到哪里?!在我的GetProcessRole方法? – HRI 2012-03-14 10:29:53

+0

好的,我认为你的技术使用的是web服务,试着用asp开发web服务,用c#http://williamsportwebdeveloper.com/cgi/wp/?p=494 – viyancs 2012-03-14 10:35:35

+0

我们不能添加attr,使用Ajax来获取JSON? – HRI 2012-03-14 11:59:26