2015-03-26 15:48:43 +00:00
/*! Widget: Build Table - updated 3/26/2015 (v2.21.3) */
2015-10-31 15:08:21 +00:00
/ *
* for tableSorter v2 . 16.0 +
* by Rob Garrison
* /
/*jshint browser:true, jquery:true, unused:false */
/*global jQuery: false */
! function ( a ) { "use strict" ; var b = a . tablesorter = a . tablesorter || { } ,
// build a table from data (requires existing <table> tag)
// data.header contains an array of header titles
// data.rows contains an array of rows which contains an array of cells
c = b . buildTable = function ( d , e ) {
// add table if one doesn't exist
var f = "TABLE" === d . nodeName ? a ( d ) : a ( "<table>" ) . appendTo ( d ) , g = f [ 0 ] , h = e . widgetOptions = a . extend ( ! 0 , { } , c . defaults , e . widgetOptions ) , i = h . build _processing , j = h . build _type , k = h . build _source || e . data ,
// determine type: html, json, array, csv, object
l = function ( b ) { var d = a . type ( b ) , f = b instanceof jQuery ;
// String (html or unprocessed json) or jQuery object
if (
// run any processing if set
"function" == typeof i && ( b = i ( b , h ) ) ,
// store processed data in table.config.data
e . data = b , f || "string" === d ) {
// look for </tr> closing tag, then we have an HTML string
if ( f || /<\s*\/tr\s*>/ . test ( b ) ) return c . html ( g , b , h ) ; try { if ( b = a . parseJSON ( b || "null" ) )
// valid JSON!
return c . object ( g , b , h ) } catch ( k ) { } }
// Array
// Array
return "array" === d || "string" === d || "array" === j || "csv" === j ? c . csv ( g , b , h ) : c . object ( g , b , h ) } ;
// even if wo.build_type is undefined, we can try to figure out the type
// store config
// even if wo.build_type is undefined, we can try to figure out the type
// get data from within a jQuery object (csv)
// load data via ajax
return g . config = e , b . buildTable . hasOwnProperty ( j ) || "" === j ? void ( k instanceof jQuery ? l ( a . trim ( k . html ( ) ) ) : k && ( k . hasOwnProperty ( "url" ) || "json" === j ) ? a . ajax ( h . build _source ) . done ( function ( a ) { l ( a ) } ) . fail ( function ( a , b , c ) { e . debug && console . error ( "aborting build table widget, failed ajax load" ) , f . html ( '<tr><td class="error">' + a . status + " " + b + "</td></tr>" ) } ) : l ( k ) ) : ( e . debug && console . error ( "aborting build table widget, incorrect build type" ) , ! 1 ) } ; c . defaults = {
// *** build widget core ***
build _type : "" , // array, csv, object, json, html
build _source : "" , // array, object, jQuery Object or ajaxObject { url: '', dataType: 'json' },
build _processing : null , // function that returns a useable build_type (e.g. string to array)
build _complete : "tablesorter-build-complete" , // triggered event when build completes
// *** CSV & Array ***
build _headers : { rows : 1 , // Number of header rows from the csv
classes : [ ] , // Header classes to apply to cells
text : [ ] , // Header cell text
widths : [ ] } , build _footers : { rows : 1 , // Number of header rows from the csv
classes : [ ] , // Footer classes to apply to cells
text : [ ] } , build _numbers : { addColumn : ! 1 , // include row numbering column?
sortable : ! 1 } ,
// *** CSV only options ***
build _csvStartLine : 0 , // line within the csv to start adding to table
build _csvSeparator : "," , // csv separator
// *** build object options ***
build _objectRowKey : "rows" , // object key containing table rows
build _objectCellKey : "cells" , // object key containing table cells (within the rows object)
build _objectHeaderKey : "headers" , // object key containing table headers
build _objectFooterKey : "footers" } , c . build = { colgroup : function ( b ) { var c = "" ;
// add colgroup if widths set
return b && b . length && ( c += "<colgroup>" , a . each ( b , function ( a , b ) { c += "<col" + ( b ? ' style="width:' + b + '"' : "" ) + ">" } ) , c += "</colgroup>" ) , c } ,
// d = cell data; typ = 'th' or 'td'; first = save widths from first header row only
cell : function ( b , c , d , e , f ) { var g , h , i = f ? a ( "<col>" ) : "" , j = c . build _headers . classes , k = c . build _headers . widths ;
// d is just an array
if ( /string|number/ . test ( typeof b ) ) h = a ( "<" + d + ( j && j [ e ] ? ' class="' + j [ e ] + '"' : "" ) + ">" + b + "</" + d + ">" ) , f && k && k [ e ] && i . width ( k [ e ] || "" ) ; else {
// assume we have an object
h = a ( "<" + d + ">" ) ; for ( g in b ) b . hasOwnProperty ( g ) && ( "text" === g || "html" === g ? h [ g ] ( b [ g ] ) : f && "width" === g ?
// set column width, but only from first row
i . width ( b [ g ] || "" ) : h . attr ( g , b [ g ] ) ) } return [ h , i ] } ,
// h1 = header text from data
header : function ( b , c ) { var d = c . build _headers . text , e = c . build _headers . classes , f = "<tr>" + ( c . build _numbers . addColumn ? "<th" + ( c . build _numbers . sortable ? "" : ' class="sorter-false"' ) + ">" + c . build _numbers . addColumn + "</th>" : "" ) ; return a . each ( b , function ( a , b ) { f += /<\s*\/t(d|h)\s*>/ . test ( b ) ? b : "<th" + ( e && e [ a ] ? ' class="' + e [ a ] + '"' : "" ) + ">" + ( d && d [ a ] ? d [ a ] : b ) + "</th>" } ) , f + "</tr>" } , rows : function ( b , c , d , e , f , g ) { var h = g ? "th" : "td" , i = "<tr>" + ( e . build _numbers . addColumn ? "<" + h + ">" + ( g ? "" : f ) + "</" + h + ">" : "" ) ; return a . each ( b , function ( a , b ) {
// test if HTML is already included; look for closing </td>
i += /<\s*\/t(d|h)\s*>/ . test ( b ) ? b : "<" + ( g ? h + ( d && d [ a ] ? ' class="' + d [ a ] + '"' : "" ) : h ) + ">" + ( g && c && c . length && c [ a ] ? c [ a ] : b ) + "</" + h + ">" } ) , i + "</tr>" } } , c . buildComplete = function ( c , d ) { a ( c ) . trigger ( d . build _complete ) , b . setup ( c , c . config ) } , / * = = = = A r r a y e x a m p l e = = = =
[
[ "header1" , "header2" , ... "headerN" ] ,
[ "row1cell1" , "row1cell2" , ... "row1cellN" ] ,
[ "row2cell1" , "row2cell2" , ... "row2cellN" ] ,
...
[ "rowNcell1" , "rowNcell2" , ... "rowNcellN" ]
]
* /
c . array = function ( a , b , d ) { return c . csv ( a , b , d ) } , / * = = = = C S V e x a m p l e = = = =
ID , Name , Age , Date
A42b , Parker , 28 , "Jul 6, 2006 8:14 AM"
A255 , Hood , 33 , "Dec 10, 2002 5:14 AM"
A33 , Kent , 18 , "Jan 12, 2003 11:14 AM"
A1 , Franklin , 45 , "Jan 18, 2001 9:12 AM"
A102 , Evans , 22 , "Jan 18, 2007 9:12 AM"
A42a , Everet , 22 , "Jan 18, 2007 9:12 AM"
ID , Name , Age , Date
* /
// Adapted & modified from csvToTable.js by Steve Sobel
// MIT license: https://code.google.com/p/jquerycsvtotable/
c . csv = function ( b , d , e ) { var f , g , h , i = "csv" === e . build _type || "string" == typeof d , j = a ( b ) , k = i ? d . replace ( "\r" , "" ) . split ( "\n" ) : d , l = k . length , m = 0 , n = ! 1 , o = e . build _headers . rows + ( i ? e . build _csvStartLine : 0 ) , p = e . build _footers . rows , q = 0 , r = "" , s = c . build . colgroup ( e . build _headers . widths ) + "<thead>" ; a . each ( k , function ( a , b ) { a >= l - p && ( n = ! 0 ) ,
// build header
( i ? a >= e . build _csvStartLine : ! 0 ) && o > a ? ( g = i ? c . splitCSV ( b , e . build _csvSeparator ) : b , q = g . length , s += c . build . header ( g , e ) ) : a >= o && (
// build tbody & tfoot rows
a === o && ( s += "</thead><tbody>" ) , h = i ? c . splitCSV ( b , e . build _csvSeparator ) : b , n && p > 0 && ( s += ( a === l - p ? "</tbody><tfoot>" : "" ) + ( a === l ? "</tfoot>" : "" ) ) , h . length > 1 && ( m ++ , h . length !== q && ( r += "error on line " + a + ": Item count (" + h . length + ") does not match header count (" + q + ") \n" ) , f = n ? e . build _footers . classes : "" , s += c . build . rows ( h , e . build _footers . text , f , e , m , n ) ) ) } ) , s += p > 0 ? "" : "</tbody>" , r ? j . html ( r ) : ( j . html ( s ) , c . buildComplete ( b , e ) ) } ,
// CSV Parser by Brian Huisman (http://www.greywyvern.com/?post=258)
c . splitCSV = function ( b , c ) { var d , e , f = a . trim ( b ) . split ( c = c || "," ) ; for ( d = f . length - 1 ; d >= 0 ; d -- ) '"' === f [ d ] . replace ( /\"\s+$/ , '"' ) . charAt ( f [ d ] . length - 1 ) ? ( e = f [ d ] . replace ( /^\s+\"/ , '"' ) ) . length > 1 && '"' === e . charAt ( 0 ) ? f [ d ] = f [ d ] . replace ( /^\s*"|"\s*$/g , "" ) . replace ( /""/g , '"' ) : d ? f . splice ( d - 1 , 2 , [ f [ d - 1 ] , f [ d ] ] . join ( c ) ) : f = f . shift ( ) . split ( c ) . concat ( f ) : f [ d ] . replace ( /""/g , '"' ) ; return f } ,
// data may be a jQuery object after processing
c . html = function ( b , d , e ) { var f = a ( b ) ; d instanceof jQuery ? f . empty ( ) . append ( d ) : f . html ( d ) , c . buildComplete ( b , e ) } , / * = = = = O b j e c t e x a m p l e = = = =
data : {
headers : [
[
{ text : 'First Name' , class : 'fname' , width : '20%' } , // row 1 cell 1
'Last Name' ,
{ text : 'Age' , class : 'age' , 'data-sorter' : false } ,
'Total' ,
{ text : 'Discount' , class : 'sorter-false' } ,
{ text : 'Date' , class : 'date' } // row 1 cell 6
]
] ,
footers : 'clone' , // clone headers or assign array like headers
rows : [
// TBODY 1
[ 'Peter' , 'Parker' , 28 , '$9.99' , '20%' , 'Jul 6, 2006 8:14 AM' ] , // row 1
[ 'John' , 'Hood' , 33 , '$19.99' , '25%' , 'Dec 10, 2002 5:14 AM' ] , // row 2
[ 'Clark' , 'Kent' , 18 , '$15.89' , '44%' , 'Jan 12, 2003 11:14 AM' ] , // row 3
// TBODY 2
{ newTbody : true , class : 'tablesorter-infoOnly' } ,
{ cells : [ { text : 'Info Row' , colSpan : 6 } ] } , // row 4
// TBODY 3
{ newTbody : true } ,
[ 'Bruce' , 'Evans' , 22 , '$13.19' , '11%' , 'Jan 18, 2007 9:12 AM' ] , // row 5
[ 'Brice' , 'Almighty' , 45 , '$153.19' , '44%' , 'Jan 18, 2001 9:12 AM' ] , // row 6
{ class : 'specialRow' , // row 7
cells : [
{ text : 'Fred' , class : 'fname' } ,
{ text : 'Smith' , class : 'lname' } ,
{ text : 18 , class : 'age' , 'data-info' : 'fake ID!, he is really 16' } ,
{ text : '$22.44' , class : 'total' } ,
{ text : '8%' , class : 'discount' } ,
{ text : 'Aug 20, 2012 10:15 AM' , class : 'date' }
] ,
'data-info' : 'This row likes turtles'
}
]
}
* /
c . object = function ( b , d , e ) {
// 'rows'
var f , g , h , i , j , k , l , m = b . config , n = e . build _objectHeaderKey , o = e . build _objectRowKey , p = d . hasOwnProperty ( n ) && ! a . isEmptyObject ( d . kh ) ? d . kh : d . hasOwnProperty ( "headers" ) ? d . headers : ! 1 , q = d . hasOwnProperty ( o ) && ! a . isEmptyObject ( d . kr ) ? d . kr : d . hasOwnProperty ( "rows" ) ? d . rows : ! 1 ;
// Build thead
// h = [ ['headerRow1Cell1', 'headerRow1Cell2', ... 'headerRow1CellN' ], ['headerRow2Cell1', ... ] ]
// or h = [ [ { text: 'firstCell', class: 'fc', width: '20%' }, ..., { text: 'last Cell' } ], [ /* second row */ ] ]
// add colgroup if it contains col elements
// Build tbody
// add footer
return p && q && 0 !== p . length && 0 !== q . length ? ( i = a ( "<colgroup>" ) , j = a ( "<table><thead/></table>" ) , a . each ( p , function ( b , d ) { for ( l = a ( "<tr>" ) . appendTo ( j . find ( "thead" ) ) , g = d . length , f = 0 ; g > f ; f ++ ) h = c . build . cell ( d [ f ] , e , "th" , f , 0 === b ) , h [ 0 ] && h [ 0 ] . length && h [ 0 ] . appendTo ( l ) , 0 === b && h [ 1 ] && h [ 1 ] . appendTo ( i ) } ) , i . find ( "col[style]" ) . length && j . prepend ( i ) , k = a ( "<tbody>" ) , a . each ( q , function ( b , d ) { var f ; if ( h = "object" === a . type ( d ) , h && d . newTbody ) { k = a ( "<tbody>" ) . appendTo ( j ) ; for ( f in d ) d . hasOwnProperty ( f ) && "newTbody" !== f && k . attr ( f , d [ f ] ) } else { if ( 0 === b && k . appendTo ( j ) , l = a ( "<tr>" ) . appendTo ( k ) , h ) { for ( f in d ) d . hasOwnProperty ( f ) && f !== e . build _objectCellKey && l . attr ( f , d [ f ] ) ; d . hasOwnProperty ( e . build _objectCellKey ) && ( d = d . cells ) } for ( g = d . length , f = 0 ; g > f ; f ++ ) i = c . build . cell ( d [ f ] , e , "td" , f ) , i [ 0 ] && i [ 0 ] . length && i [ 0 ] . appendTo ( l ) } } ) , d . hasOwnProperty ( e . build _objectFooterKey ) && ( h = d [ e . build _objectFooterKey ] , "clone" === h ? ( i = j . find ( "thead" ) . html ( ) , j . append ( "<tfoot>" + i + "</tfoot>" ) ) : ( i = a ( "<tfoot>" ) . appendTo ( j ) , a . each ( h , function ( b , d ) { for ( l = a ( "<tr>" ) . appendTo ( i ) , g = d . length , f = 0 ; g > f ; f ++ ) k = c . build . cell ( d [ f ] , e , "th" , f ) , k [ 0 ] && k [ 0 ] . length && k [ 0 ] . appendTo ( l ) } ) ) ) , a ( b ) . html ( j . html ( ) ) , void c . buildComplete ( b , e ) ) : ( m . debug && console . error ( "aborting build table widget, missing data for object build" ) , ! 1 ) } , c . ajax = c . json = function ( a , b , d ) { return c . object ( a , b , d ) } } ( jQuery ) ;