i have this code that does alternating rows for a html in jquery:
我有一个代码,它在jquery中为html交替行:
function AlternateRowColors() {
$("table.altRow1 tr").filter(function() {
return true;
}).filter(':even').addClass('alt');
$("tr.alt td[rowspan]").each(function() {
$(this).parent().nextAll().slice(0, this.rowSpan - 1).addClass('alt');
});
$('ins').css("background-color", "#cfc")
}
this works great (feel free to add suggestions if anything is inefficient above)
这非常有效(如果上面有任何无效的建议,请随意添加)
the issue that i have now is that i have code that hides a bunch of rows (details on why are not really relevant for this question), the main point is that i want to have a function that can do alternative row colors to current visible rows.
我现在的问题是,我的代码隐藏了一堆行(关于这个问题的详细信息为什么与这个问题无关),重点是我想要一个可以对当前可见行进行替代行颜色的函数。
i am hiding rows by simply adding a class to certain rows and them calling .hide() on that class.
我通过向特定的行添加一个类并调用该类上的.hide()来隐藏行。
is there any suggestion to get alternative row colors (like the above code) but have it work on the visible rows so no matter what is hidden, the table always looks correct in terms of alt row coloring.
是否有什么建议可以获得可选的行颜色(如上面的代码),但是让它对可见的行起作用,因此无论隐藏什么,表在alt行着色方面总是看起来正确。
4 个解决方案
#1
8
i wound up using this which seemed to work:
我最后用了这个,似乎有用:
function UpdateTable() {
$('table.altRow1 tr:visible').removeClass('odd').filter(':odd').addClass('odd');
with this css:
这个css:
.altRow1 tr {
background-color: #FFFFFF;
}
.altRow1 tr.odd {
background-color: #DEDEDE;
}
#2
1
I would suggest that you add a class to the visible rows as well in the code that you have to add the class to hidden rows. Say you add the class named visible. Then you can apply your alternating rows code above to the class visible:
我建议您将类添加到可见行的代码中,您必须将类添加到隐藏的行中。假设您添加了一个名为visible的类。然后您可以将上面的交替行代码应用到可见的类:
$("table.altRow1 tr.visible").filter(function() {
and so on.
等等。
#3
1
You might want to call this function again when you are hiding rows, so that it can re-calculate odd and even rows. You can use the not-selector on your tablerows to get only the visible rows, like this:
在隐藏行时,您可能希望再次调用这个函数,以便它可以重新计算奇数行和偶数行。您可以使用列表上的not-selector来获取可见的行,比如:
$('table.altRow1 tr:not(.hidden)')
#4
-1
Chain the selection:
链的选择:
$('table tr:visible:even').addClass('alt');
#1
8
i wound up using this which seemed to work:
我最后用了这个,似乎有用:
function UpdateTable() {
$('table.altRow1 tr:visible').removeClass('odd').filter(':odd').addClass('odd');
with this css:
这个css:
.altRow1 tr {
background-color: #FFFFFF;
}
.altRow1 tr.odd {
background-color: #DEDEDE;
}
#2
1
I would suggest that you add a class to the visible rows as well in the code that you have to add the class to hidden rows. Say you add the class named visible. Then you can apply your alternating rows code above to the class visible:
我建议您将类添加到可见行的代码中,您必须将类添加到隐藏的行中。假设您添加了一个名为visible的类。然后您可以将上面的交替行代码应用到可见的类:
$("table.altRow1 tr.visible").filter(function() {
and so on.
等等。
#3
1
You might want to call this function again when you are hiding rows, so that it can re-calculate odd and even rows. You can use the not-selector on your tablerows to get only the visible rows, like this:
在隐藏行时,您可能希望再次调用这个函数,以便它可以重新计算奇数行和偶数行。您可以使用列表上的not-selector来获取可见的行,比如:
$('table.altRow1 tr:not(.hidden)')
#4
-1
Chain the selection:
链的选择:
$('table tr:visible:even').addClass('alt');