↑
Main Page
Modifying the code
</tr>
<tr>
<td value=”xls”><img src=”images/excelicon.gif”/></td>
<td>Spring Budget.xls</td>
</tr>
<tr>
<td value=”doc”><img src=”images/wordicon.gif”/></td>
<td>Job Description - Web Designer.doc</td>
</tr>
<tr>
<td value=”pdf”><img src=”images/acrobaticon.gif”/></td>
<td>Saved Web Page.pdf</td>
</tr>
<tr>
<td value=”doc”><img src=”images/wordicon.gif”/></td>
<td>Chapter 1.doc</td>
</tr>
</tbody>
</table>
However, this new attribute alone doesn’t solve the problem. You must also update the JavaScript code
to take advantage of the
value
attribute.
Modifying the code
This final modification to the code determines whether to get the sortable value from the
<td/>
element’s
text or from the
value
attribute. Here’s the updated code:
function generateCompareTRs(iCol, sDataType) {
return function compareTRs(oTR1, oTR2) {
var vValue1, vValue2;
if (oTR1.cells[iCol].getAttribute(“value”)) {
vValue1 = convert(oTR1.cells[iCol].getAttribute(“value”),
sDataType);
vValue2 = convert(oTR2.cells[iCol].getAttribute(“value”),
sDataType);
} else {
vValue1 = convert(oTR1.cells[iCol].firstChild.nodeValue,
sDataType);
vValue2 = convert(oTR2.cells[iCol].firstChild.nodeValue,
sDataType);
}
As mentioned earlier in the book, adding custom attributes to HTML tags is not
allowed using the strict representation of XHTML. You may alternately want to pro-
vide the value for a table cell using the
title
attribute (if the value will make sense
to the user) or by providing an invisible
<div/>
inside the table cell that contains
the value.
383
Sorting Tables
15_579088 ch12.qxd 3/28/05 11:40 AM Page 383
Free JavaScript Editor
Ajax Editor
©
→