[英]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. 我在您的代码中看到了几件事。
#childrow-parent0
. #childrow-parent0
。 This is not legal HTML, and can lead to problems with JavaScript. .nextUntil(".parent")
to find all of the "children" of a parent. .nextUntil(".parent")
查找.nextUntil(".parent")
所有“子级”。 .click()
on a parent, and it will toggle as if you clicked it. .click()
,它会像单击它一样进行切换。 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
类。)
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.