-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.js
350 lines (292 loc) · 7.5 KB
/
list.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/**
* 分页
*
* @example
* 接口返回数据格式
* {
* code: 0,
* info: {
* list: [],
* pagination: {
* total: 总条数,
* page: 当前第几页(可选)
* }
* }
* }
*
* column字段约定
* [
* {title:, field:, escape:, nowrap:}
* ]
*
* new List({opts}).setQuery()
*/
define(function(require) {
var $ = require('jquery');
var base = require('./base');
var Seq = require('./seq');
var request = require('./request');
var pageTpl = ''
+ ' <div class="lg-pagination">'
+ ' <[ if (+pagination.total) {]>'
+ ' <p class="lg-pagination-detail">'
+ ' 显示第 <[- pagination.from ]>-<[- pagination.to ]> 条记录, 每页 <[- opts.perpage ]> 条, 共 <[- pagination.total ]> 条'
+ ' </p>'
+ ' <ul>'
+ ' <[ if (pagination.page > 1 ) { ]>'
+ ' <li data-page="<[- pagination.page - 1 ]>" ><</li>'
+ ' <[ } ]>'
+ ' <['
+ ' for(var i=0; i<pagination.pageArray.length; i++) {'
+ ' var current = pagination.pageArray[i];'
+ ' ]>'
+ ' <li'
+ ' <[- current === pagination.page ? \'class="lg-pagination-active"\' : \'\' ]>'
+ ' data-page="<[- current ]>"'
+ ' data-active="<[- +(current === pagination.page) ]>"'
+ ' >'
+ ' <[- current ]>'
+ ' </li>'
+ ' <[ } ]>'
+ ' <[ if (!(pagination.page === pagination.totalPage)) { ]>'
+ ' <li data-page="<[- pagination.page + 1 ]>" >></li>'
+ ' <[ } ]>'
+ ' </ul>'
+ ' <[ } else { ]>'
+ ' <div class="lg-empty"><p>暂无数据 !</p></div>'
+ ' <[ } ]>'
+ ' </div>';
var template = ''
+ '<div class="lg-table-list">'
+ ' <table cellpadding="0" cellspacing="0" border="0">'
+ ' <thead>'
+ ' <tr>'
+ ' <[ opts.columns.forEach(function(item) { ]>'
+ ' <th <[ if(item.className) {]> class="<[= item.className ]>" <[ } ]> ><[- item.title||\'\' ]></th>'
+ ' <[ }) ]>'
+ ' </tr>'
+ ' </thead>'
+ ' <tbody class="lg-table-tr">'
+ ' <[list.forEach(function(record, key) { ]>'
+ ' <tr data-index="<[- key ]>">'
+ ' <['
+ ' opts.columns.forEach(function(column) { '
+ ' var value = record[column.field];'
+ ' ]>'
+ ' <td <[- column.nowrap ? \'class="lg-white-space-nowrap"\' : \'\' ]> ><[- value === undefined ? \'\' : value ]></td>'
+ ' <[ }) ]>'
+ ' </tr>'
+ ' <[ }) ]>'
+ ' </tbody>'
+ ' </table>'
+ '</div>';
var options = {
container: undefined, // 容器,必填 {node|HTMLElement}
template: template,
pageTpl: pageTpl,
page: 1, // 默认从第一页开始
perpage: 10, // 每个页面的个数
ajaxOpts: {},
columns: [],
isMask: true,
maskOpts: {},
pageFromServer: false // 以服务器返回的page作为当前页面
};
var List = base.inherit({
initialize: function(opts) {
this.opts = $.extend({}, options, opts);
this.page = this.opts.page;
this._query = function() {};
this._parse = function(list) {
return list;
};
this._resetcolumn = function(column) {
return column;
};
this.addListener();
},
addListener: function() {
var node = this.opts.container;
var _this = this;
$(node).on('click', '.lg-pagination li', function() {
var data = $(this).data();
var page = +data.page;
page && !+data.active && _this.setPage(page);
});
},
_pagination: function(page, totalPage) {
var array = [];
if (totalPage <= 5) {
var i = 0;
while (++i <= totalPage) {
array.push(i);
}
return array;
}
if (page > 3) {
array.push(1);
page !== 3 && array.push('...');
}
if (page === 3 || page === totalPage) {
array.push(page - 2);
}
if (page >= 2) {
array.push(page - 1);
}
array.push(page);
(page + 1 <= totalPage) && array.push(page + 1);
if (page === 1) {
array.push(page + 2);
}
if (page + 1 < totalPage) {
page + 2 !== totalPage && array.push('...');
array.push(totalPage);
}
return array;
},
pagination: function(pagination) {
var opts = this.opts;
var page = this.page;
var total = pagination.total;
var totalPage = Math.ceil(total / opts.perpage);
var array = this._pagination(page, totalPage);
return {
page: page,
total: total,
totalPage: Math.ceil(total / opts.perpage),
pageArray: array,
from: (page - 1) * opts.perpage + 1,
to: Math.min(page * opts.perpage, total)
};
},
toList: function(list, pagination) {
var opts = this.opts;
var tpl = '<div class="lg-table-wrap lg-lg-lg">' + opts.template + opts.pageTpl + '</div>';
var build = base.template(tpl)({
opts: opts,
list: list,
pagination: this.pagination(pagination)
});
opts.container.innerHTML = build;
},
build: function(list) {
var columns = this.opts.columns || [];
var _list = [];
$.each(list, function(key, obj) {
var item = {};
// TODO 这里直接使用field作为键使用
columns.forEach(function(column) {
var value = base.get(obj, column.field);
item[column.field] = value !== undefined && value !== null
? ((column.escape || column.escape === undefined) ? base.escapeHtml(value) : value)
: '';
});
item._original_ = obj;
_list.push(item);
});
return _list;
},
recolumn: function(cb) {
typeof cb === 'function' && (this._resetcolumn = cb);
return this;
},
_recolumn: function() {
var columns = this.opts.columns || [];
var _this = this;
return (columns || []).map(function(column) {
return $.extend({}, _this._resetcolumn(column));
});
},
/**
* 渲染记录
*
* @param {object} options
*/
render: function() {
var _this = this;
var opts = this.opts;
if (opts.columns) {
opts.columns = this._recolumn();
}
this.request()
.seq(function(list, pagination) {
_this.toList(list, pagination);
this(list);
})
.seq(function(list) {
_this.attachData(list);
})
.resolve();
},
setQuery: function(cb) {
typeof cb === 'function' && (this._query = cb);
return this;
},
setParse: function(cb) {
typeof cb === 'function' && (this._parse = cb);
return this;
},
/**
* 设置请求参数
*
* @param {object}
* @return {object}
*/
query: function() {
var opts = this.opts;
var ajaxOpts = $.extend({}, opts.ajaxOpts);
if (ajaxOpts.url) {
ajaxOpts.url = base.url(ajaxOpts.url, {
page: this.page,
perpage: opts.perpage
});
}
var data = this._query();
ajaxOpts.data = base.param(ajaxOpts.data, data);
return ajaxOpts;
},
request: function() {
var opts = this.opts;
var _this = this;
var ajaxOpts = this.query();
if (opts.isMask) {
ajaxOpts.mask = $.extend({
target: $(opts.container)
}, opts.maskOpts);
}
return new Seq()
.seq(function() {
request(ajaxOpts, this);
})
.seq(function(info) {
info = info || {};
var list = info.list || [];
list = _this.build(_this._parse(list));
var pagination = info.pagination || {};
_this.page = opts.pageFromServer ? pagination.page : _this.page;
this(list, { total: pagination.total });
});
},
/**
* 设置页面
*
* @param {int} 指定具体页面
* @return void
*/
setPage: function(page) {
this.page = page;
this.render();
},
/**
* 把数据依附到对应的行
*/
attachData: function(list) {
$(this.opts.container).find('.lg-table-tr').find('tr').each(function() {
var $this = $(this);
var index = $this.data('index');
$this.data('record', (list[index] || {})._original_);
});
}
});
return List;
});