From bf94978dd2631fee00fb9d158fc45b181e2701dd Mon Sep 17 00:00:00 2001 From: Mottie Date: Tue, 13 May 2014 10:45:32 -0500 Subject: [PATCH] Core: new sorton values "a", "d", "n", "s" & "o" --- docs/css/jq.css | 5 +- docs/example-trigger-sort.html | 254 ++++++++++++++++++++++----------- docs/index.html | 2 +- js/jquery.tablesorter.js | 39 ++++- testing/testing.js | 22 +++ 5 files changed, 227 insertions(+), 95 deletions(-) diff --git a/docs/css/jq.css b/docs/css/jq.css index a47e3060..58fc1ed2 100644 --- a/docs/css/jq.css +++ b/docs/css/jq.css @@ -93,7 +93,6 @@ table th a { text-decoration: underline; color: #000; } #group .ui-slider,#align .ui-slider { width: 100px; height: 3px; font-size: 0.8em; display: inline-block; margin-left: 10px; } #group .demo-label,#align .demo-label { display: inline-block; min-width: 120px; } #align .demo-label { display: inline-block; min-width: 150px; } -@media all and (max-width: 650px) { - table.compatibility { float: none; } - .box { width: 100%; } +@media all and (max-width: 700px) { + table.compatibility, div.box { float: none; width: 100%; margin: 0; } } diff --git a/docs/example-trigger-sort.html b/docs/example-trigger-sort.html index 8e4d3de0..e2c8d999 100644 --- a/docs/example-trigger-sort.html +++ b/docs/example-trigger-sort.html @@ -12,39 +12,78 @@ + - + + +

Demo

-
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
First NameLast NameAgeTotalDiscountDate
PeterParker28$9.9920%Jul 6, 2006 8:14 AM
JohnHood33$19.9925%Dec 10, 2002 5:14 AM
ClarkKent18$15.8944%Jan 12, 2003 11:14 AM
BruceAlmighty45$153.1944%Jan 18, 2001 9:12 AM
BruceEvans22$13.1911%Jan 18, 2007 9:12 AM
-Sort first and third columns | -Toggle sort on the "Discount" column (v2.9) +
+ + +
+
+ + +

Sort Ascending/Descending

+ Use 0 or "a" for ascending sorts, and 1 or "d" for descending sorts*
+
+ ([[0,0]]) ([[0,"a"],[1,"d"]])
+ ([[0,1]]) ([[0,"d"],[1,"a"]]) +
$("table").trigger("sorton", [ [] ]);
+ * "a" & "d" values added v2.18.5. +
+
+ + + + + + + + + + + +
First NameLast NameAgeTotalDiscountDate
PeterParker28$9.9920%Jul 6, 2006 8:14 AM
JohnHood33$19.9925%Dec 10, 2002 5:14 AM
ClarkKent18$15.8944%Jan 12, 2003 11:14 AM
BruceAlmighty45$153.1944%Jan 18, 2001 9:12 AM
BruceEvans22$13.1911%Jan 18, 2007 9:12 AM
+
+
-

Javascript

-
-

-	
-

HTML

-
-

-	
+ +
+
+ + +

Toggle sort (Next)

+ Either trigger a "sort" on the desired column, or "sorton" on the table using the "n" value*.
+ Toggle the option (false), then try these sorts (click a third time).
+ +
+ Trigger "sort" event: (v2.9) +
$("#table2").find("th:contains(Discount)").trigger("sort");
+ + ([[0,"n"]])
+ ([[0,"n"],[1,"n"]]; columns are independent) +
$("table").trigger("sorton", [ [] ]);
+ * "n" value added v2.18.5. +
+
+ + + + + + + + + + + +
First NameLast NameAgeTotalDiscountDate
PeterParker28$9.9920%Jul 6, 2006 8:14 AM
JohnHood33$19.9925%Dec 10, 2002 5:14 AM
ClarkKent18$15.8944%Jan 12, 2003 11:14 AM
BruceAlmighty45$153.1944%Jan 18, 2001 9:12 AM
BruceEvans22$13.1911%Jan 18, 2007 9:12 AM
+
+
+
+ + +
+
+ +

Sort Same/Opposite

+ The same ("s") or opposite ("o") sort values always set the column sort based on the primary column*.
+ Toggle the option (false), then try these sorts (click a third time).
+
+ ([[0,"s"]]; always defaults to Asc sort if set on primary column)
+ ([[0,0],[1,"s"]])
+ ([[0,1],[1,"s"]])
+ ([[0,"n"],[1,"s"]])
+ ([[0,"n"],[1,"o"]])
+ ([[0,"n"],[1,"o"],[2,"n"]])
+ ([[0,"n"],[1,"o"],[2,"s"]])
+ ([[0,"n"],[1,"o"],[2,"s"],[3,"o"]]) +
$("table").trigger("sorton", [ [] ]);
+ * "s" & "o" values added v2.18.5. +
+
+ + + + + + + + + + + +
First NameLast NameAgeTotalDiscountDate
PeterParker28$9.9920%Jul 6, 2006 8:14 AM
JohnHood33$19.9925%Dec 10, 2002 5:14 AM
ClarkKent18$15.8944%Jan 12, 2003 11:14 AM
BruceAlmighty45$153.1944%Jan 18, 2001 9:12 AM
BruceEvans22$13.1911%Jan 18, 2007 9:12 AM
+
+
+
-
diff --git a/docs/index.html b/docs/index.html index 12c9febc..9f8ca36c 100644 --- a/docs/index.html +++ b/docs/index.html @@ -360,7 +360,7 @@
  • Change the default multi-sorting key (sortMultiSortKey)
  • Custom sort script (textSorter; added v2.2; updated v2.12)
  • Sorting Accented Characters (sortLocaleCompare; v2.24; languages)
  • -
  • Sort table using a link outside the table (external link)
  • +
  • Sort table using a link outside the table (external link; v2.16.5)
  • Attach child rows (rows that sort with their parent row) (v2.15.12)
  • Use child rows + filter widget (v2.15.12)
  • Sorting with Multiple Tbodies (v2.2)
  • diff --git a/js/jquery.tablesorter.js b/js/jquery.tablesorter.js index 705c85f0..7fbef727 100644 --- a/js/jquery.tablesorter.js +++ b/js/jquery.tablesorter.js @@ -546,19 +546,46 @@ } } - function updateHeaderSortCount(table, list, triggered) { - var s, t, o, c = table.config, + function updateHeaderSortCount(table, list) { + var s, t, o, col, primary, + c = table.config, sl = list || c.sortList; c.sortList = []; $.each(sl, function(i,v){ // ensure all sortList values are numeric - fixes #127 - s = [ parseInt(v[0], 10), parseInt(v[1], 10) ]; + col = parseInt(v[0], 10); // make sure header exists - o = c.$headers.filter('[data-column="' + s[0] + '"]:last')[0]; + o = c.$headers.filter('[data-column="' + col + '"]:last')[0]; if (o) { // prevents error if sorton array is wrong + // o.count = o.count + 1; + t = ('' + v[1]).match(/^(1|d|s|o|n)/); + t = t ? t[0] : ''; + // 0/(a)sc (default), 1/(d)esc, (s)ame, (o)pposite, (n)ext + switch(t) { + case '1': case 'd': // descending + t = 1; + break; + case 's': // same direction (as primary column) + // if primary sort is set to "s", make it ascending + t = primary || 0; + break; + case 'o': + s = o.order[(primary || 0) % (c.sortReset ? 3 : 2)]; + // opposite of primary column; but resets if primary resets + t = s === 0 ? 1 : s === 1 ? 0 : 2; + break; + case 'n': + o.count = o.count + 1; + t = o.order[(o.count) % (c.sortReset ? 3 : 2)]; + break; + default: // ascending + t = 0; + break; + } + primary = i === 0 ? t : primary; + s = [ col, parseInt(t, 10) || 0 ]; c.sortList.push(s); t = $.inArray(s[1], o.order); // fixes issue #167 - if (triggered) { o.count = o.count + 1; } o.count = t >= 0 ? t : s[1] % (c.sortReset ? 3 : 2); } }); @@ -872,7 +899,7 @@ e.stopPropagation(); $table.trigger("sortStart", this); // update header count index - updateHeaderSortCount(table, list, true); + updateHeaderSortCount(table, list); // set css for headers setHeadersCss(table); // fixes #346 diff --git a/testing/testing.js b/testing/testing.js index 33b4ec54..c4968955 100644 --- a/testing/testing.js +++ b/testing/testing.js @@ -466,6 +466,28 @@ $(function(){ }); + test( "sorton methods", function(){ + expect(6); + + $table3.trigger('sorton', [[[ 0,'d' ]]]); + equal( c3.sortList + '', '0,1', 'sorton desc [0,"d"]' ); + + $table3.trigger('sorton', [[[ 0,'a' ]]]); + equal( c3.sortList + '', '0,0', 'sorton asc [0,"a"]' ); + + $table3.trigger('sorton', [[[ 0,'n' ]]]); + equal( c3.sortList + '', '0,1', 'sorton next [0,"n"]' ); + $table3.trigger('sorton', [[[ 0,'n' ]]]); + equal( c3.sortList + '', '0,0', 'sorton next [0,"n"]' ); + + $table3.trigger('sorton', [[ [ 0,'n'], [1,'o'], [2,'s'] ]]); + equal( c3.sortList + '', '0,1,1,0,2,1', 'sorton next/opposite/same [0,"n"],[1,"o"],[2,"s"]' ); + + $table3.trigger('sorton', [[ [ 0,'n'], [1,'o'], [2,'s'] ]]); + equal( c3.sortList + '', '0,0,1,1,2,0', 'sorton next/opposite/same [0,"n"],[1,"o"],[2,"s"]' ); + + }); + test( "sort Events", function(){ expect(1);