page.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * Created by zxm on 2017/3/31.
  3. */
  4. $.fn.extend({
  5. "initPage":function(listCount,currentPage,fun){
  6. var maxshowpageitem = $(this).attr("maxshowpageitem");
  7. if(maxshowpageitem!=null&&maxshowpageitem>0&&maxshowpageitem!=""){
  8. page.maxshowpageitem = maxshowpageitem;
  9. }
  10. var pagelistcount = $(this).attr("pagelistcount");
  11. if(pagelistcount!=null&&pagelistcount>0&&pagelistcount!=""){
  12. page.pagelistcount = pagelistcount;
  13. }
  14. var pageId = $(this).attr("id");
  15. page.pageId=pageId;
  16. if(listCount<0){
  17. listCount = 0;
  18. }
  19. if(currentPage<=0){
  20. currentPage=1;
  21. }
  22. page.setPageListCount(listCount,currentPage,fun);
  23. }
  24. });
  25. var page = {
  26. "pageId":"",
  27. "maxshowpageitem":5,//最多显示的页码个数
  28. "pagelistcount":10,//每一页显示的内容条数
  29. /**
  30. * 初始化分页界面
  31. * @param listCount 列表总量
  32. */
  33. "initWithUl":function(listCount,currentPage){
  34. var pageCount = 1;
  35. if(listCount>=0){
  36. var pageCount = listCount%page.pagelistcount>0?parseInt(listCount/page.pagelistcount)+1:parseInt(listCount/page.pagelistcount);
  37. }
  38. var appendStr = page.getPageListModel(pageCount,currentPage);
  39. $("#"+page.pageId).html(appendStr);
  40. },
  41. /**
  42. * 设置列表总量和当前页码
  43. * @param listCount 列表总量
  44. * @param currentPage 当前页码
  45. */
  46. "setPageListCount":function(listCount,currentPage,fun){
  47. listCount = parseInt(listCount);
  48. currentPage = parseInt(currentPage);
  49. page.initWithUl(listCount,currentPage);
  50. page.initPageEvent(listCount,fun);
  51. fun(currentPage);
  52. },
  53. "initPageEvent":function(listCount,fun){
  54. $("#"+page.pageId +">li[class='pageItem']").on("click",function(){
  55. page.setPageListCount(listCount,$(this).attr("page-data"),fun);
  56. });
  57. },
  58. "getPageListModel":function(pageCount,currentPage){
  59. var prePage = currentPage-1;
  60. var nextPage = currentPage+1;
  61. var prePageClass ="pageItem";
  62. var nextPageClass = "pageItem";
  63. if(prePage<=0){
  64. prePageClass="pageItemDisable";
  65. }
  66. if(nextPage>pageCount){
  67. nextPageClass="pageItemDisable";
  68. }
  69. var appendStr ="";
  70. appendStr+="<li class='"+prePageClass+"' page-data='1' page-rel='firstpage'>首页</li>";
  71. appendStr+="<li class='"+prePageClass+"' page-data='"+prePage+"' page-rel='prepage'>&lt;上一页</li>";
  72. var miniPageNumber = 1;
  73. if(currentPage-parseInt(page.maxshowpageitem/2)>0&&currentPage+parseInt(page.maxshowpageitem/2)<=pageCount){
  74. miniPageNumber = currentPage-parseInt(page.maxshowpageitem/2);
  75. }else if(currentPage-parseInt(page.maxshowpageitem/2)>0&&currentPage+parseInt(page.maxshowpageitem/2)>pageCount){
  76. miniPageNumber = pageCount-page.maxshowpageitem+1;
  77. if(miniPageNumber<=0){
  78. miniPageNumber=1;
  79. }
  80. }
  81. var showPageNum = parseInt(page.maxshowpageitem);
  82. if(pageCount<showPageNum){
  83. showPageNum = pageCount
  84. }
  85. for(var i=0;i<showPageNum;i++){
  86. var pageNumber = miniPageNumber++;
  87. var itemPageClass = "pageItem";
  88. if(pageNumber==currentPage){
  89. itemPageClass = "pageItemActive";
  90. }
  91. appendStr+="<li class='"+itemPageClass+"' page-data='"+pageNumber+"' page-rel='itempage'>"+pageNumber+"</li>";
  92. }
  93. appendStr+="<li class='"+nextPageClass+"' page-data='"+nextPage+"' page-rel='nextpage'>下一页&gt;</li>";
  94. appendStr+="<li class='"+nextPageClass+"' page-data='"+pageCount+"' page-rel='lastpage'>尾页</li>";
  95. return appendStr;
  96. }
  97. }