valid json to create a jstree nodes dynamically

喜欢而已 提交于 2019-12-08 09:37:45

问题


I have a method that returns a list of roles, I want to put those roles in the jstree but I don't know how.

I tried to do the following but I just don't know how to make a valid json for 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"));
        });
    }

on the TreeLoader.aspx page load i have:

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

        {
            GetProcessRoles();
        }

    }

GetProcessRoles is my method that returns a list of ProcessRole object.


回答1:


{ 
    "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 */ ]
}

if you are using ajax to get json from server make sure you create json structure like this

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

you can see this for details

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



来源:https://stackoverflow.com/questions/9699749/valid-json-to-create-a-jstree-nodes-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!