fixed sortRestart

This commit is contained in:
Rob Garrison 2012-03-08 07:28:07 -06:00
parent 2103da4a8e
commit 384cf9710f
5 changed files with 157 additions and 23 deletions

View File

@ -0,0 +1,136 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>jQuery plugin: Tablesorter 2.0 - Reset/Restart the sort</title>
<!-- jQuery -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<!-- Demo stuff -->
<link rel="stylesheet" href="css/jq.css">
<script src="js/chili/jquery.chili-2.2.js"></script>
<script src="js/chili/recipes.js"></script>
<script src="js/docs.js"></script>
<!-- Tablesorter: required -->
<link rel="stylesheet" href="../css/blue/style.css">
<script src="../js/jquery.tablesorter.js"></script>
<script id="js">$(function() {
// call the tablesorter plugin
$("table").tablesorter({
sortReset : true,
// Resets the sort direction so that clicking on an unsorted column will sort in the sortInitialOrder direction.
sortRestart : true,
sortInitialOrder: 'desc',
headers : {
// 0 : { lockedOrder: 'asc' }, only allow sorting in one direction
3 : { sortInitialOrder: 'asc' },
4 : { sortInitialOrder: 'asc' },
5 : { sortInitialOrder: 'asc' }
}
});
});</script>
</head>
<body>
<div id="banner">
<h1>table<em>sorter</em></h1>
<h2>Reset/Restart the sort</h2>
<h3>Flexible client-side table sorting</h3>
<a href="index.html">Back to documentation</a>
</div>
<div id="main">
<p class="tip">
<em>NOTE!</em>
<ul>
<li>The default <code class="hilight">sortInitialOrder</code> for the entire table is set to <code class="hilight">desc</code> (descending sort).</li>
<li>The last three columns have the initial sort order set in the ascending direction, using the headers option.</li>
<li>Test the <code class="hilight">sortReset</code> option by clicking on any column at least three times. It should sort in the <code class="hilight">sortInitialOrder</code> direction on the first click, the opposite direction on the second click, then reset to the original sort order on the third click. This cycle repeats on subsequent sorts.</li>
<li>Test the <code class="hilight">sortRestart</code> option by clicking on any unsorted column to see that it will always restart from the <code class="hilight">sortInitialOrder</code>.</li>
</ul>
</p>
<h1>Demo</h1>
<div id="demo"><table class="tablesorter">
<thead>
<tr>
<th>First Name (desc)</th>
<th>Last Name (desc)</th>
<th>Age (desc)</th>
<th>Total (asc)</th>
<th>Discount (asc)</th>
<th>Date (asc)</th>
</tr>
</thead>
<tbody>
<tr>
<td>Peter</td>
<td>Parker</td>
<td>28</td>
<td>$9.99</td>
<td>20%</td>
<td>Jul 6, 2006 8:14 AM</td>
</tr>
<tr>
<td>John</td>
<td>Hood</td>
<td>33</td>
<td>$19.99</td>
<td>25%</td>
<td>Dec 10, 2002 5:14 AM</td>
</tr>
<tr>
<td>Clark</td>
<td>Kent</td>
<td>18</td>
<td>$15.89</td>
<td>44%</td>
<td>Jan 12, 2003 11:14 AM</td>
</tr>
<tr>
<td>Bruce</td>
<td>Almighty</td>
<td>45</td>
<td>$153.19</td>
<td>44%</td>
<td>Jan 18, 2001 9:12 AM</td>
</tr>
<tr>
<td>Bruce</td>
<td>Evans</td>
<td>22</td>
<td>$13.19</td>
<td>11%</td>
<td>Jan 18, 2007 9:12 AM</td>
</tr>
</tbody>
</table></div>
<h1>Javascript</h1>
<div id="javascript">
<pre class="js"></pre>
</div>
<h1>HTML</h1>
<div id="html">
<pre class="html"></pre>
</div>
<div class="next-up">
<hr />
Next up: <a href="example-option-sort-key.html">Change the default multi-sorting key &rsaquo;&rsaquo;</a>
</div>
</div>
</body>
</html>

View File

@ -114,7 +114,7 @@
<div class="next-up">
<hr />
Next up: <a href="example-option-sort-key.html">Change the default multi-sorting key &rsaquo;&rsaquo;</a>
Next up: <a href="example-option-sortreset-sortrestart.html">Using sortReset &amp; sortRestart &rsaquo;&rsaquo;</a>
</div>
</div>

