Search code examples
jsontwitter-bootstrapdynamictreeview

Adding nodes to bootstrap treeview dynamically


I currently have a huge JSON file (over 15k lines, size might increase) with which I want to construct a bootstrap-treeview. Adding all the nodes would make loading of page really slow, so I plan to create a service to fetch JSON of selected nodes and populate the treeview accordingly. This is what I have right now.

<!DOCTYPE html>
<html>
<head>
    <title>Bootstrap Tree View</title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <link href="./TreeView_files/bootstrap-treeview.css" rel="stylesheet">
</head>
<body>
    <div class="container">
        <h1>Bootstrap Tree View - DOM Tree</h1>
        <br/>
        <div class="row">
            <div class="col-sm-12">
                <label for="treeview"></label>
                <div id="treeview"/>
            </div>
        </div>
    </div>
    <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
    <script src="./TreeView_files/bootstrap-treeview.js"></script>
    <script type="text/javascript">

        function buildDomTree() {
            var tree = [
            {
                text: "Parent 1",
                nodes: [
                {
                    text: "Child 1",
                    nodes: [
                    {
                        text: "Grandchild 1"
                    },
                    {
                        text: "Grandchild 2"
                    }
                    ]
                },
                {
                    text: "Child 2"
                }
                ]
            },
            {
                text: "Parent 2"
            },
            {
                text: "Parent 3"
            },
            {
                text: "Parent 4"
            },
            {
                text: "Parent 5"
            }
            ];
            return tree;
        }

        $(function() {

            var options = {
                bootstrap2: false, 
                showTags: true,
                levels: 5,
                data: buildDomTree()
            };

            $('#treeview').treeview(options);
        });
    </script>
</body>

Treeview can go 4 levels deep, and I want to fetch next level JSON each time a node is clicked. So, if I click on "Parent 5", it should pop out sub nodes. I have no clue how to add nodes dynamically. Any help would be really appreciated.


Solution

  • Answer on request: I have found a way to do it, although, it is a tad inefficient. What I've done is, I keep a state of all expanded nodes, and when I click on a node to expand it, I make an HTTP request, add the new nodes to the old one, redraw the entire tree and re-expand all the previously expanded nodes. I know this is inefficient, but it is the only way I could find without going into the nitty-gritty and essentially recreating the entire tree myself (which is just a glorified recursion application).

    Here's the code I ran with when I posted the question. There is obviously room for improvement.

    var expandedNodes = [];
    var tree = [];
    
    $(function()
    {
        $.post("http://localhost:8000/getLevel1", function( data ) 
        {
            var JSObject = JSON.parse(data);
    
            for (j in JSObject)
                tree.push(JSObject[j]);
    
            createTree();
        });
    });
    
    function createTree(){
    
        var options = {
            bootstrap2: false, 
            showTags: true,
            levels: 0,
            data: tree,
            expandIcon: 'fa fa-chevron-right',
            collapseIcon: 'fa fa-chevron-down',
            onNodeExpanded: nodeExpand,
            onNodeCollapsed: nodeCollapse,
            onNodeSelected: nodeSelect,
            onNodeUnselected: nodeUnselect
        }
        $('#treeview').treeview(options);
        for (node in expandedNodes)
            $('#treeview').treeview('expandNode', [ expandedNodes[node], { levels: 0, silent: true } ]);
        $('#treeview').treeview('expandNode', 0, { silent: true } );
    };
    
    
    function nodeExpand(event, data)
    {
        expandedNodes.push(data.nodeId);
        var requestObject = []
        requestObject.push(data.text);
    
        var parent, dummy = data;
        while ((parent = $('#treeview').treeview('getParent', dummy.nodeId))["nodeId"] != undefined)
            {
            requestObject.push(parent.text);
            dummy = parent;
        }
    
        $.post("http://localhost:8000/getNode?param=" + JSON.stringify(requestObject), function(retVal) 
        {
            var JSObject = JSON.parse(retVal);
            var node = findNode(requestObject);
            node.nodes = JSObject;
            createTree();
        });
    }
    
    function nodeCollapse(event, data)
    {
        var index = expandedNodes.indexOf(data.nodeId);
        if (index > -1) 
            expandedNodes.splice(index, 1);
    }
    
    function nodeSelect(event, data)
    {
        if (data.state.expanded == true)
            $('#treeview').treeview('collapseNode', data.nodeId);
        else
            $('#treeview').treeview('expandNode', data.nodeId);
        //$('#treeview').treeview('unselectNode', [ data.nodeId, { silent: true } ]);
    }
    
    function nodeUnselect(event, data)
    {
    }
    
    function findNode(array)
    {
        var searchIn = tree; //array
        var lastFound = tree;
        for (var i = array.length - 1; i >= 0; i--)
            {
            var obj = searchInObject(searchIn, array[i]);
            searchIn = obj.nodes;
            lastFound = obj;
        }
    
        return lastFound;
    }
    
    function searchInObject(objectArray, string)
    {
        for (var index in objectArray)
            if (objectArray[index].text == string)
                return objectArray[index];
    }
    
    $(document).ready(function () {
        var trigger = $('.hamburger'),
        overlay = $('.overlay'),
        isClosed = false;
        hamburger_cross();
        $('#wrapper').toggleClass('toggled');
    
        trigger.click(function () {
            hamburger_cross();      
        });
    
        function hamburger_cross() {
    
            if (isClosed == true) {          
                overlay.hide();
                trigger.removeClass('is-open');
                trigger.addClass('is-closed');
                isClosed = false;
                $('#open_arrow').removeClass('fa-chevron-circle-left').addClass('fa-chevron-circle-right');
            } else {   
                overlay.show();
                trigger.removeClass('is-closed');
                trigger.addClass('is-open');
                isClosed = true;
                $('#open_arrow').removeClass('fa-chevron-circle-right').addClass('fa-chevron-circle-left');
            }
        }
    
        $('[data-toggle="offcanvas"]').click(function () {
            $('#wrapper').toggleClass('toggled');
    
        });  
    });
    

    Point of interest would be nodeExpand and createTree methods.