Tuesday, October 9, 2012

Paging Implementation for the Current Board

Paging Implementation for the Current Board

To implement the required functionality for the current board, a total of 3 variables were identified as necessary. Using these 3 variables, various data points were calculated. The implementation was done through a dedicated paging class as outlined below:

Essential Variables for Paging

  1. Current Page Number (pageNumber)
    The page number currently being viewed.

  2. Number of Items per Page (pageSize)
    The number of data items to display on a single page.

  3. Total Number of Items (totalCount)
    The total count of data items available.

Derived Data Points

Using the three essential variables, the following data points were calculated:

  1. Total Number of Pages (totalPage)

    • The total number of pages required to display all items.
    • Formula: totalPage = ceil(totalCount / pageSize)
  2. Start Page Number (minPageNumber)

    • The first page number to display in the current pagination block.
  3. End Page Number (maxPageNumber)

    • The last page number to display in the current pagination block.
  4. Previous Page Number (prevPageNumber)

    • The page number immediately preceding the current page, if available.
  5. Next Page Number (nextPageNumber)

    • The page number immediately following the current page, if available.
  6. First Page Number (firstPageNumber)

    • The very first page in the pagination (always 1).
  7. Last Page Number (lastPageNumber)

    • The very last page in the pagination, equal to totalPage.

Java class


class Paging {
    int pageNumber;       // Current page number
    int pageSize;         // Items per page
    int totalCount;       // Total items count
    int totalPage;        // Total number of pages
    int minPageNumber;    // Start page in the current pagination
    int maxPageNumber;    // End page in the current pagination
    int prevPageNumber;   // Previous page number
    int nextPageNumber;   // Next page number
    int firstPageNumber;  // Always 1
    int lastPageNumber;   // Last page number

    public Paging(int pageNumber, int pageSize, int totalCount) {
        this.pageNumber = pageNumber;
        this.pageSize = pageSize;
        this.totalCount = totalCount;
        this.totalPage = (int) Math.ceil((double) totalCount / pageSize);
        this.firstPageNumber = 1;
        this.lastPageNumber = totalPage;
        this.minPageNumber = Math.max(1, pageNumber - 2); // Example logic
        this.maxPageNumber = Math.min(totalPage, pageNumber + 2); // Example logic
        this.prevPageNumber = pageNumber > 1 ? pageNumber - 1 : -1;
        this.nextPageNumber = pageNumber < totalPage ? pageNumber + 1 : -1;
    }
}

Class Usage

Paging paging = new Paging(currentPageNumber, pageSize, totalRows);


Freemarker Template Example

<div class="pagination pagination-centered">
 <ul>
  <#if paging.minPageNumber != 0 && paging.maxPageNumber != 0> <#if
  paging.prevPageNumber != 0>
  <li><a href="?pageno=${paging.prevPageNumber}">Prev</a></li> </#if>
  <#list paging.minPageNumber..paging.maxPageNumber as i> <#if i =
  paging.pageNumber>
  <li class="active"><#else>
  <li></#if><a href="?pageno=${i}">${i}</a></li> </#list> <#if
  paging.nextPageNumber != 0>
  <li><a href="?pageno=${paging.nextPageNumber}">Next</a></li> </#if>
  </#if>

 </ul>
</div>