http://www.cnblogs.com/LeoLcy/p/4846638.html
1、前台使用ajax无刷新分页,主要需要生成分页的工具条,这里使用的是jquery.pagination.js
插件参数可以参考----张龙豪-jquery.pagination.js分页
下面贴出代码

1 /**
2 * This jQuery plugin displays pagination links inside the selected elements.
3 *
4 * @author Gabriel Birke (birke *at* d-scribe *dot* de)
5 * @version 1.2
6 * @param {int} maxentries Number of entries to paginate
7 * @param {Object} opts Several options (see README for documentation)
8 * @return {Object} jQuery Object
9 */
10 jQuery.fn.pagination = function(maxentries, opts){
11 opts = jQuery.extend({
12 items_per_page:10,
13 num_display_entries:10,
14 current_page:0,
15 num_edge_entries:0,
16 link_to:"#",
17 prev_text:"Prev",
18 next_text:"Next",
19 ellipse_text:"...",
20 prev_show_always:true,
21 next_show_always:true,
22 callback:function(){return false;}
23 },opts||{});
24
25 return this.each(function() {
26 /**
27 * 计算最大分页显示数目
28 */
29 function numPages() {
30 return Math.ceil(maxentries/opts.items_per_page);
31 }
32 /**
33 * 极端分页的起始和结束点,这取决于current_page 和 num_display_entries.
34 * @返回 {数组(Array)}
35 */
36 function getInterval() {
37 var ne_half = Math.ceil(opts.num_display_entries/2);
38 var np = numPages();
39 var upper_limit = np-opts.num_display_entries;
40 var start = current_page>ne_half?Math.max(Math.min(current_page-ne_half, upper_limit), 0):0;
41 var end = current_page>ne_half?Math.min(current_page+ne_half, np):Math.min(opts.num_display_entries, np);
42 return [start,end];
43 }
44
45 /**
46 * 分页链接事件处理函数
47 * @参数 {int} page_id 为新页码
48 */
49 function pageSelected(page_id, evt){
50 current_page = page_id;
51 drawLinks();
52 var continuePropagation = opts.callback(page_id, panel);
53 if (!continuePropagation) {
54 if (evt.stopPropagation) {
55 evt.stopPropagation();
56 }
57 else {
58 evt.cancelBubble = true;
59 }
60 }
61 return continuePropagation;
62 }
63
64 /**
65 * 此函数将分页链接插入到容器元素中
66 */
67 function drawLinks() {
68 panel.empty();
69 var interval = getInterval();
70 var np = numPages();
71 // 这个辅助函数返回一个处理函数调用有着正确page_id的pageSelected。
72 var getClickHandler = function(page_id) {
73 return function(evt){ return pageSelected(page_id,evt); }
74 }
75 //辅助函数用来产生一个单链接(如果不是当前页则产生span标签)
76 var appendItem = function(page_id, appendopts){
77 page_id = page_id<0?0:(page_id<np?page_id:np-1); // 规范page id值
78 appendopts = jQuery.extend({text:page_id+1, classes:""}, appendopts||{});
79 if(page_id == current_page){
80 var lnk = jQuery("<a href class='currentPage'>" + (appendopts.text) + "</a>");
81 }else{
82 var lnk = jQuery("<a>"+(appendopts.text)+"</a>")
83 .bind("click", getClickHandler(page_id))
84 .attr('href', opts.link_to.replace(/__id__/,page_id));
85 }
86 if (appendopts.classes) { lnk.addClass(appendopts.classes); }
87 panel.append(lnk);
88 }
89 //产生描述
90 panel.append("<span>共有 " + maxentries + " 条记录,当前第 <b>" + (current_page + 1) + "</b>/" + np + " 页</span>");
91
92 // 产生"Previous"-链接
93 if(opts.prev_text && (current_page > 0 || opts.prev_show_always)){
94 appendItem(current_page-1,{text:opts.prev_text, classes:"prev"});
95 }
96 // 产生起始点
97 if (interval[0] > 0 && opts.num_edge_entries > 0)
98 {
99 var end = Math.min(opts.num_edge_entries, interval[0]);
100 for(var i=0; i<end; i++) {
101 appendItem(i);
102 }
103 if(opts.num_edge_entries < interval[0] && opts.ellipse_text)
104 {
105 jQuery("<a href>"+opts.ellipse_text+"</a>").appendTo(panel);
106 }
107 }
108 // 产生内部的些链接
109 for(var i=interval[0]; i<interval[1]; i++) {
110 appendItem(i);
111 }
112 // 产生结束点
113 if (interval[1] < np && opts.num_edge_entries > 0)
114 {
115 if(np-opts.num_edge_entries > interval[1]&& opts.ellipse_text)
116 {
117 jQuery("<a href>"+opts.ellipse_text+"</a>").appendTo(panel);
118 }
119 var begin = Math.max(np-opts.num_edge_entries, interval[1]);
120 for(var i=begin; i<np; i++) {
121 appendItem(i);
122 }
123
124 }
125 // 产生 "Next"-链接
126 if(opts.next_text && (current_page < np-1 || opts.next_show_always)){
127 appendItem(current_page+1,{text:opts.next_text, classes:"next"});
128 }
129 }
130
131 //从选项中提取current_page
132 var current_page = opts.current_page;
133 //创建一个显示条数和每页显示条数值
134 maxentries = (!maxentries || maxentries < 0)?1:maxentries;
135 opts.items_per_page = (!opts.items_per_page || opts.items_per_page < 0)?1:opts.items_per_page;
136 //存储DOM元素,以方便从所有的内部结构中获取
137 var panel = jQuery(this);
138 // 获得附加功能的元素
139 this.selectPage = function(page_id){ pageSelected(page_id);}
140 this.prevPage = function(){
141 if (current_page > 0) {
142 pageSelected(current_page - 1);
143 return true;
144 }
145 else {
146 return false;
147 }
148 }
149 this.nextPage = function(){
150 if(current_page < numPages()-1) {
151 pageSelected(current_page+1);
152 return true;
153 }
154 else {
155 return false;
156 }
157 }
158 // 所有初始化完成,绘制链接
159 drawLinks();
160 // 回调函数
161 //opts.callback(current_page, this);
162 });
163 }代码还是比较容易看明白的,可以根据自己需要修改,这里使用的是自己的样式
样式代码

