|
jQuery Advanced Row Striping |
Learning jQuery - Advanced Row Striping
As we saw earlier in the chapter, row striping can be as simple as two lines of code to alternate the background color:
$(document).ready(function() { $('table.sortable tbody tr:odd').addClass('odd'); $('table.sortable tbody tr:even').addClass('even'); });
%24%28document%29.ready%28function%28%29%20%7B%20%0A%0D%0A%24%28%27table.sortable%20tbody%20tr%3Aodd%27%29.addClass%28%27odd%27%29%3B%20%0A%0D%0A%24%28%27table.sortable%20tbody%20tr%3Aeven%27%29.addClass%28%27even%27%29%3B%20%0A%0D%0A%7D%29%3B%20
If we declare background colors for the odd and even classes as follows, we can see the rows in alternating shades of gray:
tr.even { background-color: #eee; } tr.odd { background-color: #ddd; }
tr.even%20%7B%20%0A%0D%0Abackground-color%3A%20%23eee%3B%20%0A%0D%0A%7D%20%0A%0D%0Atr.odd%20%7B%20%0A%0D%0Abackground-color%3A%20%23ddd%3B%20%0A%0D%0A%7D
While this code works fine for simple table structures, if we introduce non-standard rows into the table, such as sub-headings, the basic odd-even pattern no longer suffices. For example, suppose we have a table of news items grouped by year, with columns for date, headline, author, and topic. One way to express this information is to wrap each year's news items in a
|
element and use
|
for the subheading. Such a table's HTML (in abridged form) would look like this:
-
<table class="striped">
<thead>
<tr>
<th>Date</th>
<th>Headline</th>
<th>Author</th>
<th class="filter-column">Topic</th>
</tr>
</thead>
<tbody>
<tr>
<th colspan="4">2007</th>
</tr>
<tr>
<td>Mar 11</td>
<td>SXSWi jQuery Meetup</td>
<td>John Resig</td>
<td>conference</td>
</tr>
<tr>
<td>Feb 28</td>
<td>jQuery 1.1.2</td>
<td>John Resig</td>
<td>release</td>
</tr>
<tr>
<td>Feb 21</td>
<td>jQuery is OpenAjax Compliant</td>
<td>John Resig</td>
<td>standards</td>
</tr>
<tr>
<td>Feb 20</td>
<td>jQuery and Jack Slocum's Ext</td>
<td>John Resig</td>
<td>third-party</td>
</tr>
</tbody>
<tbody>
<tr>
<th colspan="4">2006</th>
</tr>
<tr>
<td>Dec 27</td>
<td>The Path to 1.1</td>
<td>John Resig</td>
<td>source</td>
</tr>
<tr>
<td>Dec 18</td>
<td>Meet The People Behind jQuery</td>
<td>John Resig</td>
<td>announcement</td>
</tr>
<tr>
<td>Dec 13</td>
<td>Helping you understand jQuery</td>
<td>John Resig</td>
<td>tutorial</td>
</tr>
</tbody>
<tbody>
<tr>
<th colspan="4">2005</th>
</tr>
<tr>
<td>Dec 17</td>
<td>JSON and RSS</td>
<td>John Resig</td>
<td>miscellaneous</td>
</tr>
</tbody>
</table>
%0D%0A%3Ctable%20class%3D%22striped%22%3E%0D%0A%20%20%20%20%3Cthead%3E%0D%0A%20%20%20%20%20%20%20%20%3Ctr%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Cth%3EDate%3C%2Fth%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Cth%3EHeadline%3C%2Fth%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Cth%3EAuthor%3C%2Fth%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Cth%20class%3D%22filter-column%22%3ETopic%3C%2Fth%3E%0D%0A%20%20%20%20%20%20%20%20%3C%2Ftr%3E%0D%0A%20%20%20%20%3C%2Fthead%3E%0D%0A%20%20%20%20%3Ctbody%3E%0D%0A%20%20%20%20%20%20%20%20%3Ctr%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Cth%20colspan%3D%224%22%3E2007%3C%2Fth%3E%0D%0A%20%20%20%20%20%20%20%20%3C%2Ftr%3E%0D%0A%20%20%20%20%20%20%20%20%3Ctr%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctd%3EMar%2011%3C%2Ftd%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctd%3ESXSWi%20jQuery%20Meetup%3C%2Ftd%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctd%3EJohn%20Resig%3C%2Ftd%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctd%3Econference%3C%2Ftd%3E%0D%0A%20%20%20%20%20%20%20%20%3C%2Ftr%3E%0D%0A%20%20%20%20%20%20%20%20%3Ctr%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctd%3EFeb%2028%3C%2Ftd%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctd%3EjQuery%201.1.2%3C%2Ftd%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctd%3EJohn%20Resig%3C%2Ftd%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctd%3Erelease%3C%2Ftd%3E%0D%0A%20%20%20%20%20%20%20%20%3C%2Ftr%3E%0D%0A%20%20%20%20%20%20%20%20%3Ctr%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctd%3EFeb%2021%3C%2Ftd%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctd%3EjQuery%20is%20OpenAjax%20Compliant%3C%2Ftd%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctd%3EJohn%20Resig%3C%2Ftd%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctd%3Estandards%3C%2Ftd%3E%0D%0A%20%20%20%20%20%20%20%20%3C%2Ftr%3E%0D%0A%20%20%20%20%20%20%20%20%3Ctr%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctd%3EFeb%2020%3C%2Ftd%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctd%3EjQuery%20and%20Jack%20Slocum%27s%20Ext%3C%2Ftd%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctd%3EJohn%20Resig%3C%2Ftd%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctd%3Ethird-party%3C%2Ftd%3E%0D%0A%20%20%20%20%20%20%20%20%3C%2Ftr%3E%0D%0A%20%20%20%20%3C%2Ftbody%3E%0D%0A%20%20%20%20%3Ctbody%3E%0D%0A%20%20%20%20%20%20%20%20%3Ctr%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Cth%20colspan%3D%224%22%3E2006%3C%2Fth%3E%0D%0A%20%20%20%20%20%20%20%20%3C%2Ftr%3E%0D%0A%20%20%20%20%20%20%20%20%3Ctr%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctd%3EDec%2027%3C%2Ftd%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctd%3EThe%20Path%20to%201.1%3C%2Ftd%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctd%3EJohn%20Resig%3C%2Ftd%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctd%3Esource%3C%2Ftd%3E%0D%0A%20%20%20%20%20%20%20%20%3C%2Ftr%3E%0D%0A%20%20%20%20%20%20%20%20%3Ctr%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctd%3EDec%2018%3C%2Ftd%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctd%3EMeet%20The%20People%20Behind%20jQuery%3C%2Ftd%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctd%3EJohn%20Resig%3C%2Ftd%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctd%3Eannouncement%3C%2Ftd%3E%0D%0A%20%20%20%20%20%20%20%20%3C%2Ftr%3E%0D%0A%20%20%20%20%20%20%20%20%3Ctr%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctd%3EDec%2013%3C%2Ftd%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctd%3EHelping%20you%20understand%20jQuery%3C%2Ftd%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctd%3EJohn%20Resig%3C%2Ftd%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctd%3Etutorial%3C%2Ftd%3E%0D%0A%20%20%20%20%20%20%20%20%3C%2Ftr%3E%0D%0A%20%20%20%20%3C%2Ftbody%3E%0D%0A%20%20%20%20%3Ctbody%3E%0D%0A%20%20%20%20%20%20%20%20%3Ctr%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Cth%20colspan%3D%224%22%3E2005%3C%2Fth%3E%0D%0A%20%20%20%20%20%20%20%20%3C%2Ftr%3E%0D%0A%20%20%20%20%20%20%20%20%3Ctr%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctd%3EDec%2017%3C%2Ftd%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctd%3EJSON%20and%20RSS%3C%2Ftd%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctd%3EJohn%20Resig%3C%2Ftd%3E%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3Ctd%3Emiscellaneous%3C%2Ftd%3E%0D%0A%20%20%20%20%20%20%20%20%3C%2Ftr%3E%0D%0A%20%20%20%20%3C%2Ftbody%3E%0D%0A%3C%2Ftable%3E%0D%0A
With separate CSS styles applied to
|
elements within
and
, a snippet of the table might look like this:
To ensure that the alternating gray rows do not override the color of the subheading rows, we need to adjust the selector expression:
$(document).ready(function() {
$('table.striped tbody tr:not([th]):odd').addClass('odd');
$('table.striped tbody tr:not([th]):even').addClass('even');
});
%24%28document%29.ready%28function%28%29%20%7B%20%0A%0D%0A%24%28%27table.striped%20tbody%20tr%3Anot%28%5Bth%5D%29%3Aodd%27%29.addClass%28%27odd%27%29%3B%20%0A%0D%0A%24%28%27table.striped%20tbody%20tr%3Anot%28%5Bth%5D%29%3Aeven%27%29.addClass%28%27even%27%29%3B%20%0A%0D%0A%7D%29%3B
The added selector, :not([th]), removes any table row that contains a
|
from the matched set of elements. Now the table will look like this:

Trackback(0)