2015-03-28 20:03:50 +00:00
|
|
|
/*! Widget: formatter - 2/9/2015 (v2.19.1) *//*
|
2015-02-04 21:41:27 +00:00
|
|
|
* 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 ) {
|
2015-02-20 22:13:40 +00:00
|
|
|
var events = c.widgetOptions.formatter_event +
|
2015-02-09 21:59:15 +00:00
|
|
|
' pagerComplete updateComplete '.split(' ').join('.tsformatter ');
|
|
|
|
c.$table
|
2015-02-20 22:13:40 +00:00
|
|
|
.off( events.replace(/\s+/g, ' ') )
|
|
|
|
.on( events, function() {
|
2015-02-09 21:59:15 +00:00
|
|
|
ts.formatter.setup( c );
|
|
|
|
});
|
2015-02-04 21:41:27 +00:00
|
|
|
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,
|
2015-02-04 21:41:27 +00:00
|
|
|
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++ ) {
|
2015-02-27 20:26:16 +00:00
|
|
|
$headers[ column ] = c.$headerIndexed[ column ];
|
2015-02-05 21:12:38 +00:00
|
|
|
formatter[ column ] = ts.getColumnData( c.table, wo.formatter_column, column ) || false;
|
|
|
|
}
|
|
|
|
// main loop
|
2015-02-04 21:41:27 +00:00
|
|
|
for ( tbodyIndex = 0; tbodyIndex < c.$tbodies.length; tbodyIndex++ ){
|
2015-02-05 20:35:27 +00:00
|
|
|
$tbody = ts.processTbody( c.table, c.$tbodies.eq( tbodyIndex ), true ); // detach tbody
|
2015-02-04 21:41:27 +00:00
|
|
|
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' );
|
2015-02-04 21:41:27 +00:00
|
|
|
for ( column = 0; column < c.columns; column++ ) {
|
2015-02-05 21:12:38 +00:00
|
|
|
if ( formatter[ column ] ) {
|
2015-02-04 21:41:27 +00:00
|
|
|
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];
|
2015-02-04 21:41:27 +00:00
|
|
|
// 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 );
|
2015-02-04 21:41:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-05 20:35:27 +00:00
|
|
|
ts.processTbody( c.table, $tbody, false); // restore tbody
|
2015-02-04 21:41:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ts.addWidget({
|
|
|
|
id: 'formatter',
|
|
|
|
priority: 100,
|
|
|
|
options: {
|
2015-02-05 19:04:56 +00:00
|
|
|
formatter_column : {},
|
|
|
|
formatter_event : 'applyFormatter'
|
2015-02-04 21:41:27 +00:00
|
|
|
},
|
|
|
|
init: function( table ) {
|
|
|
|
ts.formatter.init( table.config );
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
})( jQuery );
|