简体   繁体   English

展开/折叠 jQuery 树表

[英]Expand/Collapse jQuery treetable

I am using the treetable library from jQuery and I am trying to do an accordion type-like behaviour, such that when I open one of the levels and it expands, I want to collapse the other level that was previously opened.我正在使用来自 jQuery 的 treetable 库,并且我正在尝试执行类似手风琴类型的行为,这样当我打开其中一个级别并展开时,我想折叠之前打开的另一个级别。 I do not know how to implement this behaviour using the jQuery treetable library.我不知道如何使用 jQuery 树表库来实现这种行为。

This is the code for my treetable:这是我的树表的代码:

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);
}

Thanks in advance!提前致谢!

You can try this:你可以试试这个:

  1. Initially collapse all the rows in Treegrid tables.最初折叠 Treegrid 表中的所有行。

  2. Then, in 'onExpand' function check whether the previous node is expanded.然后,在 'onExpand' 函数中检查前一个节点是否被扩展。 If it is expanded collapse that node.如果它展开折叠该节点。 (Note: I have stored the previous id in temp variable here.) (注意:我已经将之前的 id 存储在临时变量中。)

Now, it works fine as a normal 'collapse & accordian'现在,它可以像正常的“折叠和手风琴”一样正常工作

$(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");
        }**
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM