-
Notifications
You must be signed in to change notification settings - Fork 8
/
amazeui-pagination.js
156 lines (133 loc) · 4.66 KB
/
amazeui-pagination.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
/* global jQuery */
; (function (window, undefined, $) {
'use strict';
var Pagination = function (json) {
this.wrap = json.wrap; // 分页控件的父容器
this.count = json.count; // 页数,通过用后台传回
this.callback = json.callback; // 点击页数的回调函数
this.prevText = json.prevText || '«'; // prev 按钮的文本内容
this.nextText = json.nextText || '»'; // next 按钮的文本内容
this.differNumber = 3;
this.current = json.current || 1; // 当前处于第几页
/**
* ajax.url 要访问的路径
* ajax.success 成功回调函数
* ajax.error 失败回调函数
*/
this.ajax = json.ajax; // 点击页数调一个 get 异步请求
// 调用初始化函数
this.init();
};
window.Pagination = Pagination;
Pagination.prototype.init = function () {
var _self = this,
wrap = _self.wrap,
current = _self.current,
callback = _self.callback,
ajax = _self.ajax,
get = _self.get,
render = _self.render;
$(wrap)
.html(render.call(_self, current))
.off()
.on('click', 'li', function () {
var $this = $(this),
num = parseInt($this.text()),
ruler = $this.data('ruler');
// 如果当前点击的是页数的按钮的话,current 就赋值当前页数
if ($.isNumeric(num)) {
current = num;
}
if (($.isNumeric(num) || ruler) && !$this.hasClass('am-disabled')) {
// 通过 data-ruler 的值来判断是上一页或下一页按钮
switch (ruler) {
case 'prev':
current -= 1;
break;
case 'next':
current += 1;
break;
}
// 渲染分页内容
$(_self.wrap).html(render.call(_self, current));
}
// 如果是有设置 ajax 的选项的话
if (ajax) {
get.call(_self, current);
}
// 如果有 callback 函数的话
if (typeof callback === 'function') {
callback(current);
}
})
.on('click', 'a', function(e) {
e.preventDefault();
});
};
Pagination.prototype.get = function (num) {
var _self = this,
ajax = _self.ajax,
url = ajax.url + num,
data = ajax.data,
success = ajax.success,
error = ajax.error;
$.get(url, data)
.success(function (result) {
if (typeof success === 'function') {
success(result);
}
})
.error(function (err) {
if (typeof error === 'function') {
error(err);
}
});
};
Pagination.prototype.render = function (num) {
// 如果传入的不是一个数字的话就直接返回
if (!num) {
return;
}
var template = [],
_self = this,
count = _self.count,
differNumber = _self.differNumber,
prevText = _self.prevText,
nextText = _self.nextText,
base = (differNumber * 2) + 1,
number, number2, number3;
if (count > base) {
number = (num - differNumber) <= 0 ? 1 : num - differNumber,
number2 = (num + differNumber) >= count ? num - ((num + differNumber + 1) - count) : number,
number3 = (number2 + (differNumber + 2)) >= count ? count : number2 + differNumber + 1;
} else {
number2 = 1;
number3 = count;
}
// 如果当前是第一页的话,上一按钮不可用
num > 1 ?
template.push('<li data-ruler="prev"><a href="#">' + prevText + '</a></li>') :
template.push('<li data-ruler="prev" class="am-disabled"><a href="#">' + prevText + '</a></li>');
// 距离第一页页数过长就省略
if (number3 > base && num + differNumber >= count) {
template.push('<li><a href="#">1</a></li>');
template.push('<li class="am-disabled"><a href="#">...</a></li>');
}
for (var i = number2; i <= number3; i++) {
num === i ?
template.push('<li class="am-active"><a href="#">' + i + '</a></li>') :
template.push('<li><a href="#">' + i + '</a></li>');
}
// 距离最后一页页数过长就省略
if (count > base && num + differNumber < count) {
template.push('<li class="am-disabled"><a href="#">...</a></li>');
template.push('<li><a href="#">' + count + '</a></li>');
}
// 如果当前是最后一页的话,下一页按钮不可用
num === count ?
template.push('<li data-ruler="next" class="am-disabled"><a href="#">' + nextText + '</a></li>') :
template.push('<li data-ruler="next"><a href="#">' + nextText + '</a></li>');
// 最后把 template 数组拼接成字符串
return template.join('');
};
})(window, undefined, jQuery);