creating dynamic jstree using alternative JSON format stored in array

烂漫一生 提交于 2019-12-17 19:54:26

问题


I am able to create jstree using alternative JSON format as below:

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

But it is quite static. I want it to be dynamic; in the sense that, number of rows could be variable and row properties to be read from an array. I don't want to use ajax as data is already available in array.


回答1:


If you want your data to be dynamic, you can use the following code to initialize your jstree:

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

where arrayCollection is a variable that contians your dynamic array. For Example your arrayCollection may look like this:

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": "/"}
];

Finally, your html file should look like:

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

Whenever your arrayCollection is modified, you have to re-assign the arrayCollection to your jstree and refresh the jstree programmatically.




回答2:


var fullMenuList = [
        {"id": "1000", "parent": "#", "text": "PARENT"}
    ];
    $http({
        method: 'POST',
        url: 'Jtree.do',
        params: {}  // pass in data as strings 

    }).then(function (response) {
        console.log(response.data[0].Code1);
        console.log(response.data[0].Menu);
        console.log(response.data[0].Groupcode);

        for (var i = 0; i < response.data.length; i++) {
            fullMenuList.push({id: response.data[i].Code1,
                parent: response.data[i].Groupcode,
                text: response.data[i].Menu
            });


        }

    $('#jstree').jstree({
            "checkbox": {
                "keep_selected_style": false
            },
            // "plugins": ["checkbox"],
            'core': {
                'data': fullMenuList
            }
        });

    });


来源:https://stackoverflow.com/questions/26270239/creating-dynamic-jstree-using-alternative-json-format-stored-in-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!