Java 浮点数在这里,这个技巧会产生相同的效果 int vNumberTotalPages=(pNumberTotalHits+pNumberResultsPerPage-1)/pNumberResultsPerPage; int firstIndex=pNumberCurrentPage-(NB_PAGES_INTERVAL/2); int lastIndex=firstIndex+NB\u PAGES\u INTERVAL-1; //名单上的第一个数字 int-vPremierNumero=Math.max(第一个索引,1); //名单的最后一个号码 int vDernierNumero=Math.min(lastIndex,vNumberTotalPages); vResult=new int[vDernierNumero-vPremierNumero+1]; //填表 对于(int-vCpt=0;vCpt
您对第一个和最后一个数字的计算不正确,请尝试以下操作:Java 浮点数在这里,这个技巧会产生相同的效果 int vNumberTotalPages=(pNumberTotalHits+pNumberResultsPerPage-1)/pNumberResultsPerPage; int firstIndex=pNumberCurrentPage-(NB_PAGES_INTERVAL/2); int lastIndex=firstIndex+NB\u PAGES\u INTERVAL-1; //名单上的第一个数字 int-vPremierNumero=Math.max(第一个索引,1); //名单的最后一个号码 int vDernierNumero=Math.min(lastIndex,vNumberTotalPages); vResult=new int[vDernierNumero-vPremierNumero+1]; //填表 对于(int-vCpt=0;vCpt,java,pagination,Java,Pagination,您对第一个和最后一个数字的计算不正确,请尝试以下操作: // If no results found or if number of documents per page = 0 if (pNumberHits != 0 && pNumberResultsPerPage != 0) { // Total number of pages // You shouldn't create an intermediate floating
// If no results found or if number of documents per page = 0
if (pNumberHits != 0 && pNumberResultsPerPage != 0) {
// Total number of pages
// You shouldn't create an intermediate floating number here, this trick causes the same effect
int vNumberTotalPages = (pNumberTotalHits + pNumberResultsPerPage - 1) / pNumberResultsPerPage;
int firstIndex = pNumberCurrentPage - (NB_PAGES_INTERVAL / 2);
int lastIndex = firstIndex + NB_PAGES_INTERVAL - 1;
// First number of the list
int vPremierNumero = Math.max(firstIndex, 1);
// Last number of the list
int vDernierNumero = Math.min(lastIndex, vNumberTotalPages);
vResult = new int [vDernierNumero - vPremierNumero + 1];
// Fill in table
for (int vCpt = 0; vCpt < vResult.length; vCpt++) {
vResult [vCpt] = vPremierNumero + vCpt;
}
}
//如果未找到结果或如果每页文档数=0
如果(pNumberHits!=0&&pNumberResultsPerPage!=0){
//总页数
//你不应该在这里创建一个中间浮点数,这个技巧会产生同样的效果
int vNumberTotalPages=(pNumberTotalHits+pNumberResultsPerPage-1)/pNumberResultsPerPage;
int firstIndex=pNumberCurrentPage-(NB_PAGES_INTERVAL/2);
int lastIndex=firstIndex+NB\u PAGES\u INTERVAL-1;
//名单上的第一个数字
int-vPremierNumero=Math.max(第一个索引,1);
//名单的最后一个号码
int vDernierNumero=Math.min(lastIndex,vNumberTotalPages);
vResult=new int[vDernierNumero-vPremierNumero+1];
//填表
对于(int-vCpt=0;vCpt
非常感谢您。这,以一种非常简单的方式解决了这个问题。再次感谢您宝贵的帮助。@user1353307:您在这里并不是字面上说谢谢
,而是向上投有用的、正确的、好的答案(向上三角形)。您接受的对您帮助最大或最好的答案(勾选符号)。非常感谢。这,以一种非常简单的方式解决了这个问题。再次感谢您宝贵的帮助。@user1353307:您在这里并不是字面上说谢谢
,而是向上投有用的、正确的、好的答案(向上三角形)。对你帮助最大或最好的答案(勾选符号)。
vPremierNumero = (pNumberCurrentPage / NB_PAGES_INTERVAL - 1) * NB_PAGES_INTERVAL
pNumberCurrentPage = 10
NB_PAGES_INTERVAL = 10
vPremierNumero = 10/9 * 10 = 100/9 = 11
public static int[] getPagination(
int currentPage,
int maxPerPage,
int totalResults) throws IOException {
final int PAGES_BEFORE_AFTER = 5;
final int MAX_PER_PAGE = 20;
if (maxPerPage <= 0) {
maxPerPage = MAX_PER_PAGE;
}
int startRecords = 0;
int endRecords = totalResults;
boolean has_pagination = (totalResults > maxPerPage);
if (has_pagination) {
int pageCount = totalResults / maxPerPage;
if ((pageCount * maxPerPage) < totalResults) {
pageCount++;
}
startRecords = (currentPage * maxPerPage) - maxPerPage;
endRecords = (startRecords + maxPerPage);
if (totalResults <= maxPerPage) {
startRecords = 0;
endRecords = totalResults;
} else if (endRecords > totalResults) {
endRecords = totalResults;
}
boolean prev_enabled = ((currentPage != 0) && (currentPage != 1));
boolean next_enabled = ((pageCount != 0) && (pageCount != currentPage));
int startIndex = 0;
int stopIndex = pageCount;
if (currentPage <= PAGES_BEFORE_AFTER) {
startIndex = 0;
} else {
startIndex = (currentPage - PAGES_BEFORE_AFTER) - 1;
}
if ((currentPage + PAGES_BEFORE_AFTER) < pageCount) {
stopIndex = currentPage + PAGES_BEFORE_AFTER;
} else {
stopIndex = pageCount;
}
for (int x = startIndex; x < stopIndex; x++) {
boolean disabled = (currentPage == (x + 1));
// buttons
}
}
return new int[] { startRecords, endRecords };
}
// If no results found or if number of documents per page = 0
if (pNumberHits != 0 && pNumberResultsPerPage != 0) {
// Total number of pages
// You shouldn't create an intermediate floating number here, this trick causes the same effect
int vNumberTotalPages = (pNumberTotalHits + pNumberResultsPerPage - 1) / pNumberResultsPerPage;
int firstIndex = pNumberCurrentPage - (NB_PAGES_INTERVAL / 2);
int lastIndex = firstIndex + NB_PAGES_INTERVAL - 1;
// First number of the list
int vPremierNumero = Math.max(firstIndex, 1);
// Last number of the list
int vDernierNumero = Math.min(lastIndex, vNumberTotalPages);
vResult = new int [vDernierNumero - vPremierNumero + 1];
// Fill in table
for (int vCpt = 0; vCpt < vResult.length; vCpt++) {
vResult [vCpt] = vPremierNumero + vCpt;
}
}