From 70b33ad7e942c093867849a67de689f3a4e5e8fa Mon Sep 17 00:00:00 2001 From: virgile-hogman Date: Tue, 3 May 2016 02:14:35 +0200 Subject: [PATCH 1/2] Update sorttable.js 1. Fixed issue where sort would work only once for each column, after updating table content dynamically. Reason: multiple handlers were stacked if makesortable was called several times on same table (or when makesortable called before init). 2. Fixed issue where "num %" values would not be sorted correctly due to space chars before %. 3. Reverse sort activated by default (using mostly percentages). --- sorttable.js | 156 +++++++++++++++++++++++++++------------------------ 1 file changed, 82 insertions(+), 74 deletions(-) diff --git a/sorttable.js b/sorttable.js index ba17e5f..14a8a59 100644 --- a/sorttable.js +++ b/sorttable.js @@ -1,6 +1,6 @@ /* SortTable - version 2 + version 2 - Virgile Högman patch 2.1.0 7th April 2007 Stuart Langridge, http://www.kryogenix.org/code/browser/sorttable/ @@ -89,87 +89,96 @@ sorttable = { // make it clickable to sort headrow[i].sorttable_columnindex = i; headrow[i].sorttable_tbody = table.tBodies[0]; - dean_addEvent(headrow[i],"click", sorttable.innerSortFunction = function(e) { - - if (this.className.search(/\bsorttable_sorted\b/) != -1) { - // if we're already sorted by this column, just - // reverse the table, which is quicker - sorttable.reverse(this.sorttable_tbody); - this.className = this.className.replace('sorttable_sorted', - 'sorttable_sorted_reverse'); - this.removeChild(document.getElementById('sorttable_sortfwdind')); - sortrevind = document.createElement('span'); - sortrevind.id = "sorttable_sortrevind"; - sortrevind.innerHTML = stIsIE ? ' 5' : ' ▴'; - this.appendChild(sortrevind); - return; - } - if (this.className.search(/\bsorttable_sorted_reverse\b/) != -1) { - // if we're already sorted by this column in reverse, just - // re-reverse the table, which is quicker - sorttable.reverse(this.sorttable_tbody); - this.className = this.className.replace('sorttable_sorted_reverse', - 'sorttable_sorted'); - this.removeChild(document.getElementById('sorttable_sortrevind')); - sortfwdind = document.createElement('span'); - sortfwdind.id = "sorttable_sortfwdind"; - sortfwdind.innerHTML = stIsIE ? ' 6' : ' ▾'; - this.appendChild(sortfwdind); - return; - } - - // remove sorttable_sorted classes - theadrow = this.parentNode; - forEach(theadrow.childNodes, function(cell) { - if (cell.nodeType == 1) { // an element - cell.className = cell.className.replace('sorttable_sorted_reverse',''); - cell.className = cell.className.replace('sorttable_sorted',''); - } - }); - sortfwdind = document.getElementById('sorttable_sortfwdind'); - if (sortfwdind) { sortfwdind.parentNode.removeChild(sortfwdind); } - sortrevind = document.getElementById('sorttable_sortrevind'); - if (sortrevind) { sortrevind.parentNode.removeChild(sortrevind); } - - this.className += ' sorttable_sorted'; - sortfwdind = document.createElement('span'); - sortfwdind.id = "sorttable_sortfwdind"; - sortfwdind.innerHTML = stIsIE ? ' 6' : ' ▾'; - this.appendChild(sortfwdind); - - // build an array to sort. This is a Schwartzian transform thing, - // i.e., we "decorate" each row with the actual sort key, - // sort based on the sort keys, and then put the rows back in order - // which is a lot faster because you only do getInnerText once per row - row_array = []; - col = this.sorttable_columnindex; - rows = this.sorttable_tbody.rows; - for (var j=0; j5' : ' ▴'; + this.appendChild(sortrevind); + return; + } + if (this.className.search(/\bsorttable_sorted_reverse\b/) != -1) { + // if we're already sorted by this column in reverse, just + // re-reverse the table, which is quicker + sorttable.reverse(this.sorttable_tbody); + this.className = this.className.replace('sorttable_sorted_reverse', + 'sorttable_sorted'); + this.removeChild(document.getElementById('sorttable_sortrevind')); + sortfwdind = document.createElement('span'); + sortfwdind.id = "sorttable_sortfwdind"; + sortfwdind.innerHTML = stIsIE ? ' 6' : ' ▾'; + this.appendChild(sortfwdind); + return; + } + + // remove sorttable_sorted classes + theadrow = this.parentNode; + forEach(theadrow.childNodes, function(cell) { + if (cell.nodeType == 1) { // an element + cell.className = cell.className.replace('sorttable_sorted_reverse',''); + cell.className = cell.className.replace('sorttable_sorted',''); + } + }); + sortfwdind = document.getElementById('sorttable_sortfwdind'); + if (sortfwdind) { sortfwdind.parentNode.removeChild(sortfwdind); } + sortrevind = document.getElementById('sorttable_sortrevind'); + if (sortrevind) { sortrevind.parentNode.removeChild(sortrevind); } + + this.className += ' sorttable_sorted'; + sortfwdind = document.createElement('span'); + sortfwdind.id = "sorttable_sortfwdind"; + sortfwdind.innerHTML = stIsIE ? ' 6' : ' ▾'; + this.appendChild(sortfwdind); + + // build an array to sort. This is a Schwartzian transform thing, + // i.e., we "decorate" each row with the actual sort key, + // sort based on the sort keys, and then put the rows back in order + // which is a lot faster because you only do getInnerText once per row + row_array = []; + col = this.sorttable_columnindex; + rows = this.sorttable_tbody.rows; + for (var j=0; j Date: Tue, 3 May 2016 17:27:56 +0200 Subject: [PATCH 2/2] Update sorttable.js retro-fit current github version and remove reverse patch --- sorttable.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/sorttable.js b/sorttable.js index 14a8a59..58d2012 100644 --- a/sorttable.js +++ b/sorttable.js @@ -1,6 +1,6 @@ /* SortTable - version 2 - Virgile Högman patch 2.1.0 + version 2 - Virgile Högman patch 2.2.0 7th April 2007 Stuart Langridge, http://www.kryogenix.org/code/browser/sorttable/ @@ -89,9 +89,12 @@ sorttable = { // make it clickable to sort headrow[i].sorttable_columnindex = i; headrow[i].sorttable_tbody = table.tBodies[0]; - // HerrVigg - // if makesortable is called several times on same table, different handlers were stacked! + // patch Virgile Högman - repeated handlers + // if makesortable is called several times on same table, + // different handlers could be stacked and cancel the sort! // innerSort function must be unique for this instance of sorttable + // and therefore stored in object (anonymous function can't be used) + // so that addListener will not register same handler again // see section "Multiple identical event listeners" here: //https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener dean_addEvent(headrow[i],"click", sorttable.innerSort); @@ -160,8 +163,6 @@ sorttable = { //sorttable.shaker_sort(row_array, this.sorttable_sortfunction); /* and comment out this one */ row_array.sort(this.sorttable_sortfunction); - /* HerrVigg reverse order on first click */ - row_array.reverse(); tb = this.sorttable_tbody; for (var j=0; j