-1

我正在尝试将全部展开/全部折叠功能应用于嵌套树表,但我得到了所需的输出。我同样关注了这个相关的问题。这是小提琴

<a class="myexpand" href="#">Expand</a> | <a class="mycollapse" href="#">Collapse</a>
<table id="mytable">
    <thead>
        <th class="firstCol">Titulo</th>
        <th class="LastCol">Valor</th>
    </thead>
    <tr data-depth="0" class="collapse level0">
        <td><span class="toggle collapse"></span>Item 1</td>
        <td>123</td>
    </tr>
    <tr data-depth="1" class="collapse level1">
        <td><span class="toggle"></span>Item 2</td>
        <td>123</td>
    </tr>
    <tr data-depth="2" class="collapse level2">
        <td><span class="toggle"></span>Item 3</td>
        <td>123</td>
    </tr>
    <tr data-depth="3" class="collapse level3">
        <td>Item x</td>
        <td>Letra</td>
    </tr>
    <tr data-depth="1" class="collapse level1">
        <td><span class="toggle"></span>Item 4</td>
        <td>123</td>
    </tr>
    <tr data-depth="2" class="collapse level2">
        <td>Item 4x</td>
        <td>123</td>
    </tr>
    <tr data-depth="0" class="collapse collapsable level0">
        <td><span class="toggle collapse"></span>Item 5</td>
        <td>123</td>
    </tr>
    <tr data-depth="1" class="collapse collapsable level1">
        <td>Item 6</td>
        <td>123</td>
    </tr>
</table>

脚本


$(function() {
    $('#mytable').on('click', '.toggle', function () {
        //Gets all <tr>'s  of greater depth
        //below element in the table
        var findChildren = function (tr) {
            var depth = tr.data('depth');
            return tr.nextUntil($('tr').filter(function () {
                return $(this).data('depth') <= depth;
            }));
        };

        var el = $(this);
        var tr = el.closest('tr'); //Get <tr> parent of toggle button
        var children = findChildren(tr);

        //Remove already collapsed nodes from children so that we don't
        //make them visible. 
        //(Confused? Remove this code and close Item 2, close Item 1 
        //then open Item 1 again, then you will understand)
        var subnodes = children.filter('.expand');
        subnodes.each(function () {
            var subnode = $(this);
            var subnodeChildren = findChildren(subnode);
            children = children.not(subnodeChildren);
        });

        //Change icon and hide/show children
        if (tr.hasClass('collapse')) {
            tr.removeClass('collapse').addClass('expand');
            children.hide();
        } else {
            tr.removeClass('expand').addClass('collapse');
            children.show();
        }
        return children;
    });
   $('.toggle').trigger('click');

   $(".myexpand").click(function () {
            $("#mytable tr").show("slow");
   });
    $(".mycollapse").click(function () {
            $("#mytable tr").hide("fast");
   });


});

问题是当我单击展开所有 tr 展开但图像未展开并单击折叠整个表时被隐藏。

有人可以帮助我如何有效地做到这一点吗?

提前致谢。

4

1 回答 1

1

像下面这样使用......你在thead中缺少tr标签

<thead>
        <tr>
            <th class="firstCol">Titulo</th>
            <th class="LastCol">Valor</th>
        </tr>
    </thead>

并用我的功能替换您的功能

 $(".mycollapse").click(function () {
            $("#mytable tr").hide("fast");
            $("#mytable thead tr").show("fast");
            $("#mytable tr.level0").show("fast");
        });
于 2013-11-06T08:14:51.340 回答