-
Notifications
You must be signed in to change notification settings - Fork 1
/
jquery.ajaxpager.js
152 lines (144 loc) · 4.79 KB
/
jquery.ajaxpager.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
(function ($) {
$.fn.ajaxPager = function (settings) {
var container = this;
var options = $.extend({
showNum:6,
pageSize:5,
url:"",
param:{},
autoShow:true,
hideEmpty:false,
callback:function (data) {
},
getTotal:function (data) {
return data.total;
},
getData:function (data) {
return data.data;
},
navText:{
first:'首页',
prev:'上一页',
next:'下一页',
last:'末页'
}
}, settings || {});
var total;
var showNum;
var pageSize;
var curPage;
var url;
var param;
function init() {
showNum = options.showNum;
pageSize = options.pageSize;
curPage = 0;
url = options.url;
param = options.param;
var pages = $("<div class='ajaxPager'>");
var pageNode = $("<a href='javascript:void(0)'/>");
var navNode = pageNode.clone().addClass('page-nav');
pages.append(navNode.clone().addClass('first_page').text(options.navText.first))
.append(navNode.clone().addClass('prev_page').text(options.navText.prev));
for (var i = 1; i <= showNum; i++) {
pages.append(pageNode.clone().addClass('active').text(i));
}
pages.append(navNode.clone().addClass('next_page').text(options.navText.next))
.append(navNode.clone().addClass('last_page').text(options.navText.last));
container.hide().prepend(pages);
container.on('click', 'a.active', function () {
show($(this).text());
});
container.on('click', 'a.first_page', function () {
show(1);
});
container.on('click', 'a.last_page', function () {
show(total);
});
container.on('click', 'a.prev_page', function () {
show(curPage - 1 > 0 ? curPage - 1 : 1);
});
container.on('click', 'a.next_page', function () {
show(curPage + 1 < total ? curPage + 1 : total);
});
}
function show(page) {
if (page == null) {
page = curPage;
}
$.getJSON(url, $.extend({}, param, {page:page, pagesize:pageSize}), function (data) {
var totalCount = options.getTotal(data);
total = Math.ceil(totalCount / pageSize);
curPage = page;
options.callback.call(container, options.getData(data));
if (options.hideEmpty && total <= 1) {
container.hide()
} else {
update();
container.show();
}
})
}
function update() {
$(".page-nav").show();
if (curPage == 1) {
$(".first_page,.prev_page").hide();
}
if (curPage == total) {
$(".next_page,.last_page").hide();
}
var start = curPage - (showNum % 2 == 0 ? showNum : showNum - 1) / 2;
if (start + showNum - 1 > total) {
start = total - showNum + 1;
}
if (start < 1) {
start = 1;
}
var node = $("a.prev_page", container).next();
for (var i = start; i < start + showNum; i++) {
if (i > total) {
node.hide();
} else if (i != curPage) {
node.text(i).addClass('active').show();
} else {
node.text(i).removeClass('active').show();
}
node = node.next();
}
container.show();
}
function changeSource(newurl, newparam) {
url = newurl;
param = newparam;
}
function setParam(key, value) {
if (value == null) {
param = $.extend(param, key);
} else {
param.key = value;
}
}
function setPagesize(value) {
if (value == null) {
return pageSize;
} else {
pageSize = value;
return this;
}
}
init();
if (options.autoShow) {
show(1);
}
return {
show:show,
update:update,
changeSource:changeSource,
setParam:setParam,
pageSize:setPagesize,
getPage:function () {
return curPage;
}
}
}
})(jQuery);