Core: Add resort option

It is used as the fallback value when no resort parameter is included with the "updateAll", "update", "addRows" and "updateCell" methods. Also, modified the "updateCell" method to not reapply widgets if resort is false; this may resolve issue with widgets causing elements to lose focus
This commit is contained in:
Mottie 2015-01-28 23:27:20 -06:00
parent 828207b7fb
commit a031bcc9e0
3 changed files with 67 additions and 34 deletions

View File

@ -527,7 +527,7 @@
<li><a href="example-parsers-duration.html">Duration parser</a> (<span class="version">v2.17.8</span>).</li>
<li><a href="example-parsers-file-type.html">File type parser</a> (<span class="version">v2.13</span>).</li>
<li><a href="example-parsers-ignore-articles.html">Ignore leading articles parser</a> (Ignore &quot;A&quot;, &quot;An&quot; and &quot;The&quot; in titles) (<span class="version">v2.8</span>).</li>
<li><a href="example-widget-grouping.html">Input/select parsers</a> (used by Grouping rows widget) (<span class="version">v2.8</span>; <span class="version updated">v2.17.5</span>).</li>
<li><a href="example-widget-grouping.html">Input/select parsers</a> (used by Grouping rows widget) (<span class="version">v2.8</span>; <span class="version updated">v2.18.5</span>).</li>
<li><a href="example-parsers-feet-inch-fraction.html">Feet-inch-fraction parser</a> (<span class="version">v2.8</span>).</li>
<li><a href="example-parsers-metric.html">Metric parser</a> (<span class="version">v2.8</span>).</li>
<li><a href="example-parsers-named-numbers.html">Named Numbers parser</a> (<span class="version">v2.18.0</span>).</li>
@ -1215,6 +1215,25 @@ From the example function above, you'll end up with something similar to this HT
<td><a href="example-option-selectorsort.html">Example</a></td>
</tr>
<tr id="resort">
<td><a href="#" class="permalink">resort</a></td>
<td>Boolean</td>
<td>true</td>
<td>When this option is <code>true</code> any applied sort on the table will be reapplied after an update method (<span class="version">v2.18.5</span>).
<div class="collapsible">
<br>
Specifically, this option applies to the "updateAll", "update", "addRows" and "updateCell" methods and is checked after the method has completed updating the internal cache.<br>
<br>
If <code>false</code>, the widgets will still be refreshed for all but the "updateCell" method - this "updateCell" behavior was added in v2.18.5.<br>
<br>
<span class="label label-info">Note</span> when triggering one of the above methods, and passing a defined <code>resort</code> parameter, it will override this setting.<br>
<br>
<span class="label label-warning">Note</span> if a sort is not reapplied, problems with some widgets may occur namely the grouping widget.
</div>
</td>
<td></td>
</tr>
<tr id="serversidesorting">
<td><span class="permalink">serverSideSorting</span></td>
<td>Boolean</td>
@ -4292,7 +4311,8 @@ $.extend($.tablesorter.themes.jui, {
'&lt;td&gt;$19.99&lt;/td&gt;&lt;td&gt;15%&lt;/td&gt;&lt;td&gt;Sep 25, 1987 12:00PM&lt;/td&gt;&lt;/tr&gt;',
$row = $(row),
// resort table using true to reapply the current sort; set to false to prevent resort
// as of v2.18.5, the resort variable can contain a new sortList to be applied
// if undefined, the resort value will be obtained from config.resort (added v2.18.5)
// As of v2.18.5, the resort variable can contain a new sortList to be applied
// A callback method was added in 2.3.9.
resort = true, // or [[0,0],[1,0]] etc
callback = function(table){
@ -4377,7 +4397,7 @@ $("table").trigger("sortReset", [callback]);</pre>
<tr id="update">
<td><a href="#" class="permalink">update</a> / <a href="#" class="permalink">updateRows</a></td>
<td>Update the <code>tbody</code>'s stored data (<code>update</code> &amp; <code>updateRows</code> do exactly the same thing)
<td>Update the <code>tbody</code>'s stored data (<code>update</code> &amp; <code>updateRows</code> do exactly the same thing; <span class="version updated">v2.18.5</span>)
<div class="collapsible"><br>
<pre class="prettyprint lang-js">// Add new content
$("table tbody").append(html);
@ -4392,6 +4412,8 @@ var resort = true,
};
$("table").trigger("update", [resort, callback]);
// As of version 2.18.5, if the resort parameter is undefined, the setting from the config.resort will be used
// As of version 2.0.14, the table will automatically resort after the update (if the "resort" flag is true
// &amp; will use the current sort selection), so include the following if you want to specify a different sort
@ -4412,7 +4434,7 @@ $('table').trigger('update', [ sorting ]);</pre>
<tr id="updateall">
<td><a href="#" class="permalink">updateAll</a></td>
<td>Update a column of cells (<code>thead</code> and <code>tbody</code>) (<span class="version">v2.8</span>).
<td>Update a column of cells (<code>thead</code> and <code>tbody</code>) (<span class="version">v2.8</span>; <span class="version updated">v2.18.5</span>).
<div class="collapsible">
<pre class="prettyprint lang-js">// Change thead & tbody column of cells
// remember, "eq()" is zero based & "nth-child()" is 1 based
@ -4424,6 +4446,7 @@ $("table tbody").find('td:nth-child(3)').html(function(i,h){
// reapply the current sort if resort = true
// do not reapply the current sort if resort = false
// if undefined, resort will be obtained from config.resort (added v2.18.5)
// as of v2.18.5, apply a new sort if resort = [[0,0]] (or whatever)
// or the sort is reset if resort = []
var resort = true,
@ -4474,7 +4497,7 @@ $("table")
<tr id="updatecell">
<td><a href="#" class="permalink">updateCell</a></td>
<td>Update a table cell in the tablesorter data.
<td>Update a table cell in the tablesorter data (<span class="version updated">v2.18.5</span>).
<div class="collapsible">
<pre class="prettyprint lang-js">$(function() {
$("table").tablesorter();
@ -4483,6 +4506,7 @@ $("table")
// Do we want to reapply the current sort on the column?
// see updateRow for other resort settings as of v2.18.5
// if resort is undefined, the value from config.resort (added v2.18.5) will be used
var resort = false,
// Do something after the cell update in this callback function
callback = function(table){

View File

@ -56,6 +56,7 @@
usNumberFormat : true, // false for German "1.234.567,89" or French "1 234 567,89"
delayInit : false, // if false, the parsed table contents will not update until the first sort
serverSideSorting: false, // if true, server-side sorting should be performed because client-side sorting will be disabled, but the ui and events will still be used.
resort : true, // default setting to trigger a resort after an "update", "addRows", "updateCell", etc has completed
// *** sort options
headers : {}, // set sorter, string, empty, locked order, sortInitialOrder, filter, etc.
@ -823,10 +824,12 @@
}
function checkResort(c, resort, callback) {
var sl = $.isArray(resort) ? resort : c.sortList;
var sl = $.isArray(resort) ? resort : c.sortList,
// if no resort parameter is passed, fallback to config.resort (true by default)
resrt = typeof resort === 'undefined' ? c.resort : resort;
// don't try to resort if the table is still processing
// this will catch spamming of the updateCell method
if (resort !== false && !c.serverSideSorting && !c.table.isProcessing) {
if (resrt !== false && !c.serverSideSorting && !c.table.isProcessing) {
if (sl.length) {
c.$table.trigger('sorton', [sl, function(){
resortComplete(c, callback);
@ -906,7 +909,18 @@
// update column max value (ignore sign)
c.cache[tbdy].colMax[icell] = Math.max(Math.abs(v) || 0, c.cache[tbdy].colMax[icell] || 0);
}
checkResort(c, resort, callback);
v = resort !== 'undefined' ? resort : c.resort;
if (v !== false) {
// widgets will be reapplied
checkResort(c, v, callback);
} else {
// don't reapply widgets is resort is false, just in case it causes
// problems with element focus
if ($.isFunction(callback)) {
callback(table);
}
c.$table.trigger('updateComplete', c.table);
}
}
})
.bind("addRows" + c.namespace, function(e, $row, resort, callback) {

View File

@ -1,13 +1,12 @@
/*! input & select parsers for jQuery 1.7+ & tablesorter 2.7.11+
* Updated 9/15/2014 (v2.17.8)
* Updated 1/28/2015 (v2.18.5)
* Demo: http://mottie.github.com/tablesorter/docs/example-widget-grouping.html
*/
/*jshint browser: true, jquery:true, unused:false */
;(function($){
"use strict";
var resort = true, // resort table after update
updateServer = function(event, $table, $input){
var updateServer = function(event, $table, $input){
// do something here to update your server, if needed
// event = change event object
// $table = jQuery object of the table that was just updated
@ -98,12 +97,8 @@
// if this code interferes somehow, target the specific table $('#mytable'), instead of $('table')
$(function(){
$('table').on('tablesorter-initialized', function(){
// this flag prevents the updateCell event from being spammed
// it happens when you modify input text and hit enter
var focused = false,
restoreValue = function(isTbody){
// focused = false; // uncomment this line to prevent auto-accepting changes
// make sure we restore original values
var restoreValue = function(isTbody){
// make sure we restore original values (trigger blur)
// isTbody is needed to prevent the select from closing in IE
// see https://connect.microsoft.com/IE/feedbackdetail/view/962618/
if (isTbody) {
@ -117,7 +112,6 @@
restoreValue(e.target.tagName === 'TBODY');
})
.on('focus', 'select, input, textarea', function(){
focused = true;
$(this).data('ts-original-value', this.value);
})
.on('blur', 'input, textarea', function(){
@ -132,9 +126,10 @@
return;
}
// Update cell cache using... select: change, input: enter or textarea: alt + enter
if ( ( e.type === 'change' && focused ) ||
if ( ( e.type === 'change' ) ||
( e.type === 'keyup' && e.which === 13 && ( e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA' && e.altKey ) ) ) {
var $tar = $(e.target),
var undef,
$tar = $(e.target),
$cell = $tar.closest('td'),
$table = $cell.closest('table'),
indx = $cell[0].cellIndex,
@ -148,9 +143,9 @@
// ignore change event if nothing changed
if ($tar.val() !== $tar.data('ts-original-value')) {
$tar.data('ts-original-value', $tar.val());
$table.trigger('updateCell', [ $tar.closest('td'), resort, function(){
// pass undefined resort value so it falls back to config.resort setting
$table.trigger('updateCell', [ $tar.closest('td'), undef, function(){
updateServer(e, $table, $tar);
setTimeout(function(){ focused = false; }, 10);
} ]);
}
}