bootstrap.pagination.js
4.93 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
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
/**
* 显示一个分页控件
* 作者:黄平
* 日期:2015-07-27
*/
(function($) {
$.fn.pagination = function(options, param) {
var self = this;
if (typeof (options) == "string") {
var method = $.fn.pagination.methods[options];
if (method){
return method.call(this, param);
}
}
return this.each(function() {
var opt = $.extend({}, $.fn.pagination.defaults, options);
self.data("pagination", opt);
self.attr("role", "pagination");
var leftDiv = $("<div>").addClass("col-md-4 column").appendTo(self);
var leftSpan = $("<span>").addClass("pull-left").css({
"height" : "44px",
"line-height" : "44px",
"margin-left" : "15px"
}).appendTo(leftDiv);
var totalPage = _getTotalPage(opt.totalCount, opt.pageSize);
leftSpan.html("共" + opt.totalCount + "条记录.. 当前第" + opt.currentPage + "页..总共" + totalPage + "页");
var pageDiv = $("<div>").addClass("col-md-8 column").appendTo(self);
var ul = $("<ul>").addClass("pagination pull-right").css("margin", "5px").appendTo(pageDiv);
var previousLi = $("<li>").appendTo(ul);
if (opt.currentPage == 1) {
previousLi.addClass("disabled");
}
var previousA = $("<a>").attr({
"aria-label" : opt.currentPage - 1,
pageNum : opt.currentPage - 1,
href : "#"
}).append($("<span>").attr("aria-hidden",false).html(opt.previousPageText)).appendTo(previousLi);
var firstLi = $("<li>").appendTo(ul);
if (opt.currentPage == 1) {
firstLi.addClass("active");
}
var firstA = $("<a>").attr({
"aria-label" : 1,
pageNum : 1,
href : "#"
}).append($("<span>").attr("aria-hidden",false).html(1)).appendTo(firstLi);
if (opt.currentPage > 5) {
$("<li>").append($("<a>").append($("<span>").attr("aria-hidden",false).html("..."))).appendTo(ul);
}
for (var i = opt.currentPage - 5; i <= opt.currentPage; i++) {
if (i <= 1 || i >= totalPage) {
continue;
}
var pageLi = $("<li>").appendTo(ul);
if (i == opt.currentPage) {
pageLi.addClass("active");
}
var pageA = $("<a>").attr({
"aria-label" : i,
pageNum : i,
href : "#"
}).append($("<span>").attr("aria-hidden",false).html(i)).appendTo(pageLi);
}
for (var i = opt.currentPage + 1; i < opt.currentPage + 5 && i < totalPage; i++) {
var pageLi = $("<li>").appendTo(ul);
if (i == opt.currentPage) {
pageLi.addClass("active");
}
var pageA = $("<a>").attr({
"aria-label" : i,
pageNum : i,
href : "#"
}).append($("<span>").attr("aria-hidden",false).html(i)).appendTo(pageLi);
}
if (opt.currentPage + 5 < totalPage) {
$("<li>").append($("<a>").append($("<span>").attr("aria-hidden",false).html("..."))).appendTo(ul);
}
if (totalPage > 1) {
var lastLi = $("<li>").appendTo(ul);
if (opt.currentPage == totalPage) {
lastLi.addClass("active");
}
var lastA = $("<a>").attr({
"aria-label" : totalPage,
pageNum : totalPage,
href : "#"
}).append($("<span>").attr("aria-hidden",false).html(totalPage)).appendTo(lastLi);
}
var nextLi = $("<li>").appendTo(ul);
if (opt.currentPage == totalPage) {
nextLi.addClass("disabled");
}
var nextA = $("<a>").attr({
"aria-label" : opt.currentPage + 1,
pageNum : opt.currentPage + 1,
href : "#"
}).append($("<span>").attr("aria-hidden",false).html(opt.nextPageText)).appendTo(nextLi);
ul.find("a").click(function() {
var pageNum = $(this).attr("pageNum");
if (!pageNum) {
return;
}
_selectPage(self, $(this).attr("pageNum"));
});
});
};
/**
* 选择页数
*/
function _selectPage(jq, pageNum) {
var opt = jq.data("pagination");
var a = jq.find("a[pageNum='"+ pageNum +"']");
var li = a.parent();
if (li.hasClass("active") || li.hasClass("disabled")) {
return;
}
if (opt.onSelectPage) {
opt.onSelectPage.call(jq, pageNum, opt.pageSize);
}
jq.empty();
jq.pagination($.extend({}, jq.data("pagination"), {
currentPage : parseInt(pageNum)
}));
}
/**
* 根据总条数,计算总页数
*/
function _getTotalPage(totalCount, pageSize) {
if (totalCount == 0 || pageSize == 0) {
return 1;
}
var tmp = parseInt(totalCount / pageSize);
if (totalCount % pageSize == 0) {
return tmp;
} else {
return tmp + 1;
}
}
$.fn.pagination.methods = {
/**
* 选择页数
* @param pageNum
*/
selectPage : function(pageNum) {
_selectPage(this, pageNum);
},
/**
* 获取分页对象
*/
getPagination : function() {
return this;
}
};
$.fn.pagination.event = {
onSelectPage : function(currentPage, pageSize) {
//alert("currentPage= " + currentPage + ",pageSize= " + pageSize);
}
};
$.fn.pagination.defaults = $.extend({}, $.fn.pagination.event, {
//pageType : "backgroundPage", //前台分页-frontPage;后台分页-backgroundPage
totalCount : 0,
pageSize : 10,
currentPage : 1,
previousPageText : "«",
nextPageText : "»"
});
})(jQuery);