Browse files

Initial commit.

  • Loading branch information...
0 parents commit 60ffb62325efc0b367c568a89f1feb2b8a9e9363 @kylefox committed Mar 7, 2012
Showing with 159 additions and 0 deletions.
  1. +72 −0 index.html
  2. +87 −0 jquery.tablesort.js
72 index.html
@@ -0,0 +1,72 @@
+<html>
+<head>
+ <title></title>
+ <style type="text/css">
+ body {
+ font: normal 18px/24px Georgia, serif;
+ }
+ table {
+ font-size: 1em;
+ border-collapse: collapse;
+ margin: 100px auto;
+ }
+ table, th, td {
+ border: 1px solid #999;
+ padding: 10px 30px 10px 15px;
+ text-align: left;
+ }
+ </style>
+</head>
+<body>
+
+<table>
+ <thead>
+ <tr>
+ <th>Name</th>
+ <th>Band</th>
+ <th>Date of Birth</th>
+ <th>Age</th>
+ </tr>
+ </thead>
+ <tbody>
+ <tr>
+ <td>Thom Yorke</td>
+ <td>Radiohead</td>
+ <td>October 7, 1968</td>
+ <td>43</td>
+ </tr>
+ <tr>
+ <td>Justin Vernon</td>
+ <td>Bon Iver</td>
+ <td>April 30, 1981</td>
+ <td>30</td>
+ </tr>
+ <tr>
+ <td>Paul McCartney</td>
+ <td>The Beatles</td>
+ <td>June 18, 1942</td>
+ <td>69</td>
+ </tr>
+ <tr>
+ <td>Sam Beam</td>
+ <td>Iron &amp; Wine</td>
+ <td>July 26, 1974</td>
+ <td>37</td>
+ </tr>
+ </tbody>
+</table>
+
+
+<script src="http://code.jquery.com/jquery-latest.min.js"></script>
+<script src="jquery.tablesort.js"></script>
+<script type="text/javascript">
+
+ var sort;
+
+ $(function() {
+ sort = $('table').tablesort().data('tablesort');
+ });
+</script>
+
+</body>
+</html>
87 jquery.tablesort.js
@@ -0,0 +1,87 @@
+$(function() {
+
+ function bm(f) {
+ var s = new Date(),
+ e;
+ f();
+ e = new Date();
+ return e.getTime() - s.getTime();
+ }
+
+ function debug(msg) {
+ if(console && console.log) {
+ console.log('[tablesort] ' + msg);
+ }
+ }
+
+ function SortableTable($el) {
+ var self = this;
+ this.$el = $el;
+ this.$el.find('thead th').bind('click.tablesort', function() {
+ var time = self.sortBy($(this));
+ debug("Sorted in " + time + 'ms');
+ });
+ this.index = null;
+ this.direction = null;
+ }
+
+ SortableTable.prototype = {
+
+ sortBy: function(th) {
+ var self = this;
+ return bm(function() {
+ self.sort(th.index());
+ });
+ },
+
+ sort: function(column) {
+ var self = this,
+ table = this.$el,
+ rows = table.find('tbody tr'),
+ direction;
+
+ if(rows.length === 0)
+ return;
+
+ this.index = column;
+ this.direction = this.direction === 'asc' ? 'desc' : 'asc';
+ direction = this.direction == 'asc' ? 1 : -1;
+ debug("Sorting by " + this.index + ' ' + this.direction);
+
+ rows.sort(function(a, b) {
+ a = self.sortValueForCell(self.cellToSort(a));
+ b = self.sortValueForCell(self.cellToSort(b));
+ if(a > b) {
+ return 1 * direction;
+ } else if(a < b) {
+ return -1 * direction;
+ } else {
+ return 0;
+ }
+ });
+
+ rows.each(function(i, tr) {
+ table.append(tr);
+ });
+
+ },
+
+ cellToSort: function(row) {
+ return $($(row).find('td').get(this.index));
+ },
+
+ sortValueForCell: function(cell) {
+ return cell.text();
+ }
+
+ };
+
+ $.fn.tablesort = function() {
+ var table, sortable;
+ return this.each(function() {
+ table = $(this);
+ table.data('tablesort', new SortableTable(table));
+ });
+ };
+
+});

0 comments on commit 60ffb62

Please sign in to comment.