From a9ec53ee1963fe69b05571e3d67a7698b78fd64b Mon Sep 17 00:00:00 2001 From: Rob Garrison Date: Fri, 4 Dec 2015 00:21:57 -0600 Subject: [PATCH] Core: After init, computeColumnIndex adds "data-column" to mismatch indexes This only occurs when the function is called externally and a `config` parameter is passed to the function, otherwise a "data-column" attribute is added to all cells. This change will minimize DOM manipulation. --- docs/index.html | 23 ++++++++++++++++++----- js/jquery.tablesorter.js | 20 ++++++++++++-------- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/docs/index.html b/docs/index.html index 20b40cf9..2a19d9b2 100644 --- a/docs/index.html +++ b/docs/index.html @@ -7089,14 +7089,18 @@ $.tablesorter.isDigit( "(2,345.67)" ); - Adds the correct data-column indexing to all rows passed to this function (v2.16). -

+ Adds the correct data-column indexing to all rows passed to this function (v2.16; v2.24.7). +
+

In v2.24.7, if a config parameter is included, this function will only add a "data-column" attribute to cells where their internal cellIndex doesn't match its actual column index. This does not apply to internal usage where a "data-column" attribute is set on all header & footer cells.

Use it as follows: -
$.tablesorter.computeColumnIndex($rows);
+
// In v2.24.7, if a config parameter is included "data-columns" are not added
+// to cells where their cellIndex and calculated column index match
+$.tablesorter.computeColumnIndex($rows, config);
    -
  • $rows - jQuery object of rows in which to add data-column indexes
  • +
  • $rows - jQuery object of rows in which to add data-column indexes.
  • +
  • config - this is the table.config (table configuration variables) object.
- Example result:
<tr>
+						Example result (without including config):
<tr>
 	<td colspan="2" data-column="0">r0c0</td>
 	<td data-column="2">r0c2</td>
 </tr>
@@ -7104,6 +7108,15 @@ $.tablesorter.isDigit( "(2,345.67)" );
<td data-column="0">r1c0</td> <td data-column="1">r1c1</td> <td data-column="2">r1c2</td> +</tr>
+ Example result (including config):
<tr>
+	<td colspan="2">r0c0</td> <!-- data-column="0" is not included because it matches the cellIndex property -->
+	<td data-column="2">r0c2</td>
+</tr>
+<tr>
+	<td>r1c0</td>
+	<td>r1c1</td>
+	<td>r1c2</td>
 </tr>
diff --git a/js/jquery.tablesorter.js b/js/jquery.tablesorter.js index 8e7155ed..e07e14b1 100644 --- a/js/jquery.tablesorter.js +++ b/js/jquery.tablesorter.js @@ -520,6 +520,7 @@ timer = new Date(); } // children tr in tfoot - see issue #196 & #547 + // don't pass table.config to computeColumnIndex here - widgets (math) pass it to "quickly" index tbody cells c.columns = ts.computeColumnIndex( c.$table.children( 'thead, tfoot' ).children( 'tr' ) ); // add icon if cssIcon option exists icon = c.cssIcon ? @@ -2088,17 +2089,17 @@ // computeTableHeaderCellIndexes from: // http://www.javascripttoolbox.com/lib/table/examples.php // http://www.javascripttoolbox.com/temp/table_cellindex.html - computeColumnIndex : function( $rows ) { - var i, j, k, l, $cell, cell, cells, rowIndex, cellId, rowSpan, colSpan, firstAvailCol, + computeColumnIndex : function( $rows, c ) { + var i, j, k, l, cell, cells, rowIndex, rowSpan, colSpan, firstAvailCol, + // total columns has been calculated, use it to set the matrixrow + columns = c && c.columns || 0, matrix = [], - matrixrow = []; + matrixrow = new Array( columns ); for ( i = 0; i < $rows.length; i++ ) { cells = $rows[ i ].cells; for ( j = 0; j < cells.length; j++ ) { cell = cells[ j ]; - $cell = $( cell ); rowIndex = cell.parentNode.rowIndex; - cellId = rowIndex + '-' + $cell.index(); rowSpan = cell.rowSpan || 1; colSpan = cell.colSpan || 1; if ( typeof matrix[ rowIndex ] === 'undefined' ) { @@ -2111,11 +2112,14 @@ break; } } - // add data-column (setAttribute = IE8+) - if ( cell.setAttribute ) { + if ( columns && cell.cellIndex === firstAvailCol ) { + // don't to anything + } else if ( cell.setAttribute ) { + // add data-column (setAttribute = IE8+) cell.setAttribute( 'data-column', firstAvailCol ); } else { - $cell.attr( 'data-column', firstAvailCol ); + // remove once we drop support for IE7 - 1/12/2016 + $( cell ).attr( 'data-column', firstAvailCol ); } for ( k = rowIndex; k < rowIndex + rowSpan; k++ ) { if ( typeof matrix[ k ] === 'undefined' ) {