-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSort.js
More file actions
66 lines (60 loc) · 1.98 KB
/
Sort.js
File metadata and controls
66 lines (60 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
define([
'dojo/_base/declare',
'dojo/_base/array'
], function(declare, array){
return declare(null, {
// summary:
// Provide sort api for the grid
// and provide a single UI in the header like traditional grid sorting.
sort: function(args){
// summary:
// Sort the grid by args which in store sorting format.
if(args && (args.length || args.attribute)){
this.queryOptions.sort = args.length ? args : [args];
}else{
this.queryOptions.sort = null;
}
this.refresh();
this.updateSortIndicators();
},
_buildHeader: function(){
// summary:
// Add click to sort capability for header, only for single sort.
this.inherited(arguments);
if(this._headerClickHandler){return;}
this._headerClickHandler = this.connect(this.headerNode, 'onclick', function(evt){
var cell = evt.target;
if(!/th/i.test(cell.tagName))return;
if(!this.queryOptions){this.queryOptions = {};}
var col = this.columns[cell.cellIndex], sort = this.queryOptions.sort;
//Initial sort may be a array or null..
if(sort){sort = sort.length ? sort[0] : sort;}
else{sort = {};}
if(sort.attribute != col.field){
sort = {attribute: col.field, descending: false};
}else{
if(sort.descending)sort.attribute = null;
else if(!sort.descending)sort.descending = true;
}
this.sort(sort);
}, this);
},
updateSortIndicators: function(){
// summary:
// Update the sorting indicators. Maybe override to provide other sorting UI.
// Only support single sort now
var cols = this.columns, sort = this.queryOptions.sort,
ht = this.headerNode.firstChild.firstChild;//header table
if(sort)sort = sort.length ? sort[0] : sort;
else sort = {};
array.forEach(ht.rows[0].cells, function(cell, i){
var txt = cell.innerHTML.replace(/ *[↑↓]$/g, '');
var col = cols[cell.cellIndex];
if(col.field == sort.attribute){
txt += sort.descending ? ' ↓' : ' ↑';
}
cell.innerHTML = txt;
});
}
});
});