1

検索エンジンで結果のページを管理する方法を開発しようとして、数日間立ち往生しています(Googleのページネーションシステムと同じように)。

結果の総数、現在のページ (1 ~ 最後のページ)、および 1 ページあたりの結果数 (1 ページあたり 10 としましょう) があります。

JSP 結果ページの下部に、次のようにページの表形式のデータを表示したいと考えています。

検索エンジンが 470 件の結果を返したとします。- 「1 ページあたり 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 次へ」 ans so on...

私は次のことを行うことができました

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;
}
}

ただし、私のコードは次のように機能します。

10ページをクリックすると「1 2 3 4 5 6 7 8 9 10 次へ」 > 「前へ 11 12 13 14 15 16 17 18 19 20 次へ」など

誰かが私を助けてくれますか??

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 に答える