From 2393a973f68e27bc8b627263b01371568bcaeb96 Mon Sep 17 00:00:00 2001 From: Sergio Crisostomo Date: Mon, 26 Oct 2015 16:14:05 +0100 Subject: [PATCH] add sort order to onSort function arguments --- Docs/Interface/HtmlTable.Sort.md | 2 +- Source/Interface/HtmlTable.Sort.js | 5 +++-- Specs/Interface/HtmlTable.Sort.js | 29 +++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/Docs/Interface/HtmlTable.Sort.md b/Docs/Interface/HtmlTable.Sort.md index 935472af..85a8bfb9 100644 --- a/Docs/Interface/HtmlTable.Sort.md +++ b/Docs/Interface/HtmlTable.Sort.md @@ -40,7 +40,7 @@ HtmlTable Method: constructor ### Events -* sort - callback executed when a column is sorted; passed the *tbody* and the index of the column sorted. +* sort - callback executed when a column is sorted; passed the *tbody*, the index of the column sorted and *reversed* to know the sorting direction. ### Note diff --git a/Source/Interface/HtmlTable.Sort.js b/Source/Interface/HtmlTable.Sort.js index 3a488ba9..45ffb601 100644 --- a/Source/Interface/HtmlTable.Sort.js +++ b/Source/Interface/HtmlTable.Sort.js @@ -230,12 +230,13 @@ HtmlTable = Class.refactor(HtmlTable, { return a.value > b.value ? 1 : -1; }); - if (this.sorted.reverse == (parser == HtmlTable.Parsers['input-checked'])) data.reverse(true); + var reversed = this.sorted.reverse == (parser == HtmlTable.Parsers['input-checked']); + if (reversed) data.reverse(true); this.setRowSort(data, pre); if (rel) rel.grab(this.body); this.fireEvent('stateChanged'); - return this.fireEvent('sort', [this.body, this.sorted.index]); + return this.fireEvent('sort', [this.body, this.sorted.index, reversed]); }, parseData: function(parser){ diff --git a/Specs/Interface/HtmlTable.Sort.js b/Specs/Interface/HtmlTable.Sort.js index 2d78ee54..1ef6a7b4 100644 --- a/Specs/Interface/HtmlTable.Sort.js +++ b/Specs/Interface/HtmlTable.Sort.js @@ -120,6 +120,35 @@ describe('HtmlTable.Sort', function(){ }); + describe('onSort event', function(){ + var status, args; + var table = new HtmlTable({ + sortable: true, + headers: ['col'], + parsers: ['string'], + rows: [['a', 'c', 'b']], + onSort: function(body, index, reversed){ + args = arguments; + status = reversed; + } + }); + + it('should set function arguments', function(){ + expect(args.length).toEqual(3); + expect(args[0].tagName.toLowerCase()).toEqual('tbody'); + expect(typeof args[1]).toEqual('number'); + expect(typeof args[2]).toEqual('boolean'); + }); + + it('should correctly set the direction onSort event', function(){ + table.sort(0); + expect(status).toEqual(false); + table.sort(0); + expect(status).toEqual(true); + }); + + }); + describe('string', function(){ it('should sort a list alphabetically', function(){