如何使用 JSON 初始化 jsTree

发布于 2024-12-12 17:05:52 字数 1752 浏览 6 评论 0原文

我通过这种方式制作 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 并仅使用下面的字符串向用户显示它。

I make jsTree by this way:

$("#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" : []
                }
});

And user sees page with empty jsTree. I must initialize my jsTree when user make some action. But I musn't use ajax initialization (I musn't use "ajax" in "json_data"). I must initialize my jsTree using only string like this:

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": []
    }]
}]
}]'

No matter how I receive this string, when user wants see tree I've already had this string.
And here I get question: How can I initialize jsTree and display it for user using only string below.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

番薯 2024-12-19 17:05:53

这是我的解决方案:

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 来加载模型,该解决方案也将起作用。

来自文档:

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

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

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

Here is my solution:

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);

This solution will work even if we set up AJAX for loading model before.

From documentation:

If both data and ajax are set the initial tree is rendered from the data string.
When opening a closed node (that has no loaded children) an AJAX request is made.

More information is here http://www.jstree.com/documentation/json_data

I decided to use this solution because I must change stringJSON several times and rebuild tree using this changed string (without reloading page).

拔了角的鹿 2024-12-19 17:05:52

也许你想要类似的东西? http://jsfiddle.net/radek/fmn6g/11/

  • 我插入 jsTree 的位置单击按钮。
  • 单击函数的 JavaScript 插入“jstree”div 并且还包含 jsTree 定义。
  • 正如你所看到的,我也使用 json 数据类型。

我的问题中的更多信息点击时显示jsTree

就是这样你在追什么?

Probably you want something like that? http://jsfiddle.net/radek/fmn6g/11/

  • Where I insert jsTree on a button click.
  • The javascript on click function inserts 'jstree' div and also contains jsTree definition.
  • As you can see I also use json data type.

More info in my question display jsTree on click

Is that what you are after?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文