1

我现在被困了几天,试图开发一种方法来管理搜索引擎中的结果页面(就像谷歌分页系统一样)。

我有结果的总数、当前页面(从 1...开始到...最后一页)和每页的结果数(假设每页 10 个)。

在 JSP 结果页面的底部,我想显示页面的表格数据,如下所示:

假设搜索引擎返回了 470 个结果。- 基于“每页 10 个结果”,我们将总共有 47 页 (470 / 10)

这就是我想要展示的

"previous 2 3 4 5 6 7 8 9 10 Next" > 当我们点击第 10 页时,应该会发生以下情况:

“上一个 5 6 7 8 9 10 11 12 13 14 下一个”如果点击第 14 页,则:

“上一个 9 10 11 12 13 14 15 16 17 18 下一个”以此类推...

我已经设法做到以下几点

public class Test {

public static int [] getIntervalNumberPages(
        final int pNumberHits,
        final int pNumberTotalHits,
        final int pNumberCurrentPage,
        final int pNumberResultsPerPage) {

    // Page interval
    final int NB_PAGES_INTERVAL = 10;

    // Initialise table
    int [] vResult = new int [0];

    // If no results found or if number of documents per page = 0
    if (pNumberHits != 0 && pNumberResultsPerPage != 0) {
        // Total number of pages
        int vNumberTotalPages = (int) java.lang.Math.ceil(pNumberTotalHits / (double) pNumberResultsPerPage);
        // First number of the list
        int vPremierNumero = 0;

        // Last number of the list
        int vDernierNumero = 0;
        // managing multiples
        if (pNumberCurrentPage >= NB_PAGES_INTERVAL && pNumberCurrentPage % NB_PAGES_INTERVAL == 0) {
            vPremierNumero = (pNumberCurrentPage / NB_PAGES_INTERVAL - 1) * NB_PAGES_INTERVAL + 1;
            vDernierNumero = java.lang.Math.min(vNumberTotalPages, (pNumberCurrentPage / NB_PAGES_INTERVAL - 1) * NB_PAGES_INTERVAL + NB_PAGES_INTERVAL);
        } else {
            vPremierNumero = pNumberCurrentPage / NB_PAGES_INTERVAL * NB_PAGES_INTERVAL + 1;
            vDernierNumero = java.lang.Math.min(vNumberTotalPages, pNumberCurrentPage / NB_PAGES_INTERVAL * NB_PAGES_INTERVAL + NB_PAGES_INTERVAL);
        }
        vResult = new int [vDernierNumero - vPremierNumero + 1];
        // Fill in table
        for (int vCpt = 0; vCpt < vResult.length; vCpt++) {
            vResult [vCpt] = vPremierNumero + vCpt;
        }
    }

return vResult;
}
}

但是我的代码是这样工作的:

“1 2 3 4 5 6 7 8 9 10 Next”如果我点击第 10 页 > “Previous 11 12 13 14 15 16 17 18 19 20 Next” 以此类推

有人可以帮我吗?

4

4 回答 4

5

你的问题只是简单的数学。您重新编号的方式与您描述的要求不符。

 vPremierNumero = (pNumberCurrentPage / NB_PAGES_INTERVAL - 1) * NB_PAGES_INTERVAL

所以让我们插入一些数字

 pNumberCurrentPage = 10
 NB_PAGES_INTERVAL = 10

 vPremierNumero = 10/9 * 10 = 100/9 = 11

所以这就是为什么你的第一页是 11。你想把它移动 NB_PAGES_INTERVAL/2 以便你点击的数字在范围的中间。

于 2012-06-13T13:33:48.363 回答
2

你的数学是关闭的。你可能想要类似的东西

vPremierNumero = Math.max(1, pNumberCurrentPage - (NB_PAGES_INTERVAL / 2)); vDernierNumero = Math.min(vPremierNumero + NB_PAGES_INTERVAL, vNumberTotalPages);

于 2012-06-13T13:37:15.927 回答
0

您计算的第一个和最后一个数字不正确,请尝试以下操作:

    // 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;
        }
    }
于 2012-06-13T13:43:47.610 回答
0

这是您尝试做的基本数学运算,您可以插入其余部分

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 };
}
于 2012-06-13T13:38:16.893 回答