📜  使用 JavaScript 代码示例创建动态树视图

📅  最后修改于: 2022-03-11 15:02:11.030000             🧑  作者: Mango

代码示例3
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');

    });  
});