Class ListPager<T>

  • Type Parameters:
    T - The type of item being displayed

    public class ListPager<T>
    extends Object

    This class provides an easy way to page through a filterable list of items from a JSP page.

    It is up to the caller of the class to specify the filter, if required, in the form of a predicate. To ensure that search parameters are maintained when paging through the list, it's also necessary to supply a list of input field identifiers that should be included as part of the request when accessing another page. These field identifiers are used to extract the value of the field, and copy it to a hidden form that is used for the actual submission. This means each search criteria has two form fields associated with it - the visible field that the users types in, and in the hidden form that is submitted.

    The reason for this is that the location of the input fields on the screen may not always be appropriate for a form - for example, if the fields are location within a table, the form must wrap the whole table, which would preclude the use of any other forms within cells in that table.

    These input fields will also be modified so that pressing Enter/Return when the fields has focus will automatically submit the form.

    • Field Detail

      • DESCENDING

        public static final int DESCENDING
        Descending sort (ie 3, 2, 1...).
        See Also:
        Constant Field Values
      • ASCENDING

        public static final int ASCENDING
        Ascending sort (ie 3, 4, 5...).
        See Also:
        Constant Field Values
    • Constructor Detail

      • ListPager

        public ListPager​(javax.servlet.http.HttpServletRequest request,
                         javax.servlet.http.HttpServletResponse response,
                         List<T> items,
                         String... additionalFormFields)
        Creates a unfiltered list pager.
        Parameters:
        request - the request object for the page in question
        response - the response object for the page in question
        items - the list of items to display on the page
        additionalFormFields - 0 or more input field identifiers (NOT form field names) to include in requests for other pages
      • ListPager

        public ListPager​(javax.servlet.http.HttpServletRequest request,
                         javax.servlet.http.HttpServletResponse response,
                         List<T> items,
                         Predicate<T> filter,
                         String... additionalFormFields)
        Creates a unfiltered list pager.
        Parameters:
        request - the request object for the page in question
        response - the response object for the page in question
        items - the complete list of items to display on the page
        filter - the filter to apply to the complete list
        additionalFormFields - 0 or more input field identifiers (NOT form field names) to include in requests for other pages
      • ListPager

        public ListPager​(javax.servlet.http.HttpServletRequest request,
                         javax.servlet.http.HttpServletResponse response,
                         List<T> items,
                         Predicate<T> filter,
                         int sortColumnNumber,
                         boolean sortDescending,
                         String... additionalFormFields)
        Creates a unfiltered list pager.
        Parameters:
        request - the request object for the page in question
        response - the response object for the page in question
        items - the complete list of items to display on the page
        sortColumnNumber - the index of the column (0-based) of which values are to be ordered
        sortDescending - true if the column is to be in descending order, false for ascending order.
        filter - the filter to apply to the complete list
        additionalFormFields - 0 or more input field identifiers (NOT form field names) to include in requests for other pages
    • Method Detail

      • getTotalItemCount

        public int getTotalItemCount()
        Returns:
        the total number unfiltered items
      • getFilteredItemCount

        public int getFilteredItemCount()
        Returns:
        the number of items after the filter has been applied
      • getItemsOnCurrentPage

        public List<T> getItemsOnCurrentPage()
        Returns:
        the filtered items on the current page
      • getCurrentPageNumber

        public int getCurrentPageNumber()
        Returns:
        the current page
      • getPageSize

        public int getPageSize()
        Returns:
        the maximum number of items on the page
      • getFirstItemNumberOnPage

        public int getFirstItemNumberOnPage()
        Returns:
        the first item number on the page
      • getLastItemNumberOnPage

        public int getLastItemNumberOnPage()
        Returns:
        the last item number on the page
      • getTotalPages

        public int getTotalPages()
        Returns:
        the total number of pages
      • isFiltered

        public boolean isFiltered()
        Returns:
        true if the the supplied filter has restricted the number of items to display, otherwise false
      • getSortColumnNumber

        public int getSortColumnNumber()
      • isSortDescending

        public boolean isSortDescending()
      • getPageSizeSelection

        public String getPageSizeSelection()
        Returns:
        a string that contains HTML for selecting the page size
      • getPageLinks

        public String getPageLinks()
        Returns:
        a string that contains links to other pages in the list
      • getHiddenFields

        public String getHiddenFields()
        Returns:
        the list of hidden fields required to maintain the current list pager state
      • getQueryString

        public static String getQueryString​(javax.servlet.http.HttpServletRequest request,
                                            char prefix,
                                            String... additionalFormFields)
        Parameters:
        request - The request to retrieve the values from
        prefix - The first char of the query string - either `?` or `&`
        additionalFormFields - 0 or more form field names to include in requests for other pages
        Returns:
        a query string required to maintain the current list pager state
      • getJumpToPageForm

        public String getJumpToPageForm()
        Returns:
        a string with an HTML form containing hidden fields, used for navigating between pages
      • getPageFunctions

        public String getPageFunctions()
        Returns:
        a string containing JavaScript that helps with navigation between pages