jquery - 使用存储在数组中的替代 JSON 格式创建动态 jstree

标签 jquery json jstree

我可以使用替代 JSON 格式创建 jstree,如下所示:

$('#using_json_2').jstree({ 'core' : {
'data' : [
   { "id" : "ajson1", "parent" : "#", "text" : "Simple root node" },
   { "id" : "ajson2", "parent" : "#", "text" : "Root node 2" },
   { "id" : "ajson3", "parent" : "ajson2", "text" : "Child 1" },
   { "id" : "ajson4", "parent" : "ajson2", "text" : "Child 2" },
]
} });

但它是相当静态的。我希望它是动态的;从某种意义上说,行数可以是可变的,行属性可以从数组中读取。我不想使用 ajax,因为数据已经在数组中可用。

最佳答案

如果你希望你的数据是动态的,你可以使用下面的代码来初始化你的jstree:

$('#jstree').jstree({
    'core': {
        'data': arrayCollection
    }
});

其中arrayCollection是一个包含动态数组的变量。例如,您的 arrayCollection 可能如下所示:

var arrayCollection = [
    {"id": "animal", "parent": "#", "text": "Animals"},
    {"id": "device", "parent": "#", "text": "Devices"},
    {"id": "dog", "parent": "animal", "text": "Dogs"},
    {"id": "lion", "parent": "animal", "text": "Lions"},
    {"id": "mobile", "parent": "device", "text": "Mobile Phones"},
    {"id": "lappy", "parent": "device", "text": "Laptops"},
    {"id": "daburman", "parent": "dog", "text": "Dabur Man", "icon": "/"},
    {"id": "Dalmation", "parent": "dog", "text": "Dalmatian", "icon": "/"},
    {"id": "african", "parent": "lion", "text": "African Lion", "icon": "/"},
    {"id": "indian", "parent": "lion", "text": "Indian Lion", "icon": "/"},
    {"id": "apple", "parent": "mobile", "text": "Apple IPhone 6", "icon": "/"},
    {"id": "samsung", "parent": "mobile", "text": "Samsung Note II", "icon": "/"},
    {"id": "lenevo", "parent": "lappy", "text": "Lenevo", "icon": "/"},
    {"id": "hp", "parent": "lappy", "text": "HP", "icon": "/"}
];

最后,您的 html 文件应如下所示:

<html>
    <head>
        <title>JSTree</title>
        <link rel="stylesheet" href="dist/themes/default/style.css" />
        <script src="dist/libs/jquery.js"></script>
        <script src="dist/jstree.js"></script>
        <script>
            $(function() {
                var arrayCollection = [
                    {"id": "animal", "parent": "#", "text": "Animals"},
                    {"id": "device", "parent": "#", "text": "Devices"},
                    {"id": "dog", "parent": "animal", "text": "Dogs"},
                    {"id": "lion", "parent": "animal", "text": "Lions"},
                    {"id": "mobile", "parent": "device", "text": "Mobile Phones"},
                    {"id": "lappy", "parent": "device", "text": "Laptops"},
                    {"id": "daburman", "parent": "dog", "text": "Dabur Man", "icon": "/"},
                    {"id": "Dalmation", "parent": "dog", "text": "Dalmatian", "icon": "/"},
                    {"id": "african", "parent": "lion", "text": "African Lion", "icon": "/"},
                    {"id": "indian", "parent": "lion", "text": "Indian Lion", "icon": "/"},
                    {"id": "apple", "parent": "mobile", "text": "Apple IPhone 6", "icon": "/"},
                    {"id": "samsung", "parent": "mobile", "text": "Samsung Note II", "icon": "/"},
                    {"id": "lenevo", "parent": "lappy", "text": "Lenevo", "icon": "/"},
                    {"id": "hp", "parent": "lappy", "text": "HP", "icon": "/"}
                ];
                $('#jstree').jstree({
                    'core': {
                        'data': arrayCollection
                    }
                });
            });
        </script>
    </head>
    <body>
        <div id="jstree"></div>
    </body>
</html>

每当您的 arrayCollection 被修改时,您都必须将 arrayCollection 重新分配给您的 jstree 并以编程方式刷新 jstree。

关于jquery - 使用存储在数组中的替代 JSON 格式创建动态 jstree,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26270239/

相关文章:

javascript - 如何在弹出窗口中使用 Codebird PHP 发布推文

java - 如何使用 Jackson 的 ObjectMapper 将以下 JSON 转换为 POJO

javascript - jstree 中 dnd 插件中的拖放事件未被调用

utf-8 中的 python jsonify 字典

c# - 从 json 字符串中提取信息并将其添加到 C# 中的列表

jquery - 如何获取jstree中所选节点的完整路径?(根节点到所选节点)

javascript - 带有外部节点的 jstree 上下文菜单

javascript - 模板化仅采用标题/内容参数的 HTML 代码块的简洁方法,其中内容可能很大且散布着换行符

javascript - 如何使用 JavaScript 选择字符串的最后 4 个字符?

javascript - 带有移动平均线的 HTML5/JS 图表