js分页模板

时间:2024-03-24 21:02:56

/**

*参数说明:

*currentPage:当前页数

*countPage:总页数

*changeMethod:执行java后台代码的js函数,即是改变分页数据的js函数

*/

function pagination(currentPage,countPage,changeMethod){
  var str = "";
  var countNum;
  //head
        if (currentPage == 1) {
         str += "<a class=\"pagePre\">首页</a>";
            str += "<a >&lt;</a>";
        }
        else {
         str += "<a href=\"javascript:void(0)\" onclick=\""+changeMethod+"(1)\"  class=\"pagePre\">首页</a>";
            str += "<a href=\"javascript:void(0)\" onclick=\""+changeMethod+"("+(currentPage-1)+")\">&lt;</a>";
        }
  
        //pageNum
  if(currentPage < 4||countPage < 6){
   countNum = 1;
  }else if(currentPage + 2 > countPage){
   countNum = countPage - 4;
  }else{
   countNum = currentPage - 2;
  }
  
  var countPage1 = parseInt(countPage)+1;
  var countNum1 = parseInt(countNum);
  //body
  for(var i = countNum1; i < countPage1; i++){
   
   if(i==currentPage){ 
    str += "<span class=\"cur\">"+i+"</span>";
   }else{
    str += "<a href=\"javascript:void(0)\" onclick=\""+changeMethod+"("+i+")\">"+i+"</a>";
   }
   
   if(i == countNum + 4){
    break;
   }
   
  }
        //end
        if (currentPage == countPage) {
         str += "<a>&gt;</a>";
            str += "<a class=\"pagePre\">尾页</a>";
        }
        else {
            str += "<a href=\"javascript:void(0)\" onclick=\""+changeMethod+"("+(currentPage+1)+")\">&gt;</a>";
            str += "<a href=\"javascript:void(0)\" onclick=\""+changeMethod+"("+(countPage1-1)+")\" class=\"pagePre\">尾页</a>";
        }
  if(countPage1>2){
   $(".page").empty().append(str);
  }
 }