From f1f82b31db6af3c31d7784713d1ab1a838069f6b Mon Sep 17 00:00:00 2001 From: Mottie Date: Fri, 12 Apr 2013 17:14:00 -0500 Subject: [PATCH] Added repeatheaders widget --- js/widgets/widget-repeatheaders.js | 50 ++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 js/widgets/widget-repeatheaders.js diff --git a/js/widgets/widget-repeatheaders.js b/js/widgets/widget-repeatheaders.js new file mode 100644 index 00000000..c5513336 --- /dev/null +++ b/js/widgets/widget-repeatheaders.js @@ -0,0 +1,50 @@ +/*! tablesorter repeatHeaders widget - updated 4/12/2013 +* Requires tablesorter v2.8+ and jQuery 1.7+ +* Original by Christian Bach from the example-widgets.html demo +*/ +/*global jQuery: false */ +;(function($){ + "use strict"; + + $.tablesorter.addWidget({ + id: "repeatHeaders", + priority: 10, + options: { + rowsToSkip : 4 + }, + // format is called on init and when a sorting has finished + format: function(table, c, wo) { + var h = '', i, $tr, l, skip; + // cache and collect all TH headers + if (!wo.repeatHeaders) { + h = ''; + $.each(c.headerContent, function(i,t) { + h += '' + t + ''; + }); + // "remove-me" class was added in case the table needs to be updated, the "remove-me" rows will be + // removed prior to the update to prevent including the rows in the update - see "selectorRemove" option + wo.repeatHeaders = h + ''; + } + + // number of rows to skip + skip = wo && wo.rowsToSkip || 4; + + // remove appended headers by classname + c.$table.find("tr.repeated-header").remove(); + $tr = c.$tbodies.find('tr'); + l = $tr.length; + // loop all tr elements and insert a copy of the "headers" + for (i = skip; i < l; i += skip) { + // insert a copy of the table head every X rows + $tr.eq(i).before(wo.repeatHeaders); + } + }, + // this remove function is called when using the refreshWidgets method or when destroying the tablesorter plugin + // this function only applies to tablesorter v2.4+ + remove: function(table, c){ + c.$table.find("tr.repeated-header").remove(); + } + + }); + +})(jQuery);