Core: Tweak internal sortVars & sortReset. Fixes #1137

This commit is contained in:
Rob Garrison 2016-07-26 01:30:22 -05:00
parent 996b3594bc
commit 5e67437604
No known key found for this signature in database
GPG Key ID: 0A42D160D71978E1
2 changed files with 71 additions and 13 deletions

View File

@ -6433,6 +6433,57 @@ $headers.each(function(){
<td></td>
</tr>
<tr id="variable-sort-vars">
<td><a href="#" class="permalink">config.sortVars</a></td>
<td>Object</td>
<td>Internally stored object of column specific sort variables (<span class="version">v2.24.0</span>; <span class="version updated">v2.27.0</span>)
<div class="collapsible">
<br>
This object is defined as follows:<br>
<pre class="prettyprint lang-js">// $('table')[0].config.sortVars -> result
[{
// column 0
count: 0, // click count; used as index for order value
order: [ 0, 1, 2 ], // default sort (sortReset: true)
lockedOrder: false // true if column order is locked (read only)
},{
// ...
},{
// column n
count: 0, // click count; used as index for order value
order: [ 0, 1, 2 ], // default sort (sortReset: true)
lockedOrder: false // true if column order is locked (read only)
}]</pre>
<ul>
<li><code>count</code>
<ul>
<li>The initial value is <code>-1</code>.</li>
<li>The value is incremented each time the user clicks on a header cell; but it resets to zero once it reaches the end of the order array.</li>
</ul>
</li>
<li><code>order</code>
<ul>
<li>An array that contains the sort sequence for each column.</li>
<li><code>0</code> indicates an ascending sort.</li>
<li><code>1</code> indicates a descending sort.</li>
<li><code>0</code> indicates a reset sort (initial unsorted rows).</li>
<li>Locked sorts will contain either <code>[0, 0]</code> or <code>[1, 1]</code> determined by the lockedOrder setting.</li>
<li>Setting these values to <code>[2, 2]</code> will lock in an unsorted column, but the unsort arrows will still be visible; use a <code>"sorter-false"</code> class instead.</li>
<li>The values of this order can only be modified <em>after</em> tablesorter has initialized.</li>
</ul>
</li>
<li><code>lockedOrder</code>
<ul>
<li>See the <a href="#headers">lockedOrder</a> setting.</li>
<li>It's readonly because it is set during initialization and not used again (future use by a widget?)</li>
</ul>
</li>
</ul>
</div>
</td>
<td></td>
</tr>
<tr id="variable-tbodies">
<td><a href="#" class="permalink">config.$tbodies</a></td>
<td>jQuery Object</td>

View File

@ -563,19 +563,19 @@
}
column = parseInt( $elem.attr( 'data-column' ), 10 );
elem.column = column;
tmp = 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
c.sortVars[ column ] = {
count : -1, // set to -1 because clicking on the header automatically adds one
order: ts.getOrder( tmp ) ?
[ 1, 0, 2 ] : // desc, asc, unsorted
[ 0, 1, 2 ], // asc, desc, unsorted
order: tmp ?
( c.sortReset ? [ 1, 0, 2 ] : [ 1, 0 ] ) : // desc, asc, unsorted
( c.sortReset ? [ 0, 1, 2 ] : [ 0, 1 ] ), // asc, desc, unsorted
lockedOrder : false
};
tmp = ts.getData( $elem, configHeaders, 'lockedOrder' ) || false;
if ( typeof tmp !== 'undefined' && tmp !== false ) {
c.sortVars[ column ].lockedOrder = true;
c.sortVars[ column ].order = ts.getOrder( tmp ) ? [ 1, 1, 1 ] : [ 0, 0, 0 ];
c.sortVars[ column ].order = ts.getOrder( tmp ) ? [ 1, 1 ] : [ 0, 0 ];
}
// add cell to headerList
c.headerList[ index ] = elem;
@ -1130,6 +1130,7 @@
setColumnAriaLabel : function( c, $header, nextSort ) {
if ( $header.length ) {
var column = parseInt( $header.attr( 'data-column' ), 10 ),
vars = c.sortVars[ column ],
tmp = $header.hasClass( ts.css.sortAsc ) ?
'sortAsc' :
$header.hasClass( ts.css.sortDesc ) ? 'sortDesc' : 'sortNone',
@ -1137,7 +1138,8 @@
if ( $header.hasClass( 'sorter-false' ) || nextSort === false ) {
txt += ts.language.sortDisabled;
} else {
nextSort = c.sortVars[ column ].order[ ( c.sortVars[ column ].count + 1 ) % ( c.sortReset ? 3 : 2 ) ];
tmp = ( vars.count + 1 ) % vars.order.length;
nextSort = vars.order[ tmp ];
// if nextSort
txt += ts.language[ nextSort === 0 ? 'nextAsc' : nextSort === 1 ? 'nextDesc' : 'nextNone' ];
}
@ -1196,7 +1198,12 @@
// set order if not already defined - due to colspan header without associated header cell
// adding this check prevents a javascript error
if ( !c.sortVars[ col ].order ) {
order = c.sortVars[ col ].order = ts.getOrder( c.sortInitialOrder ) ? [ 1, 0, 2 ] : [ 0, 1, 2 ];
if ( ts.getOrder( c.sortInitialOrder ) ) {
order = c.sortReset ? [ 1, 0, 2 ] : [ 1, 0 ];
} else {
order = c.sortReset ? [ 0, 1, 2 ] : [ 0, 1 ];
}
c.sortVars[ col ].order = order;
c.sortVars[ col ].count = 0;
}
@ -1213,12 +1220,12 @@
dir = primary || 0;
break;
case 'o' :
temp = order[ ( primary || 0 ) % ( c.sortReset ? 3 : 2 ) ];
temp = order[ ( primary || 0 ) % order.length ];
// opposite of primary column; but resets if primary resets
dir = temp === 0 ? 1 : temp === 1 ? 0 : 2;
break;
case 'n' :
dir = order[ ( ++c.sortVars[ col ].count ) % ( c.sortReset ? 3 : 2 ) ];
dir = order[ ( ++c.sortVars[ col ].count ) % order.length ];
break;
default : // ascending
dir = 0;
@ -1228,7 +1235,7 @@
group = [ col, parseInt( dir, 10 ) || 0 ];
c.sortList[ c.sortList.length ] = group;
dir = $.inArray( group[ 1 ], order ); // fixes issue #167
c.sortVars[ col ].count = dir >= 0 ? dir : group[ 1 ] % ( c.sortReset ? 3 : 2 );
c.sortVars[ col ].count = dir >= 0 ? dir : group[ 1 ] % order.length;
}
}
},
@ -1491,8 +1498,8 @@
// Only call sortStart if sorting is enabled
c.$table.triggerHandler( 'sortStart', table );
// get current column sort order
c.sortVars[ col ].count =
event[ c.sortResetKey ] ? 2 : ( c.sortVars[ col ].count + 1 ) % ( c.sortReset ? 3 : 2 );
tmp = ( c.sortVars[ col ].count + 1 ) % order.length;
c.sortVars[ col ].count = event[ c.sortResetKey ] ? 2 : tmp;
// reset all sorts on non-current column - issue #30
if ( c.sortRestart ) {
for ( headerIndx = 0; headerIndx < len; headerIndx++ ) {
@ -1587,7 +1594,7 @@
dir = tmp === 0 ? 1 : 0;
break;
case 'n' :
dir = ( tmp + 1 ) % ( c.sortReset ? 3 : 2 );
dir = ( tmp + 1 ) % order.length;
break;
default:
dir = 0;