展开全部/折叠全部treetable不能正常工作?

时间:2013-10-31 07:06:44

标签: javascript html jquery

我正在尝试将全部展开/折叠全部功能应用于嵌套树表,但我得到了所需的输出。我跟着this related question一样。这是fiddle

<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>

SCRIPT


$(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展开但图片未展开并点击折叠整个表格被隐藏。

有人可以帮助我以高效的方式做到这一点吗?

先谢谢。

1 个答案:

答案 0 :(得分: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");
        });