Sorting a table based on which header cell was clicked

Posted by cf_PhillipSenn on Stack Overflow See other posts from Stack Overflow or by cf_PhillipSenn
Published on 2010-04-22T21:01:32Z Indexed on 2010/04/22 21:03 UTC
Read the original article Hit count: 117

Filed under:

If I have the following:

<table>
<thead>
<tr>
<th><a href="Index.cfm?Sort=0">First</a></th>
<th><a href="Index.cfm?Sort=1">Second</a></th>
<th><a href="Index.cfm?Sort=2">Third</a></th>
</tr>
</thead>
<tbody>
<tr>
<td>A</td>
<td class="num">123</td>
<td>XYZ</td>
</tr>
</tbody>
</table>

Q: How do I sort the table body based upon which table header cell was clicked?

<script>
$('th a').click(function() {
    var $this = $(this).closest('th');
    console.log($this.index());
    return false;
});
</script>

(I made each of the table header cells hyperlinks so that if the user has JavaScript turned off, it will follow the link and be sorted on the server side).

© Stack Overflow or respective owner

Related posts about jQuery