действительный json для динамического создания узлов jstree - PullRequest
1 голос
/ 14 марта 2012

У меня есть метод, который возвращает список ролей, я хочу поместить эти роли в 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.

1 Ответ

0 голосов
/ 14 марта 2012
{ 
    "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

...