简体   繁体   English

jQuery可折叠列表-如何更新“展开/折叠全部”按钮

[英]Collapsible list with jQuery - How to update Expand/Collapse all button

I've got a list of items which can be expanded/collapsed individually or all at once with an Expand All/Collapse All button. 我有一个项目列表,可以使用“全部展开/全部折叠”按钮将其单独展开或折叠。 All the items start collapsed, but if you manually expand item so that every item is expanded, the 'Expand All' button should change to 'Collapse All'. 所有项目都开始折叠,但是如果您手动扩展项目以使每个项目都展开,则“全部展开”按钮应更改为“全部折叠”。 Similarly if you collapse all the items it should change to 'Expand All'. 同样,如果折叠所有项目,则应更改为“全部展开”。

So every time you click on an individual line, it should check to see if ALL the items have now been collapsed/expanded, and if so, update the Expand/Collapse All button. 因此,每次单击单个行时,它都应检查一下是否所有项目现在都已折叠/展开,如果是,请更新“全部展开/折叠”按钮。

My problem is that I'm not sure how to iterate over all the items on a click to see if they are collapsed or not and properly update. 我的问题是我不确定如何遍历单击的所有项目以查看它们是否折叠并正确更新。

Here is a JSFiddle for this: JSFiddle 这是为此的JSFiddle: JSFiddle

Here is my current code: 这是我当前的代码:

    var expand = true;

jQuery.noConflict();
jQuery(function() {
    jQuery('[id^=parentrow]')
    .attr("title", "Click to expand/collapse")
    .click(function() {
        jQuery(this).siblings('#childrow-' + this.id).toggle();
        jQuery(this).toggleClass("expanded collapsed");
        ExpandCollapseCheck();
    });

    jQuery('[id^=parentrow]').each(function() {
        jQuery(this).siblings('#childrow-' + this.id).hide();

        if (jQuery(this).siblings('#childrow-' + this.id).length == 0)
            jQuery(this).find('.expand-collapse-control').text('\u00A0');
    });

    jQuery('#childrow-' + this.id).hide("slide", { direction: "up" }, 1000).children('td');


});

function CollapseItems() {
    jQuery('[id^=parentrow]').each(function() {
        jQuery(this).siblings('#childrow-' + this.id).hide();

        if (!jQuery(this).hasClass('expanded collapsed'))
            jQuery(this).addClass("expanded collapsed");
    });
}

function ExpandItems() {
    jQuery('[id^=parentrow]').each(function() {
        jQuery(this).siblings('#childrow-' + this.id).show();

        if (jQuery(this).hasClass('expanded collapsed'))
            jQuery(this).removeClass("expanded collapsed");

    });
}

function ExpandCollapseChildren() {

    if (!expand) {
        CollapseItems();
        jQuery('.expander').html('Expand All');
    }
    else {
        ExpandItems();
        jQuery('.expander').html('Collapse All');
    }

    expand = !expand;
    return false;
}

    function ExpandCollapseCheck() {
    if ((jQuery('[id^=parentrow]').hasClass('expanded collapsed')) && (expand)) {
        jQuery('.expander').html('Expand All');
        CollapseItems();
        expand = !expand;
    }
    else if ((!jQuery('[id^=parentrow]').hasClass('expanded collapsed')) && (!expand)) {
        jQuery('.expander').html('Collapse All');
        ExpandItems();
        expand = !expand;
    }
}    

Don't bother iterating, just use a selector to get a count of all the elements & their classes: 不必打扰,只需使用选择器即可获得所有元素及其类的计数:

var $all = jQuery('selector to return all lines');
if($all.length == $all.filter('.collapsed').length)
  //all the rows are collapsed
if($all.end().length == $all.filter('.expanded').length)
  //all the rows are expanded

A couple of things I see with your code. 我在您的代码中看到了几件事。

  1. It seems that you may have multiple children with the same ID, such as #childrow-parent0 . 看来您可能有多个具有相同ID的孩子,例如#childrow-parent0 This is not legal HTML, and can lead to problems with JavaScript. 这不是合法的HTML,可能会导致JavaScript问题。 Use classes instead. 改用类。
  2. Manipulating ID's to find children is more difficult than using built-in jQuery selectors to find children. 与使用内置的jQuery选择器来查找子代相比,操纵ID来查找子代更加困难。 I realize that in this case, they are siblings rather than true children, but you can still use .nextUntil(".parent") to find all of the "children" of a parent. 我意识到在这种情况下,它们是同级而不是真正的子级,但是您仍然可以使用.nextUntil(".parent")查找.nextUntil(".parent")所有“子级”。
  3. Use your click handlers to do the expanding/collapsing instead of repeating code. 使用点击处理程序进行扩展/折叠,而不是重复代码。 One you have a click handler, you can call .click() on a parent, and it will toggle as if you clicked it. 有一个单击处理程序,您可以在父级上调用.click() ,它会像单击它一样进行切换。
  4. If half of your elements are collapsed, do you want "Expand All" or "Collapse All"? 如果您的元素中有一半是折叠的,您要“全部展开”还是“全部折叠”? You might want both. 您可能两者都想要。

With all of that in mind, I wrote your code with a lot less lines. 考虑到所有这些,我用更少的代码编写了您的代码。 To answer your specific question, I just compared the number of '.parent.expanded' elements to the number of '.parent' elements to see if they were all expanded or not. 为了回答您的特定问题,我只是将'.parent.expanded'元素的数量与'.parent'元素的数量进行了比较,以查看它们是否全部被扩展。 (I changed to using a single .parent class.) (我改为使用单个.parent类。)

Demo 演示版

The relevant code to your question: 您问题的相关代码:

$('#expand_all').toggleClass("disabled", $('.parent.expanded').length == $('.parent').length);
$('#collapse_all').toggleClass("disabled", $('.parent.collapsed').length == $('.parent').length);

This uses toggleClass() , with the second argument returning true/false depending on the number of collapsed/expanded parents. 它使用toggleClass() ,第二个参数根据折叠/展开的父级数目返回true / false。 This is used by toggleClass to determine whether the disabled class is applied. toggleClass使用它来确定是否应用了disabled类。

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

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