我正在对我的网络应用程序实施分页,并希望一次只显示5页(第1-5页,第6-10页等)
假设我返回的总页数是100;使用for循环,我可以将我的5页从1-5更新到6-10,直到达到总页数?所以像这样:
numOfElemnets = 5 //pages to be displayed at once
totalPages = 100;
for(var i=0; i<totalPages; i++) {
$("#toolbar .pagination li a").attr('href', 'page=" + i + "'); //div containing <a> elements
}
或者,如果我认为这个概念完全错误,并且有更好的实施方法,请告诉我!
答案 0 :(得分:1)
这将是这样的:
numOfElemnets = 5 //pages to be displayed at once
totalPages = 100; //total pages
currentPage = 3; //this is shouldn't be static, since it's the current page the user is at
/**
* If the user is on page 1 to 5, the first page will be 1. If he's not, then the page will be the current page minus 5.
* This is to stop it from starting the loop from a negative number, since you don't have a -4 page.
*/
if(currentPage > numOfElemnets) {
i = currentPage - numOfElemnets;
} else {
i = 1;
}
// The same thing as above for the end. You will take 5 pages after the current page, but if you're on page 99, you should pick just till 100 that is your limit.
if(currentPage + numOfElemnets > totalPages) {
end = totalPages;
} else {
end = currentPage + numOfElemnets;
}
// I changed the for to a while, since the i parameter had to get out of the for. You can still use a for and give the i as a parameter. Whatever.
while(i <= end) {
$("#toolbar .pagination li a").attr('href', 'page=" + i + "'); //div containing <a> elements
i++;
}