Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Pagination

Pagination represents information relevant to pagination of data items.

When data needs to be rendered in multiple pages, Pagination can be used to represent information such as total item count, [[pageSize|page size]], [[page|current page]], etc. These information can be passed to pagers to render pagination buttons or links.

The following example shows how to create a pagination object and feed it to a pager.

Controller action:

async sampleRouteHandler(request, response, next)
{
let query = { where: { status: 1 } };
let pages = new Pagination(['totalCount' => Posts.count(query)]);
let models = Posts.findAll({ ...query, offset: pages.offset, limit: pages.limit });
let pager = new LinkPager({
pagination: pages,
})
return response.render('/index.ejs', { models, pages, pager });
}

View:

for(let model of models) {
// display model here
}

// display pagination
<%- pager.render(); %>

For more details and usage information on Pagination, see the pagination section in this guide.

property-read

int limit The limit of the data. This may be used to set the LIMIT value for a SQL statement for fetching the current page of data. Note that if the page size is infinite, a value -1 will be returned. This property is read-only.

property-read

array links The links for navigational purpose. The array keys specify the purpose of the links (e.g. [[LINK_FIRST]]), and the array values are the corresponding URLs. This property is read-only.

property-read

int offset The offset of the data. This may be used to set the OFFSET value for a SQL statement for fetching the current page of data. This property is read-only.

property

int $page The zero-based current page number.

property-read

int pageCount Number of pages. This property is read-only.

property

int pageSize The number of items per page. If it is less than 1, it means the page size is infinite, and thus a single page contains all items.

author

Mahesh S Warrier https://github.com/codespede

Hierarchy

Index

Constructors

Properties

_page: any
_pageSize: any

Number of items on each page. If it is less than 1, it means the page size is infinite, and thus a single page contains all items.

defaultPageSize: number = 20

The default page size. This property will be returned by [[pageSize]] when page size cannot be determined by pageSizeParam from params.

forcePageParam: boolean = true

Whether to always have the page parameter in the URL created by createUrl. If false and [[page]] is 0, the page parameter will not be put in the URL.

initialization: Promise<void>

A Promise which resolves when this Component has been successfully initialized.

pageParam: string = 'page'

Name of the parameter storing the current page index.

see

params

pageSizeLimit: number[] = ...

The page size limits. The first array element stands for the minimal page size, and the second the maximal page size. If this is false, it means [[pageSize]] should always return the value of defaultPageSize.

pageSizeParam: string = 'per-page'

Name of the parameter storing the page size.

see

params

params: {}

Parameters (name => value) that should be used to obtain the current page number and to create new pagination URLs. If not set, all parameters from request will be used instead.

The object element indexed by pageParam is considered to be the current page number (defaults to 0); while the element indexed by pageSizeParam is treated as the page size (defaults to defaultPageSize).

Type declaration

  • [key: string]: any
totalCount: number = 0

Total number of items.

validatePage: boolean = true

Whether to check if [[page]] is within valid range. When this property is true, the value of [[page]] will always be between 0 and ([[pageCount]]-1). Because [[pageCount]] relies on the correct value of totalCount which may not be available in some cases (e.g. MongoDB), you may want to set this property to be false to disable the page number validation. By doing so, [[page]] will return the value indexed by pageParam in params.

Methods

  • createUrl(page: string | number, pageSize?: number, absolute?: boolean): URL
  • Creates the URL suitable for pagination with the specified page number. This method is mainly called by pagers when creating URLs used to perform pagination.

    see

    params

    see

    forcePageParam

    Parameters

    • page: string | number

      the zero-based page number that the URL should point to.

    • pageSize: number = undefined

      the number of items on each page. If not set, the value of [[pageSize]] will be used.

    • absolute: boolean = false

      whether to create an absolute URL. Defaults to false.

    Returns URL

    the created URL

  • getLimit(): number
  • Returns number

    the limit of the data. This may be used to set the LIMIT value for a SQL statement for fetching the current page of data. Note that if the page size is infinite, a value -1 will be returned.

  • getLinks(absolute?: boolean): {}
  • Returns a whole set of links for navigating to the first, last, next and previous pages.

    Parameters

    • absolute: boolean = false

      whether the generated URLs should be absolute.

    Returns {}

    the links for navigational purpose. The array keys specify the purpose of the links (e.g. [[LINK_FIRST]]), and the array values are the corresponding URLs.

    • [key: string]: any
  • getOffset(): number
  • Returns number

    the offset of the data. This may be used to set the OFFSET value for a SQL statement for fetching the current page of data.

  • getPage(recalculate?: boolean): number
  • Returns the zero-based current page number.

    Parameters

    • recalculate: boolean = false

      whether to recalculate the current page based on the page size and item count.

    Returns number

    the zero-based current page number.

  • getPageCount(): number
  • getPageSize(): number
  • Returns the number of items per page. By default, this method will try to determine the page size by pageSizeParam in params. If the page size cannot be determined this way, defaultPageSize will be returned.

    see

    pageSizeLimit

    Returns number

    the number of items per page. If it is less than 1, it means the page size is infinite, and thus a single page contains all items.

  • getQueryParam(name: string, defaultValue: string | number): string
  • Returns the value of the specified query parameter. This method returns the named parameter value from params. Null is returned if the value does not exist.

    Parameters

    • name: string

      the parameter name

    • defaultValue: string | number

      the value to be returned when the specified parameter does not exist in params.

    Returns string

    the parameter value

  • init(): Promise<void>
  • Initializes the object. This method is invoked at the end of the constructor after the object is initialized with the given configuration.

    Returns Promise<void>

  • setPage(value: string | number, validatePage?: boolean): void
  • Sets the current page number.

    Parameters

    • value: string | number

      the zero-based index of the current page.

    • validatePage: boolean = false

      whether to validate the page number. Note that in order to validate the page number, both validatePage and this parameter must be true.

    Returns void

  • setPageSize(value: string | number, validatePageSize?: boolean): void
  • Parameters

    • value: string | number

      the number of items per page.

    • validatePageSize: boolean = false

      whether to validate page size.

    Returns void

Generated using TypeDoc