From 674941c4d0dca3421901de364ccee0a8a3758aed Mon Sep 17 00:00:00 2001 From: SergioCrisostomo Date: Sun, 22 Jun 2014 16:48:38 +0200 Subject: [PATCH 1/3] Add possibility to custom sort function This opens to the possibility of passing a custom user function to sort the order of a specific column. --- Source/Interface/HtmlTable.Sort.js | 4 ++-- Specs/Interface/HtmlTable.Sort.js | 22 ++++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/Source/Interface/HtmlTable.Sort.js b/Source/Interface/HtmlTable.Sort.js index a32806b4..cd7545c6 100644 --- a/Source/Interface/HtmlTable.Sort.js +++ b/Source/Interface/HtmlTable.Sort.js @@ -207,7 +207,7 @@ HtmlTable = Class.refactor(HtmlTable, { return typeOf(parser) == 'string' ? HtmlTable.Parsers[parser] : parser; }, - sort: function(index, reverse, pre){ + sort: function(index, reverse, pre, sortFunction){ if (!this.head) return; if (!pre){ @@ -225,7 +225,7 @@ HtmlTable = Class.refactor(HtmlTable, { this.body.dispose(); } - var data = this.parseData(parser).sort(function(a, b){ + var data = this.parseData(parser).sort(sortFunction ? sortFunction : function(a, b){ if (a.value === b.value) return 0; return a.value > b.value ? 1 : -1; }); diff --git a/Specs/Interface/HtmlTable.Sort.js b/Specs/Interface/HtmlTable.Sort.js index ec6a22e4..753384e2 100644 --- a/Specs/Interface/HtmlTable.Sort.js +++ b/Specs/Interface/HtmlTable.Sort.js @@ -160,6 +160,28 @@ describe('HtmlTable.Sort', function(){ expect(table.serialize()).toEqual({sortIndex: 0, sortReverse: false}); }); + it('should allow a custom sort function', function(){ + var table = new HtmlTable({ + sortable: true, + headers: ['col'], + rows: [ + ['cccc'], + ['bb'], + ['aaa'] + ] + }); + + function customSort(a, b) { // sort by length of string + return a.value.length > b.value.length ? 1: -1; + } + + table.sort(0, false, false, customSort); + var values = Array.map(table.body.rows, function(item){ + return item.cells[0].get('text'); + }); + expect(values).toEqual(["bb", "aaa", "cccc"]); + }); + it('should restore the sorted state of a table', function(){ var table = new HtmlTable({ sortable: true, From 1b6570649349abc3bcb20ba8fa51ea16868f8df4 Mon Sep 17 00:00:00 2001 From: SergioCrisostomo Date: Mon, 23 Jun 2014 11:40:12 +0200 Subject: [PATCH 2/3] docs update with new sort parameter --- Docs/Interface/HtmlTable.Sort.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Docs/Interface/HtmlTable.Sort.md b/Docs/Interface/HtmlTable.Sort.md index d3b74103..93c2d3c3 100644 --- a/Docs/Interface/HtmlTable.Sort.md +++ b/Docs/Interface/HtmlTable.Sort.md @@ -54,13 +54,14 @@ Sorts a column. ### Syntax - myHtmlTable.sort(index, reverse, prepare); + myHtmlTable.sort(index, reverse, prepare[, function]); ### Arguments 1. index - (*number*) the index of the column to sort 2. reverse - (*boolean*) reverses the sort if *true*; defaults to *false* 3. prepare - (*boolean*) if the sort has a secondary sort, set this value to *true* on the first sort, and *false* on the second. For example, if you sorted a directory list of files first by type and then secondly by file size, you would sort on type and pass *true* and then sort on size and pass *false*. +4. function - (*function*, optional) this custom function will be used to sort the column. The parameters passed to _sort_ are objects with *position* and *value*. ### Returns From b0a2bda51f6ea3c47e9e3de4d5dcb14b8ffd45e7 Mon Sep 17 00:00:00 2001 From: SergioCrisostomo Date: Mon, 23 Jun 2014 12:30:38 +0200 Subject: [PATCH 3/3] call it sort instead --- Docs/Interface/HtmlTable.Sort.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Docs/Interface/HtmlTable.Sort.md b/Docs/Interface/HtmlTable.Sort.md index 93c2d3c3..935472af 100644 --- a/Docs/Interface/HtmlTable.Sort.md +++ b/Docs/Interface/HtmlTable.Sort.md @@ -54,14 +54,14 @@ Sorts a column. ### Syntax - myHtmlTable.sort(index, reverse, prepare[, function]); + myHtmlTable.sort(index, reverse, prepare[, sort]); ### Arguments 1. index - (*number*) the index of the column to sort 2. reverse - (*boolean*) reverses the sort if *true*; defaults to *false* 3. prepare - (*boolean*) if the sort has a secondary sort, set this value to *true* on the first sort, and *false* on the second. For example, if you sorted a directory list of files first by type and then secondly by file size, you would sort on type and pass *true* and then sort on size and pass *false*. -4. function - (*function*, optional) this custom function will be used to sort the column. The parameters passed to _sort_ are objects with *position* and *value*. +4. sort - (*function*, optional) this custom function will be used to sort the column. The parameters passed to _sort_ are objects with *position* and *value*. ### Returns