js實現(xiàn)分頁插件,類似于京東的分頁查詢:

近期需要做一個平臺,需要用到分頁,

原型以及前端給的頁面是純html實現(xiàn)的,沒有用到分頁插件,于是就自己參照京東商品頁的那種方式寫了一個。1期先寫死了 最多展示7個頁簽。后期考慮制作可定制化的。

源碼如下:

vardefaultIndex=0;

varpageIndex=0;

varpageSize=20;

/*參數(shù)1:當前頁,參數(shù)2:總頁數(shù),參數(shù)3:綁定查詢函數(shù),需要自定義實現(xiàn),會傳入查詢頁數(shù)和每頁查詢數(shù)*/

functionpageUtil(currentPage,totalPage,queryFunction){

varidx=0;

varpageIdxNum=0;//擁擠統(tǒng)計當前有幾個頁面標簽,如果不足會補齊

currentPage++;

varpage='<上一頁';

if(totalPage>7){

page+='1';

pageIdxNum++;

if((currentPage-2)>2){

page+='···';

pageIdxNum++;

}else if((currentPage-2)==2){

page+='2';

pageIdxNum++;

};

if((totalPage-currentPage)<3){

varvar1=totalPage-currentPage;

varvar3=4-var1;

while(var3>1){

idx=currentPage-var3;

page+=''+idx+'';

var3--;

pageIdxNum++;

}

}

if(currentPage>2&& currentPage<=totalPage){

idx=parseInt(currentPage)-1;

page+=''+idx+'';

pageIdxNum++;

}

if(currentPage>1&& currentPage!=totalPage ){

idx=parseInt(currentPage);

page+=''+idx+'';

pageIdxNum++;

}

if((currentPage+1)

idx=parseInt(currentPage)+1;

page+=''+idx+'';

pageIdxNum++;

}

while(pageIdxNum<5)//在最后2個標簽之前,需要補足不全的頁數(shù)

{

idx++;

page+=''+idx+'';

pageIdxNum++;

}

if((currentPage+3)==totalPage){

idx=totalPage-1;

page+=''+idx+'';

}else if((currentPage+3)

page+='···';

}

page+=''+totalPage+'';

}else{

varvar4=1;

while(var4<=totalPage){

page+=''+var4+'';

var4++;

}

}

page+='';

$("#uipage").html(page);

$.each($("#uipagea"),function(index,element){

$(this).removeClass('current');

varidx1=$(this).text();

if(idx1==currentPage){

$(this).addClass('current');

}

$(this).bind('click',function(){

varidx=$(this).text();

if(idx!='···'){

if($(this).attr('class')=='pre'){

pageIndex=(currentPage-1);

}else if($(this).attr('class')=='next'){

pageIndex=(currentPage+1);

}else{

pageIndex=idx;

}

}else{

if($(this).attr('class')=='more'){

pageIndex=(currentPage+2);

}else{

pageIndex=(currentPage-2);

}

}

queryFunction((pageIndex-1),pageSize);

});

});

if(currentPage==1){

$("#uipagea:eq(0)").off("click");

$("#uipagea:eq(0)").addClass("disable");

};

if(currentPage==totalPage || totalPage ==0){

$("#uipagea:last").off("click")

$("#uipagea:last").addClass("disable");

}

}



使用方法如下:

自己需要實現(xiàn)queryFunction(idx,pageNum) ?參數(shù)1是第幾頁 ?參數(shù)2是每頁展示數(shù)量:

demo:

function ?queryFunction(idx,pageNum){

????????ajax({

? ? ? ? .....略

? ? success:function(data){

。。。數(shù)據(jù)渲染

? ??pageUtil(currentPage,totalPage,queryFunction);//返回傳入當前頁,總頁數(shù),然后把自己查詢這個函數(shù)傳入進去,即可

。。。數(shù)據(jù)提取

????}

????????})

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容