1 .pages {display: inline-block; overflow: hidden;padding: 15px 0;text-align: center; width:100%; margin:50px 0;}
2 .pages b{ color:#e75f49;}
3 .pages a { color:#666; border: 1px solid #e5e5e5;cursor: pointer;font-size: 12px;margin-right: 5px; padding: 8px 12px; text-decoration: none; background-color:#fafafa;}
4 .pages .currentPage{ background-color: #00a0e9; border: 1px solid #00a0e9;color: #fff; font-weight: bold;}显示效果如下:

原来的css样式:

1 .pagination a {
2 text-decoration: none;
3 border: 1px solid #AAE;
4 color: #15B;
5 }
6
7 .pagination a, .pagination span {
8 display: inline-block;
9 padding: 0.1em 0.4em;
10 margin-right: 5px;
11 margin-bottom: 5px;
12 }
13
14 .pagination .current {
15 background: #26B;
16 color: #fff;
17 border: 1px solid #AAE;
18 }
19
20 .pagination .current.prev, .pagination .current.next{
21 color:#999;
22 border-color:#999;
23 background:#fff;
24 }可以根据自己设计显示样式
2、使用方法
2.1、html显示

1 <div class="second-ul-ctn"> 2 <ul class="second-ul" id="ulProducts"> 3 </ul> 4 <div class="pages"> 5 <input type="hidden" id="hideTotalCount" /> 6 <div id="Pagination" class="pagination"> 7 </div> 8 </div> 9 </div>
ulProducts中放的是要显示的数据,生成的分页的工具条是放在Pagination中的
2.2 javascript代码
$(function () {
searchMyme(0);
pageInit();
$("#btnSearch").on("click", function () {
searchMyme(0);
pageInit();
return false;
});
});
function searchMyme(page, pageination) {
var month = $("#btnMonth").val();
var obj = {
Month: month,
OpType: "getme",
page: (page + 1)
, rows: 10
};
var url = "../../Controler/FinaceMo/GetStaffIncome_H.ashx";
$.get(url, obj, function (data) {
$("#tbIncome").empty();
var obj = JSON.parse(data);
var total = obj.Total;
$("#hideTotalCount").val(total);
var arrHtml = [];
$.each(obj.Rows, function (i, data) {
arrHtml.push("<tr><td>" + (i + 1) + "</td>");
arrHtml.push("<td>" + data.Account + "</td>");
arrHtml.push("<td>" + data.Name + "</td>");
arrHtml.push("<td>" + data.Month + "</td>");
arrHtml.push("<td>" + data.IncomeAmount + "</td>");
arrHtml.push("<td><a href='MyDetail.aspx?Account="+data.Account+"&Month="+data.Month+"' class='a-blue'>查看明细</a></td></tr>");
});
$("#tbIncome").append(arrHtml.join(''));
});
};
function pageInit() {
var totalCount = $("#hideTotalCount").val();
$("#Pagination").pagination(parseInt(totalCount), {
items_per_page: 10,
//current_page: 1,//当前选中的页面默认是0,表示第1页
num_edge_entries: 2,//两侧显示的首尾分页的条目数,默认为0,好像是尾部显示的个数
num_display_entries: 2,//连续分页主体部分显示的分页条目数,默认是10
link_to: "javascript:void(0)",//分页的链接
prev_text: "上一页",
next_text: "下一页",
prev_show_always: true,
next_show_always: true,
callback: searchMyIncome
});
}searchMyme是获取分页的数据,将总数放到一个隐藏的控件中,总数分页控件需要使用,这里ajax调用需要同步执行,不然取不到返回的总数
pageInit() 就是初始化控件

