Main Page

text values

<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>
This HTML code sorts both columns by using strings; therefore, the third argument isn’t necessary when
calling
sortTable()
. Even though the first column contains images, the code uses the value attribute to
sort it in both ascending and descending order.
Summary
This chapter explored using JavaScript to move more server-based functionality onto the client: sorting
HTML tables. You learned about using comparison functions along with the
Array
’s
sort()
method to
define custom sort order. With this knowledge, you went on to sort columns with string values in
ascending order.
Next, you learned how to sort in descending order by using the
reverse()
method of the
Array.
Then,
columns with different values were introduced for sorting as well. After you wrote a small conversion
function, you were able to sort integers, floats, and dates in table columns. Lastly, you learned how to
account for table cells that contained HTML instead of simple text values.
Along the way, you learned about closures in JavaScript, which allow generated functions to contain ref-
erences to variables that are seemingly out of scope. Using closures, you saw how the seemingly limited
definition of comparison functions could be extended to allow the usage of additional data to determine
which of two values should occur first.
385
Sorting Tables
15_579088 ch12.qxd 3/28/05 11:40 AM Page 385


JavaScript EditorFree JavaScript Editor     Ajax Editor


©