tablesorter/js/widgets/widget-formatter.js

71 lines
2.2 KiB
JavaScript
Raw Normal View History

2015-02-07 06:46:44 +00:00
/*! tablesorter Formatter widget - 2/7/2015 (v2.19.0)
* Requires tablesorter v2.8+ and jQuery 1.7+
* by Rob Garrison
*/
/*jshint browser:true, jquery:true, unused:false */
/*global jQuery: false */
;(function($){
'use strict';
var ts = $.tablesorter;
ts.formatter = {
init : function( c ) {
var events = $.trim( c.widgetOptions.formatter_event ) + ' pagerComplete updateComplete '
.split(' ').join('.tsformatter ');
c.$table.on( events, function() {
ts.formatter.setup( c );
});
ts.formatter.setup( c );
},
setup : function( c ) {
// do nothing for empty tables
if ( $.isEmptyObject( c.cache ) ) { return; }
2015-02-05 21:12:38 +00:00
var $tbody, tbodyIndex, rowIndex, rows, cell, len, column,
wo = c.widgetOptions,
2015-02-05 21:12:38 +00:00
data = { config: c, wo: wo },
formatter = [],
$headers = [];
// set up variables
for ( column = 0; column < c.columns; column++ ) {
$headers[ column ] = c.$headers.filter('[data-column="' + column + '"]:last');
formatter[ column ] = ts.getColumnData( c.table, wo.formatter_column, column ) || false;
}
// main loop
for ( tbodyIndex = 0; tbodyIndex < c.$tbodies.length; tbodyIndex++ ){
$tbody = ts.processTbody( c.table, c.$tbodies.eq( tbodyIndex ), true ); // detach tbody
rows = c.cache[ tbodyIndex ];
len = rows.normalized.length;
for ( rowIndex = 0; rowIndex < len; rowIndex++ ) {
data.$row = rows.normalized[ rowIndex ][ c.columns ].$row;
2015-02-05 21:12:38 +00:00
data.$cells = data.$row.children( 'th, td' );
for ( column = 0; column < c.columns; column++ ) {
2015-02-05 21:12:38 +00:00
if ( formatter[ column ] ) {
data.columnIndex = column;
2015-02-05 21:12:38 +00:00
data.$header = $headers[ column ];
data.$cell = data.$cells.eq( column );
cell = data.$cell[0];
// get text from attribute first, just in case we're updating
2015-02-05 21:12:38 +00:00
data.text = cell.getAttribute( c.textAttribute ) || cell.textContent || data.$cell.text();
cell.innerHTML = formatter[ column ]( data.text, data );
}
}
}
ts.processTbody( c.table, $tbody, false); // restore tbody
}
}
};
ts.addWidget({
id: 'formatter',
priority: 100,
options: {
formatter_column : {},
formatter_event : 'applyFormatter'
},
init: function( table ) {
ts.formatter.init( table.config );
}
});
})( jQuery );