frontpaging.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. var FrontPaging = function(nodeid,datas,pageSize,callBack){
  2. this.currentPage = 1;
  3. this.totalPages = 0;
  4. this.count = 0;
  5. //初始化
  6. this.init = function(){
  7. this.totalPages = parseInt((this.count + pageSize - 1) / pageSize);
  8. if(this.totalPages > 1){
  9. var html = '<nav>'
  10. +'<ul class="pagination"><li';
  11. if(!this.hasPrevPage()){
  12. html += ' class="disabled"';
  13. }
  14. html += ' id="firstPage"><a>&laquo;</a></li><li';
  15. if(!this.hasPrevPage()){
  16. html += ' class="disabled"';
  17. }
  18. html += ' id="prevPage">'
  19. +'<a>&lsaquo;</a>'
  20. +'</li>';
  21. for(var i=0;i<this.totalPages;i++){
  22. html += '<li id="pageNum-'+(i+1)+'"';
  23. if(i == 0){
  24. html += ' class="disabled"';
  25. }else if(i >= 10){
  26. html += ' class="hide"';
  27. }
  28. html += '><a>'+(i+1)+'</a></li>';
  29. }
  30. html += '<li';
  31. if(!this.hasNextPage()){
  32. html += ' class="disabled"';
  33. }
  34. html += ' id="nextPage"><a>&rsaquo;</a></li><li';
  35. if(!this.hasNextPage()){
  36. html += ' class="disabled"';
  37. }
  38. html += ' id="lastPage"><a>&raquo;</a></li></ul></nav>';
  39. $("#"+nodeid).html(html);
  40. var thisClass = this;
  41. var getThisClass = function(){
  42. return thisClass;
  43. }
  44. $("#"+nodeid+" #firstPage").click(function(){
  45. if($(this).hasClass("disabled")){
  46. return;
  47. }
  48. thisClass.currentPage = 1;
  49. $(this).addClass("disabled");
  50. $("#"+nodeid+" #prevPage").addClass("disabled");
  51. $("#"+nodeid+" #nextPage,#"+nodeid+" #lastPage").removeClass("disabled");
  52. thisClass.commonMethod();
  53. });
  54. $("#"+nodeid+" #prevPage").click(function(){
  55. if($(this).hasClass("disabled")){
  56. return;
  57. }
  58. $("#"+nodeid+" #pageNum-"+(--thisClass.currentPage)).addClass("disabled");
  59. if(thisClass.hasPrevPage()){
  60. $(this).removeClass("disabled");
  61. $("#"+nodeid+" #firstPage").removeClass("disabled");
  62. }else{
  63. $(this).addClass("disabled");
  64. $("#"+nodeid+" #firstPage").addClass("disabled");
  65. }
  66. $("#"+nodeid+" #nextPage,#"+nodeid+" #lastPage").removeClass("disabled");
  67. thisClass.commonMethod();
  68. });
  69. $("#"+nodeid+" #nextPage").click(function(){
  70. if($(this).hasClass("disabled")){
  71. return;
  72. }
  73. $("#"+nodeid+" #pageNum-"+(++thisClass.currentPage)).addClass("disabled");
  74. if(thisClass.hasNextPage()){
  75. $(this).removeClass("disabled");
  76. $("#"+nodeid+" #lastPage").removeClass("disabled");
  77. }else{
  78. $(this).addClass("disabled");
  79. $("#"+nodeid+" #lastPage").addClass("disabled");
  80. }
  81. $("#"+nodeid+" #firstPage,#prevPage").removeClass("disabled");
  82. thisClass.commonMethod();
  83. });
  84. $("#"+nodeid+" #lastPage").click(function(){
  85. if($(this).hasClass("disabled")){
  86. return;
  87. }
  88. thisClass.currentPage = thisClass.totalPages;
  89. $("#"+nodeid+" #firstPage,#"+nodeid+" #prevPage").removeClass("disabled");
  90. $("#"+nodeid+" #nextPage,#"+nodeid+" #lastPage").addClass("disabled");
  91. thisClass.commonMethod();
  92. });
  93. $("#"+nodeid+" [id^='pageNum-']").click(function(){
  94. if($(this).hasClass("disabled")){
  95. return;
  96. }
  97. thisClass.currentPage = parseInt(this.id.split("-")[1]);
  98. if(thisClass.hasPrevPage()){
  99. $("#"+nodeid+" #firstPage,#"+nodeid+" #prevPage").removeClass("disabled");
  100. }else{
  101. $("#"+nodeid+" #firstPage,#"+nodeid+" #prevPage").addClass("disabled");
  102. }
  103. if(thisClass.hasNextPage()){
  104. $("#"+nodeid+" #nextPage,#"+nodeid+" #lastPage").removeClass("disabled");
  105. }else{
  106. $("#"+nodeid+" #nextPage,#"+nodeid+" #lastPage").addClass("disabled");
  107. }
  108. thisClass.commonMethod();
  109. });
  110. }
  111. }
  112. this.commonMethod = function(){
  113. $("#"+nodeid+" [id^='pageNum-']").removeClass("disabled");
  114. $("#"+nodeid+" #pageNum-"+this.currentPage).addClass("disabled");
  115. if(this.currentPage == 1){
  116. $("#"+nodeid+" [id^='pageNum-']").addClass("hide");
  117. for(var i=1;i<=10;i++){
  118. if(i > this.totalPages){
  119. break;
  120. }
  121. $("#"+nodeid+" #pageNum-"+i).removeClass("hide");
  122. }
  123. }else if(this.currentPage >= 5){
  124. var start = this.currentPage-5;
  125. var end = this.currentPage+5;
  126. if(this.currentPage > this.totalPages - 5){
  127. start = this.totalPages - 10;
  128. end = this.totalPages;
  129. }
  130. for(var i=1;i<=start;i++){
  131. $("#"+nodeid+" #pageNum-"+i).addClass("hide");
  132. }
  133. for(var i=start+1;i<=end;i++){
  134. $("#"+nodeid+" #pageNum-"+i).removeClass("hide");
  135. }
  136. for(var i=end+1;i<=this.totalPages;i++){
  137. $("#"+nodeid+" #pageNum-"+i).addClass("hide");
  138. }
  139. }
  140. this.getDatas();
  141. }
  142. //从服务端获取数据
  143. this.getDatas = function(){
  144. if(this.totalPages == 0){
  145. this.count = datas.length;
  146. this.init();
  147. }
  148. var start = (this.currentPage - 1) * pageSize;
  149. var end = start + pageSize;
  150. callBack(datas.slice(start,end));
  151. }
  152. //是否有上一页
  153. this.hasPrevPage = function(){
  154. if(this.currentPage > 1){
  155. return true;
  156. }
  157. return false;
  158. }
  159. //是否有下一页
  160. this.hasNextPage = function(){
  161. if(this.currentPage < this.totalPages){
  162. return true;
  163. }
  164. return false;
  165. }
  166. //第一次获取数据和总数
  167. this.getDatas();
  168. }