Skip to content

Commit c6a90c3

Browse files
Merge pull request #1332 from SergioCrisostomo/sort-direction
add sort order to onSort function arguments
2 parents 899de1f + 58c256c commit c6a90c3

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed

Docs/Interface/HtmlTable.Sort.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ HtmlTable Method: constructor
4040

4141
### Events
4242

43-
* sort - callback executed when a column is sorted; passed the *tbody* and the index of the column sorted.
43+
* sort - callback executed when a column is sorted; passed the *tbody* (Element), the index of the column sorted (Integer) and which direction the table is sorted ("asc" or "desc").
4444

4545
### Note
4646

Source/Interface/HtmlTable.Sort.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -230,12 +230,13 @@ HtmlTable = Class.refactor(HtmlTable, {
230230
return a.value > b.value ? 1 : -1;
231231
});
232232

233-
if (this.sorted.reverse == (parser == HtmlTable.Parsers['input-checked'])) data.reverse(true);
233+
var reverse = this.sorted.reverse == (parser == HtmlTable.Parsers['input-checked']);
234+
if (reverse) data.reverse(true);
234235
this.setRowSort(data, pre);
235236

236237
if (rel) rel.grab(this.body);
237238
this.fireEvent('stateChanged');
238-
return this.fireEvent('sort', [this.body, this.sorted.index]);
239+
return this.fireEvent('sort', [this.body, this.sorted.index, reverse ? 'asc' : 'desc']);
239240
},
240241

241242
parseData: function(parser){

Specs/Interface/HtmlTable.Sort.js

+39-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ provides: [HtmlTable.Sort.Tests]
66
...
77
*/
88
describe('HtmlTable.Sort', function(){
9+
10+
function getText(rows){
11+
return Array.map(rows, function(item){
12+
return item.cells[0].get('text') || item.cells[0].getElement('input').get('value');
13+
});
14+
}
915

1016
it('should not step on prior this.bind declarations', function () {
1117
var table = new HtmlTable();
@@ -22,9 +28,7 @@ describe('HtmlTable.Sort', function(){
2228
rows: data.map(function(item){return [item];})
2329
});
2430

25-
return Array.map(table.sort(0, false).body.rows, function(item){
26-
return item.cells[0].get('text') || item.cells[0].getElement('input').get('value');
27-
});
31+
return getText(table.sort(0, false).body.rows);
2832
};
2933

3034
describe('date', function(){
@@ -120,6 +124,38 @@ describe('HtmlTable.Sort', function(){
120124

121125
});
122126

127+
describe('onSort event', function(){
128+
var status, args, tbody;
129+
var table = new HtmlTable({
130+
sortable: true,
131+
headers: ['col'],
132+
parsers: ['string'],
133+
rows: [['a'], ['c'], ['b']],
134+
onSort: function(body, index, reversed){
135+
args = arguments;
136+
tbody = body;
137+
status = reversed;
138+
}
139+
});
140+
141+
it('should set function arguments', function(){
142+
expect(args.length).toEqual(3);
143+
expect(args[0].tagName.toLowerCase()).toEqual('tbody');
144+
expect(typeof args[1]).toEqual('number');
145+
expect(typeof args[2]).toEqual('string');
146+
});
147+
148+
it('should correctly set the direction onSort event', function(){
149+
table.sort(0, false);
150+
expect(status).toEqual('asc');
151+
expect(getText(tbody.rows)).toEqual(['a', 'b', 'c']);
152+
table.sort(0, true);
153+
expect(status).toEqual('desc');
154+
expect(getText(tbody.rows)).toEqual(['c', 'b', 'a']);
155+
});
156+
157+
});
158+
123159
describe('string', function(){
124160

125161
it('should sort a list alphabetically', function(){

0 commit comments

Comments
 (0)