View File

@ -17,8 +17,8 @@
<script>
$(function(){
$('table').tablesorter({
widgets: ['zebra', 'columns'],
sortReset: true
widgets : ['zebra', 'columns'],
sortReset : true
});
});
</script>

View File

@ -1,5 +1,5 @@
/*
* TableSorter 2.1 - Client-side table sorting with ease!
* TableSorter 2.1.1 - Client-side table sorting with ease!
* @requires jQuery v1.2.3
*
* Copyright (c) 2007 Christian Bach
@ -315,7 +315,8 @@
}
function formatSortingOrder(v) {
return (/^d/i.test(v) || v === 1) ? 1 : 0;
// look for "d" in "desc" order; return true
return (/^d/i.test(v) || v === 1);
}
function checkHeaderMetadata(cell) {
@ -348,12 +349,14 @@
.wrapInner("<span/>")
.each(function (index) {
this.column = header_index[this.parentNode.rowIndex + "-" + this.cellIndex];
this.order = formatSortingOrder( checkHeaderOrder(table, index) );
this.count = this.order;
this.order = formatSortingOrder( checkHeaderOrder(table, index) ) ? [1,0,2] : [0,1,2];
this.count = -1; // set to -1 because clicking on the header automatically adds one
if (checkHeaderMetadata(this) || checkHeaderOptions(table, index) || $(this).is('.sorter-false')) { this.sortDisabled = true; }
this.lockedOrder = false;
lock = checkHeaderLocked(table, index);
if (typeof(lock) !== 'undefined' && lock !== false) { this.order = this.lockedOrder = formatSortingOrder(lock); }
if (typeof(lock) !== 'undefined' && lock !== false) {
this.order = this.lockedOrder = formatSortingOrder(lock) ? [1,1,1] : [0,0,0];
}
if (!this.sortDisabled) {
$th = $(this).addClass(c.cssHeader);
if (c.onRenderHeader) { c.onRenderHeader.apply($th, [index]); }
@ -427,8 +430,7 @@
for (i = 0; i < l; i++) {
s = sortList[i];
o = c.headerList[s[0]];
o.count = s[1];
o.count++;
o.count = (s[1] + 1) % (c.sortReset ? 3 : 2);
}
}
@ -580,21 +582,19 @@
// store exp, for speed
$cell = $(this);
// get current column sort order
this.order = this.count++ % (c.sortReset ? 3 : 2);
this.count = (this.count + 1) % (c.sortReset ? 3 : 2);
// reset all sorts on non-current column - issue #30
if (c.sortRestart) {
i = this;
$headers.each(function(){
// only reset counts on columns that weren't just clicked on and if not included in a multisort
if (this !== i && (!$(this).is('.' + c.cssDesc + ',.' + c.cssAsc) || !e[c.sortMultiSortKey])) {
this.count = 0;
this.count = -1;
}
});
}
// get current column index
i = this.column;
// always sort on the locked order.
if(typeof(this.lockedOrder) !== "undefined" && this.lockedOrder !== false) { this.order = this.lockedOrder; }
// user only wants to sort on one column
if (!e[c.sortMultiSortKey]) {
// flush the sort list
@ -608,7 +608,7 @@
}
}
// add column to sort list
if (this.order < 2) { c.sortList.push([i, this.order]); }
if (this.order[this.count] < 2) { c.sortList.push([i, this.order[this.count]]); }
// multi column sorting
} else {
// the user has clicked on an already sorted column.
@ -618,18 +618,16 @@
s = c.sortList[j];
o = c.headerList[s[0]];
if (s[0] === i) {
o.count = s[1];
o.count++;
s[1] = o.count % (c.sortReset ? 3 : 2);
if (s[1] >= 2) {
s[1] = o.order[o.count]; // % (c.sortReset ? 3 : 2);
if (s[1] === 2) {
c.sortList.splice(j,1);
o.count = 0;
o.count = -1;
}
}
}
} else {
// add column to sort list array
if (this.order < 2) { c.sortList.push([i, this.order]); }
if (this.order[this.count] < 2) { c.sortList.push([i, this.order[this.count]]); }
}
}
if (c.sortAppend !== null) {

File diff suppressed because one or more lines are too long