单独或一起展开折叠表格
我需要能够通过按下表的某些部分的按钮来单独展开和折叠该部分,但是如果用户想要查看表中的所有数据,他们可以点击表顶部的一个按钮来展开所有信息。
然后,用户应该仍然能够折叠他们不感兴趣的部分,如果他们随后折叠整个表格,则所有单元格都应该再次折叠。
我已经用两种不同的方式对此进行了重击,这已经很接近了,但我的 logix 让我失败了。请看一下,我将不胜感激。
$(function () {
var collapseIcon = 'images/btn-open.gif';
var collapseText = 'Collapse this section';
var expandIcon = 'images/btn-closed.gif';
var expandText = 'Expand this section';
var $tableNum = 0;
$(".openCloseBtn").each(function (i) {
var $section = $(this);
$tableNum = i + 1
$($section).attr('src', collapseIcon)
.attr('alt', collapseText)
.addClass('clickable')
.click(function () {
if ($section.is('.collapsed')) {
$section.removeClass('collapsed');
$(this).attr('src', collapseIcon)
.attr('alt', collapseText);
$('.table' + i).fadeOut("slow");
}
else {
//alert('i = ' + i)
$section.addClass('collapsed');
$('.table' + i).fadeIn("slow");
$(this).attr('src', expandIcon)
.attr('alt', expandText);
}
});
});
$('#ViewAll').click(function () {
$(".openCloseBtn").each(function (i) {
var $section = $(this);
if ($section.is('.collapsed')) {
$section.removeClass('collapsed');
$(this).attr('src', collapseIcon)
.attr('alt', collapseText);
$('.table' + i).fadeOut("slow");
}
else {
//alert('i = ' + i)
$section.addClass('collapsed');
$('.table' + i).fadeIn("slow");
$(this).attr('src', expandIcon)
.attr('alt', expandText);
}
});
});
});
I need to be able to expand and collapse certain sections of a table individually by pressing that section's button but if a user wants to see all the data in the table, they hit one button at the top of the table to expand all the information.
The use should then still be able to collapse sections they are not interested in and if they then collapse the entire table, all cells should then collapse again.
I've given this a bash in two different ways and this is close but my logix fails me. I'd appreciate a look at it please.
$(function () {
var collapseIcon = 'images/btn-open.gif';
var collapseText = 'Collapse this section';
var expandIcon = 'images/btn-closed.gif';
var expandText = 'Expand this section';
var $tableNum = 0;
$(".openCloseBtn").each(function (i) {
var $section = $(this);
$tableNum = i + 1
$($section).attr('src', collapseIcon)
.attr('alt', collapseText)
.addClass('clickable')
.click(function () {
if ($section.is('.collapsed')) {
$section.removeClass('collapsed');
$(this).attr('src', collapseIcon)
.attr('alt', collapseText);
$('.table' + i).fadeOut("slow");
}
else {
//alert('i = ' + i)
$section.addClass('collapsed');
$('.table' + i).fadeIn("slow");
$(this).attr('src', expandIcon)
.attr('alt', expandText);
}
});
});
$('#ViewAll').click(function () {
$(".openCloseBtn").each(function (i) {
var $section = $(this);
if ($section.is('.collapsed')) {
$section.removeClass('collapsed');
$(this).attr('src', collapseIcon)
.attr('alt', collapseText);
$('.table' + i).fadeOut("slow");
}
else {
//alert('i = ' + i)
$section.addClass('collapsed');
$('.table' + i).fadeIn("slow");
$(this).attr('src', expandIcon)
.attr('alt', expandText);
}
});
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
代码是做相反的事情吗?在这里,看起来代码正在对类名执行相反的操作。如果没有
collapsed
类,该行就会淡出。您可以尝试仅使用.toggle
这样 jQuery 就能处理所有事情。Is the code doing the reverse? Right here, it looks like the code is doing the reverse with the classname. The row fades out if there is no
collapsed
class. You can try just using.toggle
so jQuery will handle everything.