From f788cc449469bd5db43daf6c997c948e9cad655d Mon Sep 17 00:00:00 2001 From: Marin Bezhanov Date: Tue, 21 Oct 2014 15:02:12 +0300 Subject: [PATCH 1/2] sorting of child rows --- jquery.tablesorter.js | 48 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/jquery.tablesorter.js b/jquery.tablesorter.js index c5eabf0..9d96667 100644 --- a/jquery.tablesorter.js +++ b/jquery.tablesorter.js @@ -119,6 +119,7 @@ sortForce: null, sortAppend: null, sortLocaleCompare: true, + sortChildren: false, textExtraction: "simple", parsers: {}, widgets: [], widgetZebra: { @@ -258,6 +259,10 @@ row: [], normalized: [] }; + + if (table.config.sortChildren) { + cache.normalizedChildren = []; + } for (var i = 0; i < totalRows; ++i) { @@ -269,6 +274,20 @@ // continue to the next row if (c.hasClass(table.config.cssChildRow)) { cache.row[cache.row.length - 1] = cache.row[cache.row.length - 1].add(c); + + // if we are planning to sort child rows, add the row to the special "normalizedChildren" Array + if (table.config.sortChildren) { + if (cache.normalizedChildren[cache.row.length - 1] === undefined) { + cache.normalizedChildren[cache.row.length - 1] = []; + } + var normalizedChild = []; + for (var j = 0; j < totalCells; ++j) { + normalizedChild.push(parsers[j].format(getElementText(table.config, c[0].cells[j]), table, c[0].cells[j])); + } + normalizedChild.push(cache.normalizedChildren[cache.row.length - 1].length); // add position for rowCache + cache.normalizedChildren[cache.row.length - 1].push(normalizedChild); + } + // go to the next for loop continue; } @@ -287,7 +306,7 @@ if (table.config.debug) { benchmark("Building cache for " + totalRows + " rows:", cacheTime); } - + return cache; }; @@ -336,9 +355,13 @@ checkCell = (n[0].length - 1), tableBody = $(table.tBodies[0]), rows = []; - - + + if (table.config.sortChildren) { + var nc = c.normalizedChildren; + } + for (var i = 0; i < totalRows; i++) { + var pos = n[i][checkCell]; rows.push(r[pos]); @@ -347,12 +370,20 @@ //var o = ; var l = r[pos].length; + for (var j = 0; j < l; j++) { - tableBody[0].appendChild(r[pos][j]); + if (table.config.sortChildren && (j > 0)) { + // if this row has children and they need to be sorted... + var childIndex = nc[pos][j - 1][checkCell] + 1; + tableBody[0].appendChild(r[pos][childIndex]); + } else { + tableBody[0].appendChild(r[pos][j]); + } } // } + } @@ -631,7 +662,14 @@ eval(dynamicExp); cache.normalized.sort(sortWrapper); - + + if (table.config.sortChildren) { + var ncl = cache.normalizedChildren.length; + for (var i = 0; i < ncl; i++) { + cache.normalizedChildren[i].sort(sortWrapper); + } + } + if (table.config.debug) { benchmark("Sorting on " + sortList.toString() + " and dir " + order + " time:", sortTime); } From c00fbb5a3af6a7679fbc0e5c2d416d14d3c4bfc4 Mon Sep 17 00:00:00 2001 From: Marin Bezhanov Date: Tue, 21 Oct 2014 15:40:05 +0300 Subject: [PATCH 2/2] sort expanded child rows in place, without sorting parent rows --- jquery.tablesorter.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/jquery.tablesorter.js b/jquery.tablesorter.js index 9d96667..5d9ac91 100644 --- a/jquery.tablesorter.js +++ b/jquery.tablesorter.js @@ -120,6 +120,7 @@ sortAppend: null, sortLocaleCompare: true, sortChildren: false, + sortInPlace: false, textExtraction: "simple", parsers: {}, widgets: [], widgetZebra: { @@ -661,7 +662,10 @@ eval(dynamicExp); - cache.normalized.sort(sortWrapper); + // don't sort parent rows, if children rows are expanded and the "sort in place" setting is enabled + if (!table.config.sortInPlace || !$(table).find('.' + table.config.cssChildRow).is(':visible')) { + cache.normalized.sort(sortWrapper); + } if (table.config.sortChildren) { var ncl = cache.normalizedChildren.length;