Skip to content

Commit

Permalink
Core: Add widgetClass option & fix debug logs for applying widgets. F…
Browse files Browse the repository at this point in the history
…ixes #743
  • Loading branch information
Mottie committed Oct 11, 2014
1 parent 5cd7b33 commit e649b0a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 23 deletions.
17 changes: 17 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,23 @@ <h1>Configuration</h1>
<td></td>
</tr>

<tr id="widgetclass">
<td><a href="#" class="permalink">widgetClass</a></td>
<td>String</td>
<td>'widget-{name}'</td>
<td>When the table has a class name that matches the template and a widget id that matches the <code>{name}</code>, the widget will automatically be added to the table (<span class="version">v2.18.0</span>)
<div class="collapsible">
<br>
By default, this option is set to <code>'widget-{name}'</code>. So if the table has a class name of <code>widget-zebra</code> the zebra widget will be automatically added to the <code>config.widgets</code> option and applied to the table.<br>
<br>
Some widget ID's with special characters may not be detected; ID's with letters, numbers, underscores and/or dashes will be correctly detected.<br>
<br>
The template string *must* contain the <code>{name}</code> tag.
</div>
</td>
<td></td>
</tr>

<tr id="onrenderheader">
<td><a href="#" class="permalink">onRenderHeader</a></td>
<td>Function</td>
Expand Down
21 changes: 17 additions & 4 deletions js/jquery.tablesorter.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
zebra : [ 'even', 'odd' ] // zebra widget alternating row class names
},
initWidgets : true, // apply widgets on tablesorter initialization
widgetClass : 'widget-{name}', // table class name template to match to include a widget

// *** callbacks
initialized : null, // function(table){},
Expand Down Expand Up @@ -1561,10 +1562,20 @@
var c = table.config,
wo = c.widgetOptions,
widgets = [],
time, w, wd;
time, time2, w, wd;
// prevent numerous consecutive widget applications
if (init !== false && table.hasInitialized && (table.isApplyingWidgets || table.isUpdating)) { return; }
if (c.debug) { time = new Date(); }
wd = new RegExp( '\\b' + c.widgetClass.replace( /\{name\}/i, '([\\w-]+)' )+ '\\b', 'g' );
if ( c.table.className.match( wd ) ) {
// extract out the widget id from the table class (widget id's can include dashes)
w = c.table.className.match( wd );
if ( w ) {
$.each( w, function( i,n ){
c.widgets.push( n.replace( wd, '$1' ) );
});
}
}
if (c.widgets.length) {
table.isApplyingWidgets = true;
// ensure unique widget ids
Expand Down Expand Up @@ -1594,17 +1605,22 @@
wo = table.config.widgetOptions = $.extend( true, {}, w.options, wo );
}
if (w.hasOwnProperty('init')) {
if (c.debug) { time2 = new Date(); }
w.init(table, w, c, wo);
if (c.debug) { ts.benchmark('Initializing ' + w.id + ' widget', time2); }
}
}
if (!init && w.hasOwnProperty('format')) {
if (c.debug) { time2 = new Date(); }
w.format(table, c, wo, false);
if (c.debug) { ts.benchmark( ( init ? 'Initializing ' : 'Applying ' ) + w.id + ' widget', time2); }
}
}
});
}
setTimeout(function(){
table.isApplyingWidgets = false;
$.data(table, 'lastWidgetApplication', new Date());
}, 0);
if (c.debug) {
w = c.widgets.length;
Expand Down Expand Up @@ -1888,9 +1904,6 @@
$tr.removeClass(wo.zebra[even ? 1 : 0]).addClass(wo.zebra[even ? 0 : 1]);
});
}
if (c.debug) {
ts.benchmark("Applying Zebra widget", time);
}
},
remove: function(table, c, wo){
var k, $tb,
Expand Down
17 changes: 3 additions & 14 deletions js/jquery.tablesorter.widgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ ts.addWidget({
columns : [ "primary", "secondary", "tertiary" ]
},
format: function(table, c, wo) {
var time, $tbody, tbodyIndex, $rows, rows, $row, $cells, remove, indx,
var $tbody, tbodyIndex, $rows, rows, $row, $cells, remove, indx,
$table = c.$table,
$tbodies = c.$tbodies,
sortList = c.sortList,
Expand All @@ -283,9 +283,6 @@ ts.addWidget({
css = wo && wo.columns || [ "primary", "secondary", "tertiary" ],
last = css.length - 1;
remove = css.join(' ');
if (c.debug) {
time = new Date();
}
// check if there is a sort (on initialization there may not be one)
for (tbodyIndex = 0; tbodyIndex < $tbodies.length; tbodyIndex++ ) {
$tbody = ts.processTbody(table, $tbodies.eq(tbodyIndex), true); // detach tbody
Expand Down Expand Up @@ -325,9 +322,6 @@ ts.addWidget({
}
}
}
if (c.debug) {
ts.benchmark("Applying Columns widget", time);
}
},
remove: function(table, c, wo) {
var tbodyIndex, $tbody,
Expand Down Expand Up @@ -581,11 +575,8 @@ ts.filter = {
and : 'and'
}, ts.language);

var options, string, txt, $header, column, filters, val, time, fxn, noSelect,
var options, string, txt, $header, column, filters, val, fxn, noSelect,
regex = ts.filter.regex;
if (c.debug) {
time = new Date();
}
c.$table.addClass('hasFilters');

// define timers so using clearTimeout won't cause an undefined error
Expand Down Expand Up @@ -717,9 +708,6 @@ ts.filter = {
// set filtered rows count (intially unfiltered)
c.filteredRows = c.totalRows;

if (c.debug) {
ts.benchmark("Applying Filter widget", time);
}
// add default values
c.$table.bind('tablesorter-initialized pagerInitialized', function() {
// redefine "wo" as it does not update properly inside this callback
Expand All @@ -728,6 +716,7 @@ ts.filter = {
if (filters.length) {
// prevent delayInit from triggering a cache build if filters are empty
if ( !(c.delayInit && filters.join('') === '') ) {

ts.setFilters(table, filters, true);
}
}
Expand Down
6 changes: 1 addition & 5 deletions js/widgets/widget-grouping.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ ts.grouping = {

update : function(table, c, wo){
if ($.isEmptyObject(c.cache)) { return; }
var rowIndex, tbodyIndex, currentGroup, $rows, groupClass, grouping, time, cache, saveName, direction,
var rowIndex, tbodyIndex, currentGroup, $rows, groupClass, grouping, cache, saveName, direction,
lang = wo.grouping_language,
group = '',
savedGroup = false,
Expand All @@ -67,7 +67,6 @@ ts.grouping = {
c.$table.data('pagerSavedHeight', 0);
}
if (column >= 0 && !c.$headers.filter('[data-column="' + column + '"]:last').hasClass('group-false')) {
if (c.debug){ time = new Date(); }
wo.group_currentGroup = ''; // save current groups
wo.group_currentGroups = {};

Expand Down Expand Up @@ -151,9 +150,6 @@ ts.grouping = {
}
});
c.$table.trigger(wo.group_complete);
if (c.debug) {
$.tablesorter.benchmark("Applying groups widget: ", time);
}
}
},

Expand Down

0 comments on commit e649b0a

Please sign in to comment.