jquery.pager.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. jQuery.pagefoot =
  2. {
  3. //生成分页脚
  4. create: function(_this, s) {
  5. var pageCount = 0;
  6. pageCount = (s.count / s.pagesize <= 0) ? 1 : (parseInt(s.count / s.pagesize) + ((s.count % s.pagesize > 0) ? 1 : 0));
  7. s.current = (s.current > pageCount) ? pageCount : s.current
  8. var strPage = "<label>每页" + s.pagesize + "条/总" + s.count + "条</label>";
  9. if (s.current <= 1) {
  10. strPage += "<span class=\"disabled\">" + s.previous + "</span>";
  11. } else {
  12. strPage += "<a href=\"" + (s.current - 1) + "\">" + s.previous + "</a>";
  13. }
  14. var startP = 1;
  15. startP = 1
  16. var anyMore;
  17. anyMore = parseInt(s.displaynum / 2)
  18. var endP = (s.current + anyMore) > pageCount ? pageCount : s.current + anyMore;
  19. var pCount = s.pagesize - s.displaylastNum;
  20. if (s.current > s.displaynum) {
  21. startP = s.current - anyMore;
  22. for (i = 1; i <= s.displaylastNum; i++) {
  23. strPage += "<a href=\"" + i + "\">" + i + "</a>";
  24. }
  25. strPage += "...";
  26. }
  27. if (s.current + s.displaynum <= pageCount) {
  28. endP = s.current + anyMore;
  29. } else {
  30. endP = pageCount;
  31. }
  32. for (i = startP; i <= endP; i++) {
  33. if (s.current == i) {
  34. strPage += "<span class=\"current\">" + i + "</span>";
  35. } else {
  36. strPage += "<a href=\"./" + i + "\">" + i + "</a>";
  37. }
  38. }
  39. if (s.current + s.displaynum <= pageCount) {
  40. strPage += "...";
  41. for (i = pageCount - s.displaylastNum + 1; i <= pageCount; i++) {
  42. strPage += "<a href=\"" + i + "\">" + i + "</a>";
  43. }
  44. }
  45. if (s.current >= pageCount) {
  46. strPage += "<span class=\"disabled\">" + s.next + "</span>";
  47. } else {
  48. strPage += "<a href=\"" + (s.current + 1) + "\">" + s.next + "</a>";
  49. }
  50. $(_this).empty().append(strPage).find("a").click(function() {
  51. var ln = this.href.lastIndexOf("/");
  52. var href = this.href;
  53. var page = parseInt(href.substring(ln + 1, href.length));
  54. s.current = page;
  55. if (!$.pagefoot.paging(page, s.paging))
  56. return false;
  57. $.pagefoot.create(_this, s);
  58. return false;
  59. });
  60. return this;
  61. },
  62. paging: function(page, callback) {
  63. if (callback) {
  64. if (callback(page) == false)
  65. return false;
  66. }
  67. return true;
  68. }
  69. }
  70. jQuery.fn.pagefoot = function(opt) {
  71. var setting = { pagesize: 10 //每页显示的页码数
  72. , count: 0 //数据条数
  73. , css: "mj_pagefoot" //分页脚css样式类
  74. , current: 1 //当前页码
  75. , displaynum: 5 //中间显示页码数
  76. , displaylastNum: 5 //最后显示的页码数
  77. , previous: "上一页" //上一页显示样式
  78. , next: "下一页" //下一页显示样式
  79. , paging: null //分页事件触发时callback函数
  80. };
  81. opt = opt || {}
  82. $.extend(setting, opt);
  83. return this.each(function() {
  84. $(this).addClass(setting.css);
  85. $.pagefoot.create(this, setting);
  86. });
  87. }