Update isValueInArray function & sortAppend option. Fixes #523

This commit is contained in:
Mottie 2014-03-02 01:01:28 -06:00
parent 009c8c5f20
commit 0b91b7c586
3 changed files with 132 additions and 62 deletions

View File

@ -1905,10 +1905,8 @@ $(function(){
});</pre>
These external inputs have one requirement, they must have a <code>data-column="#"</code>, where the <code>#</code> targets the column (zero-based index), pointing to a specific column to search.<br>
<pre class="prettyprint lang-html">&lt;input class=&quot;search&quot; type=&quot;search&quot; data-column=&quot;0&quot; placeholder=&quot;Search first column&quot;&gt;</pre>
<br>
If you want to search all columns, using the updated "any match" method, set the data column value to "all":<br>
<pre class="prettyprint lang-html">&lt;input class=&quot;search&quot; type=&quot;search&quot; data-column=&quot;any&quot; placeholder=&quot;Search entire table&quot;&gt;</pre>
<br>
The updated any matching code will now automatically update all associated inputs with the latest search.
<br>
This option replaces <a href="#widget-filter-anymatch"><code>filter_anyMatch</code></a>.
@ -3075,7 +3073,7 @@ $.extend($.tablesorter.themes.jui, {
If <code>true</code>, child rows will be counted towards the pager set size (<span class="version">v2.13</span>).
<div class="collapsible">
<br>
Caution: when <code>true</code>, child row(s) may not appear to be attached to its parent row, may be split across pages or
<span class="label label-warning">*CAUTION*</span> When <code>true</code>, child row(s) may not appear to be attached to its parent row, may be split across pages or
may distort the table if rowspan or cellspans are included within the child row.<br>
<br>
If this option is <code>false</code>, child row(s) will always appear on the same page as its parent.
@ -3345,7 +3343,9 @@ var sort = [[0,0],[2,0]],
};
// Note that the sort value below is inside of another array (inside another set of square brackets)
// A callback method was added in 2.3.9.
$("table").trigger("sorton", [sort, callback]);</pre></div>
$("table").trigger("sorton", [sort, callback]);</pre>
<span class="label label-warning">*NOTE*</span> using this method to sort ignores the additions from the <a href="#sortforce"><code>sortForce</code></a> and <a href="#sortappend"><code>sortAppend</code></a> options.
</div>
</td>
<td><a href="example-trigger-sort.html">Example</a></td>
</tr>
@ -3354,9 +3354,12 @@ $("table").trigger("sorton", [sort, callback]);</pre></div>
<td><a href="#" class="permalink">sortReset</a></td>
<td>Use this method to reset the table to it's initial unsorted state (v2.4.7).
<div class="collapsible">
Don't confuse this method with the <a href="#sortreset"><code>sortReset</code> option</a>. This method immediately resets the entire table sort, while the option only resets the column sort after a third click.
<br>
This method immediately resets the entire table sort, while the option only resets the column sort after a third click.
<pre class="prettyprint lang-js">// Reset the table (make it unsorted)
$("table").trigger("sortReset");</pre></div>
$("table").trigger("sortReset");</pre>
<span class="label label-warning">*NOTE*</span> Don't confuse this method with the <a href="#sortreset"><code>sortReset</code> option</a>.
</div>
</td>
<td><a href="example-method-sortreset.html">Example</a></td>
</tr>
@ -3386,7 +3389,7 @@ var sorting = [[2,1],[0,0]];
$("table")
.trigger("update", [ false ])
.trigger("sorton", [sorting]);</pre>
Note: <code>updateRows</code> was added to work around the issue of using jQuery with the Prototype library. Triggering an "update" would make Prototype clear the tbody; Please see <a href="https://github.com/Mottie/tablesorter/issues/217">issue #217</a> for more details.
<span class="label label-info">NOTE</span> Don't confuse this method with the <a href="#sortreset"><code>sortReset</code> option</a>. <code>updateRows</code> was added to work around the issue of using jQuery with the Prototype library. Triggering an "update" would make Prototype clear the tbody; Please see <a href="https://github.com/Mottie/tablesorter/issues/217">issue #217</a> for more details.
</div>
</td>
<td><a href="example-ajax.html">Example</a></td>
@ -4602,7 +4605,7 @@ myArray.sort(function(a,b) { return $.tablesorter.sortText(a, b); });</pre>
<tr id="function-isvalueinarray">
<td><a href="#" class="permalink">isValueInArray</a></td>
<td>This function returns a boolean value if the value is contained within an array.
<td>This function returns a zero-based index value of the position of the value within the array, otherwise it returns -1 (Modified <span class="version updated">v2.15.6</span>).
<div class="collapsible"><br>
Use it as follows:
<pre class="prettyprint lang-js">$.tablesorter.isValueInArray(value, array);</pre>
@ -4611,9 +4614,10 @@ myArray.sort(function(a,b) { return $.tablesorter.sortText(a, b); });</pre>
<li><code>array</code> - array (sortList) to search for the value.</li>
</ul>
Sadly, this function has limited usefulness outside of tablesorter. It is only meant to search a <code>sortList</code> array and determine if a column (value) is already contained within it. Here is a basic example of how this function is used:
<pre class="prettyprint lang-js">var sortList = [ [0,0], [1,0] ];
// result: true
$.tablesorter.isValueInArray(1, sortList);</pre>
<pre class="prettyprint lang-js">var sortList = [ [1,0], [2,0], [0,0] ];
// result: 1
$.tablesorter.isValueInArray(2, sortList);</pre>
After <span class="version">v2.15.6</span>, this function returns a zero-based index of the position of the value within the array parameter, or -1 if the value is not in the array. Previously, this function returned a boolean value of <code>true</code> if the value was contained within the array, or <code>false</code> if not.
</div>
</td>
</tr>

View File

@ -582,92 +582,95 @@
return (parsers && parsers[i]) ? parsers[i].type || '' : '';
}
function initSort(table, cell, e){
var a, i, j, o, s,
function initSort(table, cell, event){
var arry, indx, col, order, s,
c = table.config,
k = !e[c.sortMultiSortKey],
$table = $(table);
key = !event[c.sortMultiSortKey],
$table = c.$table;
// Only call sortStart if sorting is enabled
$table.trigger("sortStart", table);
// get current column sort order
cell.count = e[c.sortResetKey] ? 2 : (cell.count + 1) % (c.sortReset ? 3 : 2);
cell.count = event[c.sortResetKey] ? 2 : (cell.count + 1) % (c.sortReset ? 3 : 2);
// reset all sorts on non-current column - issue #30
if (c.sortRestart) {
i = cell;
indx = cell;
c.$headers.each(function() {
// only reset counts on columns that weren't just clicked on and if not included in a multisort
if (this !== i && (k || !$(this).is('.' + ts.css.sortDesc + ',.' + ts.css.sortAsc))) {
if (this !== indx && (key || !$(this).is('.' + ts.css.sortDesc + ',.' + ts.css.sortAsc))) {
this.count = -1;
}
});
}
// get current column index
i = cell.column;
indx = cell.column;
// user only wants to sort on one column
if (k) {
if (key) {
// flush the sort list
c.sortList = [];
if (c.sortForce !== null) {
a = c.sortForce;
for (j = 0; j < a.length; j++) {
if (a[j][0] !== i) {
c.sortList.push(a[j]);
arry = c.sortForce;
for (col = 0; col < arry.length; col++) {
if (arry[col][0] !== indx) {
c.sortList.push(arry[col]);
}
}
}
// add column to sort list
o = cell.order[cell.count];
if (o < 2) {
c.sortList.push([i, o]);
order = cell.order[cell.count];
if (order < 2) {
c.sortList.push([indx, order]);
// add other columns if header spans across multiple
if (cell.colSpan > 1) {
for (j = 1; j < cell.colSpan; j++) {
c.sortList.push([i + j, o]);
for (col = 1; col < cell.colSpan; col++) {
c.sortList.push([indx + col, order]);
}
}
}
// multi column sorting
} else {
// get rid of the sortAppend before adding more - fixes issue #115
// get rid of the sortAppend before adding more - fixes issue #115 & #523
if (c.sortAppend && c.sortList.length > 1) {
if (ts.isValueInArray(c.sortAppend[0][0], c.sortList)) {
c.sortList.pop();
for (col = 0; col < c.sortAppend.length; col++) {
s = ts.isValueInArray(c.sortAppend[col][0], c.sortList);
if (s >= 0) {
c.sortList.splice(s,1);
}
}
}
// the user has clicked on an already sorted column
if (ts.isValueInArray(i, c.sortList)) {
if (ts.isValueInArray(indx, c.sortList) >= 0) {
// reverse the sorting direction
for (j = 0; j < c.sortList.length; j++) {
s = c.sortList[j];
o = c.$headers[s[0]];
if (s[0] === i) {
// o.count seems to be incorrect when compared to cell.count
s[1] = o.order[cell.count];
for (col = 0; col < c.sortList.length; col++) {
s = c.sortList[col];
order = c.$headers[s[0]];
if (s[0] === indx) {
// order.count seems to be incorrect when compared to cell.count
s[1] = order.order[cell.count];
if (s[1] === 2) {
c.sortList.splice(j,1);
o.count = -1;
c.sortList.splice(col,1);
order.count = -1;
}
}
}
} else {
// add column to sort list array
o = cell.order[cell.count];
if (o < 2) {
c.sortList.push([i, o]);
order = cell.order[cell.count];
if (order < 2) {
c.sortList.push([indx, order]);
// add other columns if header spans across multiple
if (cell.colSpan > 1) {
for (j = 1; j < cell.colSpan; j++) {
c.sortList.push([i + j, o]);
for (col = 1; col < cell.colSpan; col++) {
c.sortList.push([indx + col, order]);
}
}
}
}
}
if (c.sortAppend !== null) {
a = c.sortAppend;
for (j = 0; j < a.length; j++) {
if (a[j][0] !== i) {
c.sortList.push(a[j]);
arry = c.sortAppend;
for (col = 0; col < arry.length; col++) {
if (arry[col][0] !== indx) {
c.sortList.push(arry[col]);
}
}
}
@ -1032,7 +1035,7 @@
// get headers from the sortList
$h = $h.filter(function(){
// get data-column from attr to keep compatibility with jQuery 1.2.6
return this.sortDisabled ? false : ts.isValueInArray( parseFloat($(this).attr('data-column')), c.sortList);
return this.sortDisabled ? false : ts.isValueInArray( parseFloat($(this).attr('data-column')), c.sortList) >= 0;
});
}
$h.addClass(ts.css.processing + ' ' + c.cssProcessing);
@ -1291,14 +1294,14 @@
};
// *** utilities ***
ts.isValueInArray = function(v, a) {
var i, l = a.length;
for (i = 0; i < l; i++) {
if (a[i][0] === v) {
return true;
ts.isValueInArray = function(column, arry) {
var indx, len = arry.length;
for (indx = 0; indx < len; indx++) {
if (arry[indx][0] === column) {
return indx;
}
}
return false;
return -1;
};
ts.addParser = function(parser) {

View File

@ -22,7 +22,7 @@
OPTIONS:
cssAsc, cssChildRow, cssDesc, cssHeader, cssHeaderRow, cssInfoBlock, dateFormat, emptyTo, headerList,
headers, ignoreCase, initialized, parsers, sortList, sortLocaleCompare, sortReset, stringTo, tableClass,
usNumberFormat, widgets (just zebra)
usNumberFormat, widgets (just zebra), sortAppend, sortForce, sortMultiSortKey, sortResetKey
METHODS:
addRows, applyWidgets, destroy, sorton, sortReset, update/updateRow, updateAll, updateCell
@ -35,7 +35,7 @@
OPTIONS:
cancelSelection, cssIcon, cssProcessing, debug, delayInit, headerTemplate, initWidgets, onRenderHeader,
onRenderTemplate, selectorHeaders, selectorRemove, selectorSort, serverSideSorting, showProcessing,
sortAppend, sortForce, sortInitialOrder, sortMultiSortKey, sortResetKey, sortRestart, strings,
sortInitialOrder, sortRestart, strings,
textExtraction, textSorter, theme, widthFixed, widgets (also need priority testing)
METHODS:
@ -51,9 +51,11 @@
$table1 = $('.tester:eq(0)'),
$table2 = $('.tester:eq(1)'),
$table3 = $('.tester:eq(2)'),
$table4 = $('.tester:eq(3)'),
table1 = $table1[0],
table2 = $table2[0],
table3 = $table3[0],
table4 = $table4[0],
th0 = $table1.find('th')[0], // first table header cell
init = false,
sortIndx = 0,
@ -63,7 +65,7 @@
returnTime = function(string){
return new Date(string).getTime();
},
undef, c1, c2, c3, e, i, l, t;
undef, c1, c2, c3, c4, e, i, l, t;
$table1
.bind('tablesorter-initialized', function(){
@ -97,6 +99,21 @@
}
});
$table4.tablesorter({
sortAppend : [[2,0],[3,0]],
sortForce : [[0,0],[1,0]],
initialized: function(table){
var e, c = table.config;
// trigger sort on 5th column
// this method is used because triggering a "sorton" would ignore sortForce/sortAppend
c.$headers.eq(4).trigger('sort');
e = $.Event('sort');
e.which = 1;
e.shiftKey = true;
c.$headers.eq(5).trigger(e);
}
});
/************************************************
JSHint testing
************************************************/
@ -120,6 +137,7 @@
c1 = table1.config;
c2 = table2.config;
c3 = table3.config;
c4 = table4.config;
/************************************************
check isDigit function
@ -346,7 +364,7 @@
test( "sort Events", function(){
expect(1);
// table1 sorted twice in the above test; sortIndx = 6 (3 events x 2)
equal( sortIndx, 6, 'sortStart, sortBegin & sortComplet fired in order' );
equal( sortIndx, 6, 'sortStart, sortBegin & sortComplete fired in order x2' );
});
/************************************************
@ -404,6 +422,34 @@
equal( updateIndx, updateCallback, 'updatedComplete and update callback functions working properly' );
});
/************************************************
test sortForce, sortAppend, sortMultiSortKey and sortResetKey options
************************************************/
asyncTest( "sortForce, sortAppend, sortMultiSortKey & sortResetKey", function(){
expect(3);
var count = 0;
tester.cacheCompare( table4, 3, [ 2, 1, 7, 6, 5, 3, 4, 8, 9, 10 ], 'force x2 + sorted x2 + append x2, ascending' );
$table4.on('sortEnd', function(){
count++;
if (count === 1) {
tester.cacheCompare( table4, 3, [ 2, 1, 6, 7, 5, 4, 3, 8, 10, 9 ], 'force x2 + sorted x2 + append x2, descending' );
c4.sortResetKey = 'shiftKey';
var e = $.Event('sort');
e.which = 1;
e.shiftKey = true; // testing sortResetKey
c4.$headers.eq(0).trigger(e);
} else {
tester.cacheCompare( table4, 3, [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ], 'sortResetKey' );
$table4.off('sortEnd');
start();
}
});
var e = $.Event('sort');
c4.sortMultiSortKey = 'altKey';
e.which = 1;
e.altKey = true; // testing sortMultiSortKey
c4.$headers.eq(5).trigger(e);
});
/************************************************
check header css
@ -451,7 +497,6 @@
expect(7);
t = $table2.find('th:first');
e = jQuery._data(table2, 'events'); // get a list of all bound events
console.log(e);
equal( $.isEmptyObject(e), true, 'no events applied' );
equal( $table2.data().hasOwnProperty('tablesorter'), false, 'Data removed' );
equal( $table2.attr('class'), 'tester', 'All table classes removed' );
@ -552,5 +597,23 @@ console.log(e);
</tbody>
</table>
<table class="tester">
<thead>
<tr><th>1</th><th>2</th><th>3</th><th>4</th><th>5</th><th>6</th></tr>
</thead>
<tbody>
<tr><td>F01</td><td>F11</td><td>A21</td><td>01</td><td>ZZS</td><td>WA</td></tr>
<tr><td>F01</td><td>F11</td><td>A21</td><td>02</td><td>ZY</td><td>WB</td></tr>
<tr><td>F02</td><td>F12</td><td>A22</td><td>03</td><td>ZZ</td><td>WA</td></tr>
<tr><td>F02</td><td>F12</td><td>A22</td><td>04</td><td>ZZ</td><td>WB</td></tr>
<tr><td>F02</td><td>F12</td><td>A23</td><td>05</td><td>ZY</td><td>WA</td></tr>
<tr><td>F02</td><td>F12</td><td>A23</td><td>06</td><td>ZX</td><td>WB</td></tr>
<tr><td>F02</td><td>F12</td><td>A23</td><td>07</td><td>ZX</td><td>WA</td></tr>
<tr><td>F02</td><td>F13</td><td>A23</td><td>08</td><td>ZZ</td><td>WB</td></tr>
<tr><td>F03</td><td>F13</td><td>A24</td><td>09</td><td>ZZ</td><td>WA</td></tr>
<tr><td>F03</td><td>F13</td><td>A24</td><td>10</td><td>ZZ</td><td>WB</td></tr>
</tbody>
</table>
</body>
</html>