展开/折叠jQuery树表

问题描述 投票:2回答:1

我正在使用jQuery的treetable库,并且尝试做类似手风琴类型的行为,因此当我打开其中一个关卡并对其进行扩展时,我想折叠以前打开的另一个关卡。我不知道如何使用jQuery树表库实现此行为。

这是我的树表的代码:

function buildTreeTable(tree){

    $("#example").treetable({
        expandable:     true,
        onNodeExpand:   nodeExpand,
        onNodeCollapse: nodeCollapse
    });

function nodeExpand () {
        getNode(this.id); 
 }


function nodeCollapse () {
         console.log("Collapsed: " + this.id);
    }

function getNode(parentNode){
        id = parentNode;
        console.log("The new var is", id);
        console.log("The id of the parent is: ", parentNode);
        var parentNode = $("#example").treetable("node", parentNode);
        $("#example").treetable("unloadBranch", parentNode);
    console.log("parent node",parentNode);
}

提前感谢!

javascript jquery accordion treetable
1个回答
0
投票

您可以尝试以下方法:

  1. 最初折叠Treegrid表中的所有行。

  2. 然后,在'onExpand'函数中检查前一个节点是否已扩展。如果展开,则折叠该节点。 (注意:我之前的ID已存储在此处的temp变量中。)

现在,它可以正常用作'折叠和手风琴'

$(document).ready(function () {
    var temp = "";
    $('.tree').treegrid({
        'expanderExpandedClass': 'glyphicon glyphicon-minus',
        'expanderCollapsedClass': 'glyphicon glyphicon-plus',
        'initialState': 'collapsed',
        **'onExpand': function () {
            if ($(document.getElementById(temp)).treegrid('isExpanded')) {
                $(document.getElementById(temp)).treegrid('collapse');
            }
            temp = $(this).attr("id");
        }**
    });
});
© www.soinside.com 2019 - 2024. All rights reserved.