-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
265 lines (218 loc) · 4.78 KB
/
index.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/**
* Component dependencies
*/
var o = require('jquery');
var type = require('type');
var free = require('tags-free');
var Pager = require('pager');
/**
* Expose `DataTable`
*/
module.exports = DataTable;
/**
* Expose `DataTable`
*
* @api public
*/
function DataTable(){
if (!(this instanceof DataTable)) return new DataTable;
this.config = {
sort: { col: 0, dir: 1 } ,
pager: { page: 0, perpage: 10 }
};
// get markup template
this.el = o(require('./template'));
this.columns = [];
this.rows = [];
return this;
}
/**
* Add new row to DataTable
*
* @param {Array} row
* @api public
*/
DataTable.prototype.add = function(row){
if (!row.length) return this;
this.rows.push(row);
return this;
};
/**
* Add a lot of rows
*
* @param {Array} data
* @api public
*/
DataTable.prototype.load = function(data){
if (!data.length) return this;
for (var i = 0; i < data.length; i++) {
this.add(data[i]);
}
return this;
};
/**
* Set table header
*
* @param {Array} rows
* @api public
*/
DataTable.prototype.header = function(cols){
if (!cols.length) return this;
for (var i = 0, col = cols[0]; i < cols.length; i++, col = cols[i]) {
this.column(col);
}
return this;
};
/**
* Set column to table header
*
* @param {String|Array} rows
* @return {Datable} this
* @api private
*/
DataTable.prototype.column = function(column) {
var title = 0;
var sort = 1;
var sortType = 2;
var isstr = 'string' == type(column);
var cssname = !isstr && column[sort] ? 'sort' : '';
var el;
var el;
if (isstr) {
el = o('<span>');
} else {
el = o('<a>', { href: '#', class: cssname })
.click(this.onsort.bind(this, column[sortType] || 'numeric' ));
}
o('<th>')
.append( el.html( isstr ? column : column[title] ) )
.appendTo( this.el.find('thead tr') );
// add column to columns list
this.columns.push(column);
// set colspan in footer element
this.el.find('tfoot tr td').attr('colspan', this.columns.length);
return this;
};
/**
* Render the table body
*
* @api private
*/
DataTable.prototype.body = function(){
var prg = this.config.pager;
var ini = prg.page * prg.perpage;
var end = Math.min(ini + prg.perpage, this.rows.length);
this.el.find('tbody').empty();
for (var j = ini, row = this.rows[ini]; j < end; j++, row = this.rows[j]) {
for (var i = 0, tr = o('<tr>'); i < row.length; i++) {
tr.append(o('<td>', { html: row[i] }));
}
this.el.find('tbody').append(tr);
}
};
/**
* Bind `click` event in table header
*
* @param {string} type
* @param {Object} ev jQuery object event
* @api public
*/
DataTable.prototype.onsort = function(type, ev){
ev.preventDefault();
var el = o(ev.target);
var th = el.closest('th');
var col = th.prevAll().length;
var dir = el.hasClass('asc') ? -1 : 1;
this.sort(col, dir, type);
};
/**
* Sort the tabe data
*
* @api public
*/
DataTable.prototype.sort = function(col, dir, type){
var th = this.el.find('thead tr th').eq(col);
var el = th.find('a');
this.el.find('thead th a').removeClass('asc desc');
el[(dir > 0 ? 'add' : 'remove') + 'Class']('asc');
el[(dir < 0 ? 'add' : 'remove') + 'Class']('desc');
sortBy(this.rows, col, dir, type);
this.body();
return this;
};
/**
* Add paginate to table footer
*
* Emit `pager` event
*
* @param {Object} opts pager options
* @api private
*/
DataTable.prototype.paginate = function(page, perpage){
this.config.pager.page = page;
this.config.pager.perpage = perpage;
var pager = new Pager;
pager.el.appendTo(this.el.find('tfoot td').get(0));
this.el.find('tfoot td').append(
pager
.total(this.rows.length)
.perpage(perpage || 10)
.select(page || 0)
.render()
);
// Emit `pager` event
pager.on('show', this.onpager.bind(this));
return this;
};
/**
* Bind pager `show` event
*
* @param {Number} page select page
* @api public
*/
DataTable.prototype.onpager = function(page){
this.config.pager.page = page;
this.body();
};
/**
* Render component
*
* @api public
*/
DataTable.prototype.render = function(){
this.body();
return this.el;
};
/**
* Replace markup into the given element
*
* @param {String|jQuery} el reference element to inject the markup
* @api public
*/
DataTable.prototype.replace = function(el){
o(el).append(this.render());
};
/**
* Sort the given array by col and dir
*
* @param {Array} arr array to sort
* @param {Number} col
* @param {Number} dir
*/
function sortBy(arr, col, dir, type){
arr.sort(function(e0, e1){
var v0 = e0[col];
var v1 = e1[col];
switch(type) {
case 'numeric':
v0 = Number(v0);
v1 = Number(v1);
break;
case 'alpha':
v0 = free(v0).trim();
v1 = free(v1).trim();
break;
}
return (v1 < v0 ? 1 : -1) * dir;
});
}