Vue use-hook for renderless pagination logic
npm install vue-use-paginatorVue 3 composition API hook function to reactively paginate data and arrange paginator buttons. Completely renderless.
``shell`
$ npm install vue-use-paginator
`html`
Function usePaginator returns an object of type IPagination which contains the following reactive data plus some convenience functions. You can initialize page, pageSize, numItems and numButtons by passing an object of type IOptions. Otherwise, default initial values will be used.
| Name | Type | Description
| -------------- | ---------------------------------- | -----------
| page | Ref | Current active page (default initial value: 1)pageSize
| | Ref | Size of each page (default initial value: 5)numItems
| | Ref | Total number of items to paginate (default initial value: 0)numButtons
| | Ref | Number of paginator buttons to display (default initial value: 5)numPages
| | ComputedRef | Total number of pagesslice
| | ComputedRef<[number, number]> | Tuple containing start and end index delimiting the currently active page (0 based, end index is exclusive). For example, given you are on page 2, pageSize is 10 and numItems is greater than or equal to 20, slice will be [10, 20].buttons
| | ComputedRef | Array of objects usable for displaying paginator buttons (details below)hasPrev
| | ComputedRef | Whether the currently active page is the first pagehasNext
| | ComputedRef | Whether the currently active page is the last pagegoPrev
| | () => number | Go to next pagegoNext
| | () => number | Go to previous pagegoStart
| | () => 1 | Go to first pagegoEnd
| | () => number | Go to last page
* page, pageSize, numItems, as well as numButtons are writable refs that will cause the other reactive properties to adjust when changed. Once you know how many items to paginate, you need to set numItems at least.goPrev
* You can move to another page by either calling , goNext, goStart, goEnd, or by assigning to page directly. Note that assigning a value outside of 1 and numPages will be ignored.
When displaying paginator buttons, you often don't have enough slots for each page and therefore need to use a placeholder like '...'. Calculating the positions of these placeholders is not trivial. Fortunately, buttons is an array of IPaginatorButton where each item contains the following properties to help you out.
| Name | Type | Description
| ---------- | ---------- | -----------
| page | number | The page to navigate to when clicking this buttonactive
| | boolean | Whether this button shows the currently active pageellipsis
| | boolean | Whether this button is a placeholder for omitted pages
#### Template example
Here is a minimal example how you could write your paginator markup.
`vue`
{{ '<<' }}
{{ '<' }}
v-for="button in buttons"
:key="button.page"
:class="{ 'current-page': button.active }"
@click="page = button.page"
>
{{ button.ellipsis ? '...' : button.page }}
{{ '>' }}
{{ '>>' }}
Notice how certain classes and texts are set based on the values of hasPrev, hasNext, button.active and button.ellipsis. How you style these elements is completely up to you.
This library doesn't make any assumptions about whether you paginate in the frontend or call a paginated endpoint.
If you paginate in the frontend, just assign the length of your collection to numItems and use the values from slice to slice out the items to display for your current page.
If you call a paginated endpoint, it needs to tell you the total number of items in order to set numItems. You can then use slice or page/pageSize to fetch a page. You will normally want to set up a watcher in order to fetch a new page when page or pageSize changes.
`typescript`
watch([page, pageSize], ([newPage, newPageSize]) => {
fetchPage(newPage, newPageSize);
})
#### Definitions
`typescript
interface IOptions {
page?: number | Ref
pageSize?: number | Ref
numItems?: number | Ref
numButtons?: number | Ref
}
interface IPagination {
page: Ref
pageSize: Ref
numItems: Ref
numButtons: Ref
numPages: ComputedRef
slice: ComputedRef<[number, number]>
buttons: ComputedRef
hasPrev: ComputedRef
hasNext: ComputedRef
goPrev: () => number
goNext: () => number
goStart: () => 1
goEnd: () => number
}
interface IButton {
page: number
}
interface IPageButton extends IButton {
active: boolean
ellipsis: false
}
interface IEllipsisButton extends IButton {
active: false
ellipsis: true
}
type IPaginatorButton = IPageButton | IEllipsisButton
`
#### Importing Types
`typescript``
import type { IOptions, IPagination, IPaginatorButton } from 'vue-use-paginator'