Skip to content

usePagination ​

Manages infinite-scroll / load-more pagination state — page tracking, element accumulation, search, and URL construction.

File: vue/src/js/hooks/utils/usePagination.js


Props Factory ​

js
import { makePaginationProps } from '@/hooks/utils/usePagination'
PropTypeDefaultDescription
endpointString—Base URL for the paginated data source
pageNumber1Initial page number
lastPageNumber-1Last known page number (-1 = unknown)
itemsPerPageNumber20Items per page
sourceLoadingBooleanfalseExternal loading flag
withArray[]Relationships to eager-load
scopesArray[]Query scopes to apply
ordersArray[]Sort order definitions
appendsArray[]Appended attributes
columnArray[]Columns to select
searchKeysArray['name']Fields to search within
paginationPageKeyString'page'Query parameter name for the page number

Usage ​

js
import { usePagination, makePaginationProps } from '@/hooks/utils/usePagination'

const props = defineProps(makePaginationProps())

const {
  elements,
  activePage,
  nextPage,
  activeLastPage,
  itemsLoading,
  fullUrl,
  searchModel,
  appendElements,
  setActivePage,
  setActiveLastPage,
  setItemsLoading,
} = usePagination(props, context)

Returns ​

State ​

NameTypeDescription
elementsRef<Array>Accumulated list of loaded items
activePageRef<Number>Current page number
nextPageRef<Number>Page to load next
activeLastPageRef<Number>Last known page number
itemsLoadingRef<Boolean>true while a fetch is in progress
searchModelRef<String>Search input value
searchRef<String>Committed search value

Computed ​

NameTypeDescription
fullUrlComputedRef<String>Complete URL with all query parameters
queryParametersComputedRef<String>URLSearchParams string (page, itemsPerPage, search, with, …)
searchFilterObjectComputedRef<Object>{ search: value } when search is non-empty
searchFieldsFilterComputedRef<Object>Multi-key search filter object
defaultQueryParametersRef<Object>Parameters parsed from the original endpoint URL

Methods ​

NameSignatureDescription
setActivePage(page) => voidUpdate activePage and advance nextPage
setActiveLastPage(page) => voidUpdate activeLastPage
setItemsLoading(value) => voidSet the loading flag
setElements(items) => voidReplace the elements list
appendElements(items) => voidAppend items to the end of elements
prependElements(items) => voidPrepend items to the beginning of elements
setSearchValue(value?) => voidCommit a search value

Notes ​

  • For infinite-scroll UIs, call appendElements on each successful page load and setActivePage to advance.
  • For standard paginated tables, use setElements to replace the list on each load.
  • The main data-table uses useTable directly. usePagination is intended for simpler paginated list components.

See Also ​

  • useTable — full-featured table composable