Skip to content

Commit

Permalink
add sort order to onSort function arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioCrisostomo committed Oct 26, 2015
1 parent a28974f commit 2393a97
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Docs/Interface/HtmlTable.Sort.md
Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions Source/Interface/HtmlTable.Sort.js
Expand Up @@ -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){
Expand Down
29 changes: 29 additions & 0 deletions Specs/Interface/HtmlTable.Sort.js
Expand Up @@ -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(){
Expand Down

0 comments on commit 2393a97

Please sign in to comment.