【问题标题】:How to initialize jsTree using JSON如何使用 JSON 初始化 jsTree
【发布时间】:2011-10-31 10:15:49
【问题描述】:

我是这样制作jsTree的:

$("#myTree").jstree({
                "plugins": ["themes", "json_data", "ui", "crrm", "dnd"],
                "themes": {
                    "theme": "default",
                    "dots": false,
                    "icons": false,
                    "url": "../../Content/jsTreeThemes/default/style.css"
                },
                "json_data": {
                   "data" : []
                }
});

并且用户看到带有空 jsTree 的页面。当用户进行某些操作时,我必须初始化我的 jsTree。但我不能使用 ajax 初始化(我不能在“json_data”中使用“ajax”)。我必须只使用这样的字符串来初始化我的 jsTree:

var stringJSON = [{
    "attr": {
        "id": "1",
        "rel": "root",
        "mdata": null
    },
    "data": "title": "root_jsTree",
    "icon": null
}, "state": "open",
"children": [{
    "attr": {
        "id": "7",
        "rel": "folder",
        "mdata": null
    },
    "data": {
        "title": "1",
        "icon": null
    },
    "state": "",
    "children": [{
        "attr": {
            "id": "10",
            "rel": "folder",
            "mdata": null
        },
        "data": {
            "title": "leaf",
            "icon": null
        },
        "state": "",
        "children": []
    }]
}, {
    "attr": {
        "id": "8",
        "rel": "folder",
        "mdata": null
    },
    "data": {
        "title": "leaf",
        "icon": null
    },
    "state": "",
    "children": [{
        "attr": {
            "id": "9",
            "rel": "folder",
            "mdata": null
        },
        "data": {
            "title": "leaf",
            "icon": null
        },
        "state": "",
        "children": []
    }]
}]
}]'

无论我如何收到这个字符串,当用户想要查看树时,我已经有了这个字符串。 在这里我得到一个问题:如何初始化 jsTree 并仅使用下面的字符串为用户显示它。

【问题讨论】:

    标签: jquery json jstree


    【解决方案1】:

    可能你想要这样的东西? http://jsfiddle.net/radek/fmn6g/11/

    • 我在单击按钮时插入 jsTree。
    • javascript on click 函数插入'jstree' div 并且还包含jsTree 定义。
    • 如您所见,我也使用 json 数据类型。

    更多信息在我的问题display jsTree on click

    这就是你所追求的吗?

    【讨论】:

      【解决方案2】:

      这是我的解决方案:

      var jsTreeSettings = $("#myTree").jstree("get_settings");
      jsTreeSettings.json_data.data = $.parseJSON(stringJSON);
      $.jstree._reference("myTree")._set_settings(jsTreeSettings);
      
      // Refresh whole our tree (-1 means root of tree)
      $.jstree._reference("myTree").refresh(-1);
      

      即使我们之前设置了 AJAX 来加载模型,这个解决方案也可以工作。

      来自文档:

      如果同时设置了 data 和 ajax,则初始树是从数据字符串呈现的。 当打开一个关闭的节点(没有加载的子节点)时,会发出 AJAX 请求。

      更多信息在这里http://www.jstree.com/documentation/json_data

      我决定使用这个解决方案,因为我必须多次更改stringJSON 并使用这个更改后的字符串重建树(无需重新加载页面)。

      【讨论】:

        猜你喜欢
        • 2013-03-31
        • 1970-01-01
        • 2016-08-06
        • 1970-01-01
        • 2017-11-03
        • 2014-05-18
        • 1970-01-01
        • 2017-03-24
        相关资源
        最近更新 更多