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.
This commit is contained in:
Rob Garrison 2015-12-04 00:21:57 -06:00
parent f6012b4e76
commit a9ec53ee19
2 changed files with 30 additions and 13 deletions

View File

@ -7089,14 +7089,18 @@ $.tablesorter.isDigit( &quot;(2,345.67)&quot; );</pre>
<tr id="function-computecolumnindex">
<td><a href="#" class="permalink">computeColumnIndex</a></td>
<td>Adds the correct <code>data-column</code> indexing to all rows passed to this function (<span class="version">v2.16</span>).
<div class="collapsible"><br>
<td>Adds the correct <code>data-column</code> indexing to all rows passed to this function (<span class="version">v2.16</span>; <span class="version updated">v2.24.7</span>).
<div class="collapsible">
<p>In <span class="version">v2.24.7</span>, if a <code>config</code> parameter is included, this function will only add a "data-column" attribute to cells where their internal <code>cellIndex</code> doesn't match its actual column index. This does not apply to internal usage where a "data-column" attribute is set on all header &amp; footer cells.</p>
Use it as follows:
<pre class="prettyprint lang-js">$.tablesorter.computeColumnIndex($rows);</pre>
<pre class="prettyprint lang-js">// 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);</pre>
<ul>
<li><code>$rows</code> - jQuery object of rows in which to add data-column indexes</li>
<li><code>$rows</code> - jQuery object of rows in which to add data-column indexes.</li>
<li><code>config</code> - this is the <code>table.config</code> (table configuration variables) object.</li>
</ul>
Example result:<pre class="prettyprint lang-html">&lt;tr&gt;
Example result (without including <code>config</code>):<pre class="prettyprint lang-html">&lt;tr&gt;
&lt;td colspan=&quot;2&quot; data-column=&quot;0&quot;&gt;r0c0&lt;/td&gt;
&lt;td data-column=&quot;2&quot;&gt;r0c2&lt;/td&gt;
&lt;/tr&gt;
@ -7104,6 +7108,15 @@ $.tablesorter.isDigit( &quot;(2,345.67)&quot; );</pre>
&lt;td data-column=&quot;0&quot;&gt;r1c0&lt;/td&gt;
&lt;td data-column=&quot;1&quot;&gt;r1c1&lt;/td&gt;
&lt;td data-column=&quot;2&quot;&gt;r1c2&lt;/td&gt;
&lt;/tr&gt;</pre>
Example result (including <code>config</code>):<pre class="prettyprint lang-html">&lt;tr&gt;
&lt;td colspan=&quot;2&quot;&gt;r0c0&lt;/td&gt; &lt;!-- data-column=&quot;0&quot; is not included because it matches the cellIndex property --&gt;
&lt;td data-column=&quot;2&quot;&gt;r0c2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;r1c0&lt;/td&gt;
&lt;td&gt;r1c1&lt;/td&gt;
&lt;td&gt;r1c2&lt;/td&gt;
&lt;/tr&gt;</pre>
</div>
</td>

View File

@ -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' ) {