Skip to content

Instantly share code, notes, and snippets.

@darklight721
Last active August 29, 2015 14:10
Show Gist options
  • Save darklight721/c75bc359cc8a44093531 to your computer and use it in GitHub Desktop.
Save darklight721/c75bc359cc8a44093531 to your computer and use it in GitHub Desktop.
A jQuery tablesorter plugin
/*
* jquery.tablesorter.js - a small table sorter jquery plugin
* Usage:
* $('table').tablesorter();
* Options:
* $('table').tablesorter({
* defaultSort: [0, 1] // initial sort - [<column index>, <1 (ascending) || -1 (descending)>]
* });
* Action Triggers:
* $('table').trigger('update'); // to update the table when there are changes like addition/removal of <tr>/<td>
* $('table').trigger('sort', [0, 1]); // to manually sort a column, param is same as defaultSort
* $('table').trigger('destroy'); // to remove and clean up
*/
!function($) {
var TableSorter = function(element, options) {
this.init(element, options);
};
TableSorter.prototype = {
init: function(element, options) {
this.$el = $(element);
this.setOptions(options);
this.attachEventHandlers();
this.update();
},
setOptions: function(options) {
this.currentSort = null;
if (options && typeof options === 'object') {
if (options.defaultSort instanceof Array) {
this.currentSort = options.defaultSort;
}
}
},
attachEventHandlers: function() {
this.$el
.on('update.tablesorter', $.proxy(this.update, this))
.on('sort.tablesorter', $.proxy(this.sort, this))
.on('destroy.tablesorter', $.proxy(this.destroy, this))
.on('click.tablesorter', 'thead th', $.proxy(function(evt) {
this.sort($(evt.currentTarget).data('index'));
}, this));
},
update: function() {
var self = this;
this.$th = this.$el.children('thead').find('th');
this.$tbody = this.$el.children('tbody');
this.$th.each(function(index) {
$(this).data('index', index);
});
this.rows = this.$tbody.children('tr').map(function() {
var $el = $(this).clone();
return {
$el: $el,
data: $el.children('td').map(self.extractData).get()
};
}).get();
if (this.currentSort) {
this.sort(this.currentSort[0], this.currentSort[1]);
}
},
extractData: function(el) {
var $el = $(el),
value = $el.data('value') || $el.text().trim(),
numValue = +value;
return isNaN(numValue) ? value : numValue;
},
sort: function(index, sort) {
var $th = this.$th.eq(index);
if ($th.length === 0) return;
sort = !isNaN(sort) ? sort || 1 :
$th.hasClass('sort-up') ? -1 : 1;
this.rows = this.rows.sort(function(a, b) {
var valueA = a.data[index] || 0,
valueB = b.data[index] || 0,
result = valueA > valueB ? 1 :
valueA < valueB ? -1 : 0;
return result * sort;
});
if (this.currentSort) {
this.$th.eq(this.currentSort[0]).removeClass('sort-up sort-down');
}
$th.addClass(sort > 0 ? 'sort-up' : 'sort-down');
this.$tbody.html($.map(this.rows, function(row) { return row.$el }));
this.currentSort = [index, sort];
},
destroy: function() {
this.$el.off('.tablesorter').removeData('tablesorter');
}
};
$.fn.tablesorter = function(options) {
return this.each(function() {
var $this = $(this),
data = $this.data('tablesorter');
if (!data) $this.data('tablesorter', new TableSorter(this, options));
});
};
}(jQuery);
!function(c){var d=function(a,b){this.init(a,b)};d.prototype={init:function(a,b){this.$el=c(a);this.setOptions(b);this.attachEventHandlers();this.update()},setOptions:function(a){this.currentSort=null;a&&"object"===typeof a&&a.defaultSort instanceof Array&&(this.currentSort=a.defaultSort)},attachEventHandlers:function(){this.$el.on("update.tablesorter",c.proxy(this.update,this)).on("sort.tablesorter",c.proxy(this.sort,this)).on("destroy.tablesorter",c.proxy(this.destroy,this)).on("click.tablesorter",
"thead th",c.proxy(function(a){this.sort(c(a.currentTarget).data("index"))},this))},update:function(){var a=this;this.$th=this.$el.children("thead").find("th");this.$tbody=this.$el.children("tbody");this.$th.each(function(a){c(this).data("index",a)});this.rows=this.$tbody.children("tr").map(function(){var b=c(this).clone();return{$el:b,data:b.children("td").map(a.extractData).get()}}).get();this.currentSort&&this.sort(this.currentSort[0],this.currentSort[1])},extractData:function(a){a=c(a);a=a.data("value")||
a.text().trim();var b=+a;return isNaN(b)?a:b},sort:function(a,b){var e=this.$th.eq(a);0!==e.length&&(b=isNaN(b)?e.hasClass("sort-up")?-1:1:b||1,this.rows=this.rows.sort(function(c,e){var d=c.data[a]||0,f=e.data[a]||0;return(d>f?1:d<f?-1:0)*b}),this.currentSort&&this.$th.eq(this.currentSort[0]).removeClass("sort-up sort-down"),e.addClass(0<b?"sort-up":"sort-down"),this.$tbody.html(c.map(this.rows,function(a){return a.$el})),this.currentSort=[a,b])},destroy:function(){this.$el.off(".tablesorter").removeData("tablesorter")}};
c.fn.tablesorter=function(a){return this.each(function(){var b=c(this);b.data("tablesorter")||b.data("tablesorter",new d(this,a))})}}(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment