Sorting Tables

While it’s nice enough to have a table that’s striped, sometimes you want a site that allows user interaction. Maybe the user wants to be able to sort the columns in a different order—instead of having the years increase, they want them to instead decrease. Or maybe they want to sort by name. Or maybe by name in reverse order. Or... you get the idea.

Here’s the one example where jQuery doesn’t contain this functionality out of the box—so instead, we’ll have to use a plug-in. This one’s called, meaningfully enough, tablesorter

To create sortable tables:

1.
<script type="text/javascript" src="jquery/jquery.tablesorter.js"></script>
Script 16.11, our HTML file, is virtually identical to Script 16.8. There are only two changes: we ...

Get JavaScript and Ajax for the Web: Visual QuickStart Guide, Seventh Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.