Core & Filter: Add "entire column colspan" support. See #746 & #1047

This commit is contained in:
Rob Garrison 2015-10-17 15:15:36 -05:00
parent c7d4395c85
commit 55d19b3a95
2 changed files with 142 additions and 98 deletions

View File

@ -253,7 +253,7 @@
}; };
function buildParserCache( c, $tbodies ) { function buildParserCache( c, $tbodies ) {
var rows, list, l, i, h, ch, np, p, e, time, tb, len, var rows, list, span, max, colIndex, i, h, ch, np, p, e, time, tb, len,
table = c.table, table = c.table,
j = 0, j = 0,
debug = {}; debug = {};
@ -274,39 +274,48 @@
while (j < len) { while (j < len) {
rows = tb[j].rows; rows = tb[j].rows;
if (rows.length) { if (rows.length) {
l = c.columns; // rows[j].cells.length; colIndex = 0;
for (i = 0; i < l; i++) { max = c.columns; // rows[j].cells.length;
h = c.$headerIndexed[i]; for (i = 0; i < max; i++) {
// get column indexed table cell h = c.$headerIndexed[ colIndex ];
ch = ts.getColumnData( table, c.headers, i ); if ( h && h.length ) {
// get column parser/extractor // get column indexed table cell
e = ts.getParserById( ts.getData(h, ch, 'extractor') ); ch = ts.getColumnData( table, c.headers, colIndex );
p = ts.getParserById( ts.getData(h, ch, 'sorter') ); // get column parser/extractor
np = ts.getData(h, ch, 'parser') === 'false'; e = ts.getParserById( ts.getData(h, ch, 'extractor') );
// empty cells behaviour - keeping emptyToBottom for backwards compatibility p = ts.getParserById( ts.getData(h, ch, 'sorter') );
c.empties[i] = ( ts.getData(h, ch, 'empty') || c.emptyTo || (c.emptyToBottom ? 'bottom' : 'top' ) ).toLowerCase(); np = ts.getData(h, ch, 'parser') === 'false';
// text strings behaviour in numerical sorts // empty cells behaviour - keeping emptyToBottom for backwards compatibility
c.strings[i] = ( ts.getData(h, ch, 'string') || c.stringTo || 'max' ).toLowerCase(); c.empties[colIndex] = ( ts.getData(h, ch, 'empty') || c.emptyTo || (c.emptyToBottom ? 'bottom' : 'top' ) ).toLowerCase();
if (np) { // text strings behaviour in numerical sorts
p = ts.getParserById('no-parser'); c.strings[colIndex] = ( ts.getData(h, ch, 'string') || c.stringTo || 'max' ).toLowerCase();
if (np) {
p = ts.getParserById('no-parser');
}
if (!e) {
// For now, maybe detect someday
e = false;
}
if (!p) {
p = detectParserForColumn(c, rows, -1, i);
}
if (c.debug) {
debug[ '(' + colIndex + ') ' + h.text() ] = {
parser : p.id,
extractor : e ? e.id : 'none',
string : c.strings[colIndex],
empty : c.empties[colIndex]
};
}
list.parsers[colIndex] = p;
list.extractors[colIndex] = e;
span = h[0].colSpan - 1;
if ( span > 0 ) {
colIndex += span;
max += span;
}
} }
if (!e) { colIndex++;
// For now, maybe detect someday
e = false;
}
if (!p) {
p = detectParserForColumn(c, rows, -1, i);
}
if (c.debug) {
debug[ '(' + i + ') ' + h.text() ] = {
parser : p.id,
extractor : e ? e.id : 'none',
string : c.strings[i],
empty : c.empties[i]
};
}
list.parsers[i] = p;
list.extractors[i] = e;
} }
} }
j += (list.parsers.length) ? len : 1; j += (list.parsers.length) ? len : 1;
@ -326,8 +335,8 @@
/* utils */ /* utils */
function buildCache(table, callback, $tbodies) { function buildCache(table, callback, $tbodies) {
var cc, t, v, i, j, k, $tb, $row, cols, cacheTime, var cc, t, v, i, j, k, $tb, $row, cols, cell, cacheTime,
totalRows, rowData, prevRowData, colMax, totalRows, rowData, prevRowData, colMax, span, cacheIndex, max,
c = table.config, c = table.config,
parsers = c.parsers; parsers = c.parsers;
// update tbody variable // update tbody variable
@ -379,29 +388,52 @@
t = prevRowData.child.length; t = prevRowData.child.length;
prevRowData.child[ t ] = []; prevRowData.child[ t ] = [];
// child row content does not account for colspans/rowspans; so indexing may be off // child row content does not account for colspans/rowspans; so indexing may be off
for ( j = 0; j < c.columns; j++ ) { cacheIndex = 0;
prevRowData.child[ t ][ j ] = ts.getParsedText( c, v[ j ], j ); max = c.columns;
for ( j = 0; j < max; j++ ) {
cell = v[ j ];
if ( cell ) {
prevRowData.child[ t ][ j ] = ts.getParsedText( c, cell, j );
span = v[ j ].colSpan - 1;
if ( span > 0 ) {
cacheIndex += span;
max += span
}
}
cacheIndex++;
} }
// go to the next for loop // go to the next for loop
continue; continue;
} }
rowData.$row = $row; rowData.$row = $row;
rowData.order = i; // add original row position to rowCache rowData.order = i; // add original row position to rowCache
for ( j = 0; j < c.columns; ++j ) { cacheIndex = 0;
if (typeof parsers[ j ] === 'undefined') { max = c.columns;
if ( c.debug ) { for ( j = 0; j < max; ++j ) {
console.warn( 'No parser found for cell:', $row[ 0 ].cells[ j ], 'does it have a header?' ); cell = $row[ 0 ].cells[ j ];
if ( cell ) {
if (typeof parsers[ cacheIndex ] === 'undefined') {
if ( c.debug ) {
console.warn( 'No parser found for cell:', cell, 'does it have a header?' );
}
continue;
}
t = ts.getElementText( c, cell, cacheIndex );
rowData.raw[ cacheIndex ] = t; // save original row text
v = ts.getParsedText( c, cell, cacheIndex, t );
cols[ cacheIndex ] = v;
if ( ( parsers[ cacheIndex ].type || '' ).toLowerCase() === 'numeric' ) {
// determine column max value (ignore sign)
colMax[ j ] = Math.max( Math.abs( v ) || 0, colMax[ cacheIndex ] || 0 );
}
// allow colSpan in tbody
span = cell.colSpan - 1;
if ( span > 0 ) {
cacheIndex += span;
max += span;
} }
continue;
}
t = ts.getElementText( c, $row[ 0 ].cells[j], j );
rowData.raw.push( t ); // save original row text
v = ts.getParsedText( c, $row[ 0 ].cells[ j ], j, t );
cols.push( v );
if ( ( parsers[ j ].type || '' ).toLowerCase() === 'numeric' ) {
// determine column max value (ignore sign)
colMax[ j ] = Math.max( Math.abs( v ) || 0, colMax[ j ] || 0 );
} }
cacheIndex++;
} }
// ensure rowData is always in the same location (after the last column) // ensure rowData is always in the same location (after the last column)
cols[ c.columns ] = rowData; cols[ c.columns ] = rowData;
@ -487,7 +519,9 @@
$t = c.$headers.filter('[data-column="' + indx + '"]'); $t = c.$headers.filter('[data-column="' + indx + '"]');
// target sortable column cells, unless there are none, then use non-sortable cells // target sortable column cells, unless there are none, then use non-sortable cells
// .last() added in jQuery 1.4; use .filter(':last') to maintain compatibility with jQuery v1.2.6 // .last() added in jQuery 1.4; use .filter(':last') to maintain compatibility with jQuery v1.2.6
c.$headerIndexed[indx] = $t.not('.sorter-false').length ? $t.not('.sorter-false').filter(':last') : $t.filter(':last'); c.$headerIndexed[indx] = $t.length ?
$t.not('.sorter-false').length ? $t.not('.sorter-false').filter(':last') : $t.filter(':last') :
$();
} }
c.$table.find(c.selectorHeaders).attr({ c.$table.find(c.selectorHeaders).attr({
scope: 'col', scope: 'col',

View File

@ -647,71 +647,81 @@
return parsed ? c.parsers[column].format( filter, c.table, [], column ) : filter; return parsed ? c.parsers[column].format( filter, c.table, [], column ) : filter;
}, },
buildRow: function( table, c, wo ) { buildRow: function( table, c, wo ) {
var col, column, $header, makeSelect, disabled, name, ffxn, tmp, var $filter, col, column, $header, makeSelect, disabled, name, ffxn, tmp,
// c.columns defined in computeThIndexes() // c.columns defined in computeThIndexes()
cellFilter = wo.filter_cellFilter, cellFilter = wo.filter_cellFilter,
columns = c.columns, columns = c.columns,
arry = $.isArray( cellFilter ), arry = $.isArray( cellFilter ),
buildFilter = '<tr role="row" class="' + tscss.filterRow + ' ' + c.cssIgnoreRow + '">'; buildFilter = '<tr role="row" class="' + tscss.filterRow + ' ' + c.cssIgnoreRow + '">';
for ( column = 0; column < columns; column++ ) { for ( column = 0; column < columns; column++ ) {
buildFilter += '<td'; if ( c.$headerIndexed[ column ].length ) {
if ( arry ) { buildFilter += '<td data-column="' + column + '"';
buildFilter += ( cellFilter[ column ] ? ' class="' + cellFilter[ column ] + '"' : '' ); // account for entire column set with colspan. See #1047
} else { tmp = c.$headerIndexed[ column ] && c.$headerIndexed[ column ][0].colSpan || 0;
buildFilter += ( cellFilter !== '' ? ' class="' + cellFilter + '"' : '' ); if ( tmp > 1 ) {
buildFilter += ' colspan="' + tmp + '"';
}
if ( arry ) {
buildFilter += ( cellFilter[ column ] ? ' class="' + cellFilter[ column ] + '"' : '' );
} else {
buildFilter += ( cellFilter !== '' ? ' class="' + cellFilter + '"' : '' );
}
buildFilter += '></td>';
} }
buildFilter += '></td>';
} }
c.$filters = $( buildFilter += '</tr>' ) c.$filters = $( buildFilter += '</tr>' )
.appendTo( c.$table.children( 'thead' ).eq( 0 ) ) .appendTo( c.$table.children( 'thead' ).eq( 0 ) )
.find( 'td' ); .children( 'td' );
// build each filter input // build each filter input
for ( column = 0; column < columns; column++ ) { for ( column = 0; column < columns; column++ ) {
disabled = false; disabled = false;
// assuming last cell of a column is the main column // assuming last cell of a column is the main column
$header = c.$headerIndexed[ column ]; $header = c.$headerIndexed[ column ];
ffxn = ts.getColumnData( table, wo.filter_functions, column ); if ( $header && $header.length ) {
makeSelect = ( wo.filter_functions && ffxn && typeof ffxn !== 'function' ) || $filter = c.$filters.filter( '[data-column="' + column + '"]' );
$header.hasClass( 'filter-select' ); ffxn = ts.getColumnData( table, wo.filter_functions, column );
// get data from jQuery data, metadata, headers option or header class name makeSelect = ( wo.filter_functions && ffxn && typeof ffxn !== 'function' ) ||
col = ts.getColumnData( table, c.headers, column ); $header.hasClass( 'filter-select' );
disabled = ts.getData( $header[0], col, 'filter' ) === 'false' || // get data from jQuery data, metadata, headers option or header class name
ts.getData( $header[0], col, 'parser' ) === 'false'; col = ts.getColumnData( table, c.headers, column );
disabled = ts.getData( $header[0], col, 'filter' ) === 'false' ||
ts.getData( $header[0], col, 'parser' ) === 'false';
if ( makeSelect ) { if ( makeSelect ) {
buildFilter = $( '<select>' ).appendTo( c.$filters.eq( column ) ); buildFilter = $( '<select>' ).appendTo( $filter );
} else {
ffxn = ts.getColumnData( table, wo.filter_formatter, column );
if ( ffxn ) {
wo.filter_formatterCount++;
buildFilter = ffxn( c.$filters.eq( column ), column );
// no element returned, so lets go find it
if ( buildFilter && buildFilter.length === 0 ) {
buildFilter = c.$filters.eq( column ).children( 'input' );
}
// element not in DOM, so lets attach it
if ( buildFilter && ( buildFilter.parent().length === 0 ||
( buildFilter.parent().length && buildFilter.parent()[0] !== c.$filters[column] ) ) ) {
c.$filters.eq( column ).append( buildFilter );
}
} else { } else {
buildFilter = $( '<input type="search">' ).appendTo( c.$filters.eq( column ) ); ffxn = ts.getColumnData( table, wo.filter_formatter, column );
if ( ffxn ) {
wo.filter_formatterCount++;
buildFilter = ffxn( $filter, column );
// no element returned, so lets go find it
if ( buildFilter && buildFilter.length === 0 ) {
buildFilter = $filter.children( 'input' );
}
// element not in DOM, so lets attach it
if ( buildFilter && ( buildFilter.parent().length === 0 ||
( buildFilter.parent().length && buildFilter.parent()[0] !== $filter[0] ) ) ) {
$filter.append( buildFilter );
}
} else {
buildFilter = $( '<input type="search">' ).appendTo( $filter );
}
if ( buildFilter ) {
tmp = $header.data( 'placeholder' ) ||
$header.attr( 'data-placeholder' ) ||
wo.filter_placeholder.search || '';
buildFilter.attr( 'placeholder', tmp );
}
} }
if ( buildFilter ) { if ( buildFilter ) {
tmp = $header.data( 'placeholder' ) || // add filter class name
$header.attr( 'data-placeholder' ) || name = ( $.isArray( wo.filter_cssFilter ) ?
wo.filter_placeholder.search || ''; ( typeof wo.filter_cssFilter[column] !== 'undefined' ? wo.filter_cssFilter[column] || '' : '' ) :
buildFilter.attr( 'placeholder', tmp ); wo.filter_cssFilter ) || '';
} buildFilter.addClass( tscss.filter + ' ' + name ).attr( 'data-column', column );
} if ( disabled ) {
if ( buildFilter ) { buildFilter.attr( 'placeholder', '' ).addClass( tscss.filterDisabled )[0].disabled = true;
// add filter class name }
name = ( $.isArray( wo.filter_cssFilter ) ?
( typeof wo.filter_cssFilter[column] !== 'undefined' ? wo.filter_cssFilter[column] || '' : '' ) :
wo.filter_cssFilter ) || '';
buildFilter.addClass( tscss.filter + ' ' + name ).attr( 'data-column', column );
if ( disabled ) {
buildFilter.attr( 'placeholder', '' ).addClass( tscss.filterDisabled )[0].disabled = true;
} }
} }
} }
@ -1261,7 +1271,7 @@
// ( '> -10' => '> -100' will ignore hidden rows ) // ( '> -10' => '> -100' will ignore hidden rows )
!( regex.isNeg1.test( val ) || regex.isNeg2.test( val ) ) && !( regex.isNeg1.test( val ) || regex.isNeg2.test( val ) ) &&
// if filtering using a select without a 'filter-match' class ( exact match ) - fixes #593 // if filtering using a select without a 'filter-match' class ( exact match ) - fixes #593
!( val !== '' && c.$filters && c.$filters.eq( indx ).find( 'select' ).length && !( val !== '' && c.$filters && c.$filters.filter( '[data-column="' + indx + '"]' ).find( 'select' ).length &&
!c.$headerIndexed[indx].hasClass( 'filter-match' ) ); !c.$headerIndexed[indx].hasClass( 'filter-match' ) );
} }
} }