【问题标题】:valid json to create a jstree nodes dynamically用于动态创建 jstree 节点的有效 json
【发布时间】:2012-03-30 18:45:28
【问题描述】:

我有一个返回角色列表的方法,我想将这些角色放在 jstree 中,但我不知道如何。

我尝试执行以下操作,但我只是不知道如何为 jstree 制作有效的 json

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 对象的列表。

【问题讨论】:

    标签: jquery json jquery-plugins jstree


    【解决方案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 */ ]
    }
    

    如果您使用 ajax 从服务器获取 json,请确保创建这样的 json 结构

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

    你可以看看这个了解详情

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

    【讨论】:

    • 好的,谢谢!只是我不知道在哪里退货?!在我的 GetProcessRole 方法中???
    • 好的,我认为您的技术正在使用 Web 服务,尝试使用 c#williamsportwebdeveloper.com/cgi/wp/?p=494 在 asp 中使用 json 开发 Web 服务
    • 如果我使用 ajax 获取 json,我们无法添加 attr??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-29
    相关资源
    最近更新 更多