Core: Target header cells for data-column. Fixes #1459

This commit is contained in:
Rob Garrison 2017-09-18 21:48:00 -05:00
parent b8c47b5077
commit ea5c242f04

View File

@ -461,9 +461,9 @@
downTarget = null; downTarget = null;
if ( core !== true ) { if ( core !== true ) {
$headers.addClass( namespace.slice( 1 ) + '_extra_headers' ); $headers.addClass( namespace.slice( 1 ) + '_extra_headers' );
tmp = $.fn.closest ? $headers.closest( 'table' )[ 0 ] : $headers.parents( 'table' )[ 0 ]; tmp = ts.getClosest( $headers, 'table' );
if ( tmp && tmp.nodeName === 'TABLE' && tmp !== table ) { if ( tmp.length && tmp[ 0 ].nodeName === 'TABLE' && tmp[ 0 ] !== table ) {
$( tmp ).addClass( namespace.slice( 1 ) + '_extra_table' ); $( tmp[ 0 ] ).addClass( namespace.slice( 1 ) + '_extra_table' );
} }
} }
tmp = ( c.pointerDown + ' ' + c.pointerUp + ' ' + c.pointerClick + ' sort keyup ' ) tmp = ( c.pointerDown + ' ' + c.pointerUp + ' ' + c.pointerClick + ' sort keyup ' )
@ -515,8 +515,7 @@
ts.buildCache( c ); ts.buildCache( c );
} }
// jQuery v1.2.6 doesn't have closest() // jQuery v1.2.6 doesn't have closest()
$cell = $.fn.closest ? $( this ).closest( 'th, td' ) : $cell = ts.getHeaderCell( $( this ) );
/TH|TD/.test( this.nodeName ) ? $( this ) : $( this ).parents( 'th, td' );
// reference original table headers and find the same cell // reference original table headers and find the same cell
// don't use $headers or IE8 throws an error - see #987 // don't use $headers or IE8 throws an error - see #987
temp = $headers.index( $cell ); temp = $headers.index( $cell );
@ -556,7 +555,7 @@
''; '';
// redefine c.$headers here in case of an updateAll that replaces or adds an entire header cell - see #683 // redefine c.$headers here in case of an updateAll that replaces or adds an entire header cell - see #683
c.$headers = $( $.map( c.$table.find( c.selectorHeaders ), function( elem, index ) { c.$headers = $( $.map( c.$table.find( c.selectorHeaders ), function( elem, index ) {
var configHeaders, header, column, template, tmp, var configHeaders, header, column, template, tmp, $th,
$elem = $( elem ); $elem = $( elem );
// ignore cell (don't add it to c.$headers) if row has ignoreRow class // ignore cell (don't add it to c.$headers) if row has ignoreRow class
if ( $elem.parent().hasClass( c.cssIgnoreRow ) ) { return; } if ( $elem.parent().hasClass( c.cssIgnoreRow ) ) { return; }
@ -582,7 +581,9 @@
if ( c.onRenderHeader ) { if ( c.onRenderHeader ) {
c.onRenderHeader.apply( $elem, [ index, c, c.$table ] ); c.onRenderHeader.apply( $elem, [ index, c, c.$table ] );
} }
column = parseInt( $elem.attr( 'data-column' ), 10 ); // data-column stored on th or td only
$th = ts.getHeaderCell( $elem );
column = parseInt( $th.attr( 'data-column' ), 10 );
elem.column = column; elem.column = column;
tmp = ts.getOrder( ts.getData( $elem, configHeaders, 'sortInitialOrder' ) || c.sortInitialOrder ); tmp = ts.getOrder( ts.getData( $elem, configHeaders, 'sortInitialOrder' ) || c.sortInitialOrder );
// this may get updated numerous times if there are multiple rows // this may get updated numerous times if there are multiple rows
@ -600,10 +601,9 @@
} }
// add cell to headerList // add cell to headerList
c.headerList[ index ] = elem; c.headerList[ index ] = elem;
$elem.addClass( ts.css.header + ' ' + c.cssHeader );
// add to parent in case there are multiple rows // add to parent in case there are multiple rows
$elem ts.getClosest( $elem, 'tr' )
.addClass( ts.css.header + ' ' + c.cssHeader )
.parent()
.addClass( ts.css.headerRow + ' ' + c.cssHeaderRow ) .addClass( ts.css.headerRow + ' ' + c.cssHeaderRow )
.attr( 'role', 'row' ); .attr( 'role', 'row' );
// allow keyboard cursor to focus on element // allow keyboard cursor to focus on element
@ -1161,10 +1161,29 @@
} }
}, },
// This function does NOT return closest if the $el matches the selector
getClosest : function( $el, selector ) {
return $.fn.closest ?
$el.closest( selector ) :
$el.parents( selector ).filter( ':first' );
},
getHeaderCell : function( $el ) {
// jQuery v1.2.6 doesn't have closest()
if ( $.fn.closest ) {
return $el.closest( 'th, td' );
}
return /TH|TD/.test( $el[0].nodeName ) ?
$el :
$el.parents( 'th, td' ).filter( ':first' );
},
// nextSort (optional), lets you disable next sort text // nextSort (optional), lets you disable next sort text
setColumnAriaLabel : function( c, $header, nextSort ) { setColumnAriaLabel : function( c, $header, nextSort ) {
if ( $header.length ) { if ( $header.length ) {
var column = parseInt( $header.attr( 'data-column' ), 10 ), var $th = ts.getHeaderCell( $header ),
// data-column always stored on the th/td
column = parseInt( $th.attr( 'data-column' ), 10 ),
vars = c.sortVars[ column ], vars = c.sortVars[ column ],
tmp = $header.hasClass( ts.css.sortAsc ) ? tmp = $header.hasClass( ts.css.sortAsc ) ?
'sortAsc' : 'sortAsc' :
@ -1322,10 +1341,9 @@
$cell = $( cell ), $cell = $( cell ),
// update cache - format: function( s, table, cell, cellIndex ) // update cache - format: function( s, table, cell, cellIndex )
// no closest in jQuery v1.2.6 // no closest in jQuery v1.2.6
tbodyIndex = $tbodies tbodyIndex = $tbodies.index( ts.getClosest( $cell, 'tbody' ) ),
.index( $.fn.closest ? $cell.closest( 'tbody' ) : $cell.parents( 'tbody' ).filter( ':first' ) ),
tbcache = c.cache[ tbodyIndex ], tbcache = c.cache[ tbodyIndex ],
$row = $.fn.closest ? $cell.closest( 'tr' ) : $cell.parents( 'tr' ).filter( ':first' ); $row = ts.getClosest( $cell, 'tr' );
cell = $cell[ 0 ]; // in case cell is a jQuery object cell = $cell[ 0 ]; // in case cell is a jQuery object
// tbody may not exist if update is initialized while tbody is removed for processing // tbody may not exist if update is initialized while tbody is removed for processing
if ( $tbodies.length && tbodyIndex >= 0 ) { if ( $tbodies.length && tbodyIndex >= 0 ) {
@ -1380,11 +1398,13 @@
if ( valid ) { if ( valid ) {
$row = $( $row ); $row = $( $row );
c.$tbodies.append( $row ); c.$tbodies.append( $row );
} else if ( !$row || } else if (
!$row ||
// row is a jQuery object? // row is a jQuery object?
!( $row instanceof jQuery ) || !( $row instanceof jQuery ) ||
// row contained in the table? // row contained in the table?
( $.fn.closest ? $row.closest( 'table' )[ 0 ] : $row.parents( 'table' )[ 0 ] ) !== c.table ) { ( ts.getClosest( $row, 'table' )[ 0 ] !== c.table )
) {
if ( c.debug ) { if ( c.debug ) {
console.error( 'addRows method requires (1) a jQuery selector reference to rows that have already ' + console.error( 'addRows method requires (1) a jQuery selector reference to rows that have already ' +
'been added to the table, or (2) row HTML string to be added to a table with only one tbody' ); 'been added to the table, or (2) row HTML string to be added to a table with only one tbody' );
@ -1531,10 +1551,10 @@
notMultiSort = !event[ c.sortMultiSortKey ], notMultiSort = !event[ c.sortMultiSortKey ],
table = c.table, table = c.table,
len = c.$headers.length, len = c.$headers.length,
// get current column index // get current column index; *always* stored on th/td
col = parseInt( $( cell ).attr( 'data-column' ), 10 ), $th = ts.getHeaderCell( $( cell ) ),
col = parseInt( $th.attr( 'data-column' ), 10 ),
order = c.sortVars[ col ].order; order = c.sortVars[ col ].order;
// Only call sortStart if sorting is enabled // Only call sortStart if sorting is enabled
c.$table.triggerHandler( 'sortStart', table ); c.$table.triggerHandler( 'sortStart', table );
// get current column sort order // get current column sort order