2012-06-13 11 views
1

我被困了幾天了,試圖開發一種管理搜索引擎結果頁面的方法(就像谷歌分頁系統)。java中的分頁

我有結果總數,當前頁面(從1 ...開始到最後一頁)以及每頁結果數量(比方說每頁10個)。

在JSP結果頁面的底部,我想如下顯示的頁面的表格數據:

假設搜索引擎返回結果470。 - 基於 「每頁10分的結果」,我們將有一個總的47頁(十分之四百七十零)

這是我想顯示什麼

「前2 3 4 5 6 7 8 9 10下一頁「>當我們點擊第10頁上的,這是應該發生的事情:

‘以前的5 6 7 8 9 10 11 12 13 14下一頁’如果第14頁被點擊,則:

」以前9 10 11 12 13 14 15 16 17 18 Next「ans such ...

我已經設法做到了下面

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下一頁」 如果我點擊頁面10> 「上一頁上11 12 13 14 15 16 17 18 19 20下一個「ans so

有人可以幫我嗎?

回答

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轉向這讓你點擊的數量是在範圍的中間。

2

你的數學是關閉的。你可能想是這樣

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

+0

非常感謝你。這以一種非常簡單的方式解決了這個問題。再次感謝您寶貴的幫助。 – user1353307

+0

非常歡迎。:) – Keppil

+0

@ user1353307:你不會在這裏說'謝謝',但是upvote有用,正確和好的答案(向上的三角形)。這有助於你最多的還是你接受最好的答案之一(登記號)。 –

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