|
| 1 | +# Renderless Pagination |
| 2 | + |
| 3 | +All of the pre-built pagination components in this library use a `RenderlessPagination` component under the hood. If you want to build your own pagination component, you can use the `RenderlessPagination` component to handle all of the pagination logic while leaving the UI up to you. |
| 4 | + |
| 5 | +## Example |
| 6 | + |
| 7 | +Below is an example of how you might use the `RenderlessPagination` component to build a custom pagination component: |
| 8 | + |
| 9 | +```vue |
| 10 | +<template> |
| 11 | + <RenderlessPagination |
| 12 | + :data="data" |
| 13 | + :limit="limit" |
| 14 | + @pagination-change-page="onPaginationChangePage" |
| 15 | + v-slot="slotProps" |
| 16 | + > |
| 17 | + <nav |
| 18 | + v-bind="$attrs" |
| 19 | + aria-label="Pagination" |
| 20 | + v-if="slotProps.computed.total > slotProps.computed.perPage" |
| 21 | + > |
| 22 | + <button |
| 23 | + :disabled="!slotProps.computed.prevPageUrl" |
| 24 | + v-on="slotProps.prevButtonEvents" |
| 25 | + > |
| 26 | + <slot name="prev-nav"> |
| 27 | + Previous |
| 28 | + </slot> |
| 29 | + </button> |
| 30 | +
|
| 31 | + <button |
| 32 | + :aria-current="slotProps.computed.currentPage ? 'page' : null" |
| 33 | + v-for="(page, key) in slotProps.computed.pageRange" |
| 34 | + :key="key" |
| 35 | + v-on="slotProps.pageButtonEvents(page)" |
| 36 | + > |
| 37 | + {{ page }} |
| 38 | + </button> |
| 39 | +
|
| 40 | + <button |
| 41 | + :disabled="!slotProps.computed.nextPageUrl" |
| 42 | + v-on="slotProps.nextButtonEvents" |
| 43 | + > |
| 44 | + <slot name="next-nav"> |
| 45 | + Next |
| 46 | + </slot> |
| 47 | + </button> |
| 48 | + </nav> |
| 49 | + </RenderlessPagination> |
| 50 | +</template> |
| 51 | +
|
| 52 | +<script> |
| 53 | +import RenderlessPagination from 'laravel-vue-pagination/src/RenderlessPagination.vue'; |
| 54 | +
|
| 55 | +export default { |
| 56 | + inheritAttrs: false, |
| 57 | +
|
| 58 | + emits: ['pagination-change-page'], |
| 59 | +
|
| 60 | + components: { |
| 61 | + RenderlessPagination |
| 62 | + }, |
| 63 | +
|
| 64 | + props: { |
| 65 | + data: { |
| 66 | + type: Object, |
| 67 | + default: () => {} |
| 68 | + }, |
| 69 | + limit: { |
| 70 | + type: Number, |
| 71 | + default: 0 |
| 72 | + }, |
| 73 | + }, |
| 74 | +
|
| 75 | + methods: { |
| 76 | + onPaginationChangePage(page) { |
| 77 | + this.$emit('pagination-change-page', page); |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | +</script> |
| 82 | +``` |
| 83 | + |
| 84 | +## Scoped Slot Props |
| 85 | +The `RenderlessPagination` component has the same [pros](/guide/api/props.html) and [events](/guide/api/events.html) as the pre-built pagination components. However, it also exposes the following scoped slot props: |
| 86 | + |
| 87 | +### data |
| 88 | + |
| 89 | +The `data` prop that was passed to the `RenderlessPagination` component. |
| 90 | + |
| 91 | +### limit |
| 92 | + |
| 93 | +The `limit` prop that was passed to the `RenderlessPagination` component. |
| 94 | + |
| 95 | +### computed |
| 96 | + |
| 97 | +Some computed properties that are used by the `RenderlessPagination` component: |
| 98 | + |
| 99 | +* `isApiResource` - Boolean indicating whether the `data` prop is an API resource |
| 100 | +* `currentPage` - Integer indicating the current page |
| 101 | +* `firstPageUrl` - The URL for the first page |
| 102 | +* `from` - Integer indicating the first item on the current page |
| 103 | +* `lastPage` - Integer indicating the last page |
| 104 | +* `lastPageUrl` - The URL for the last page |
| 105 | +* `nextPageUrl` - The URL for the next page |
| 106 | +* `perPage` - Integer indicating the number of items per page |
| 107 | +* `prevPageUrl` - The URL for the previous page |
| 108 | +* `to` - Integer indicating the last item on the current page |
| 109 | +* `total` - Integer indicating the total number of items |
| 110 | +* `pageRange` - An array of integers indicating the page range |
| 111 | + |
| 112 | +### prevButtonEvents |
| 113 | + |
| 114 | +An object containing the event listeners for the previous button: |
| 115 | + |
| 116 | +* `click` - Event listener for the `click` event |
| 117 | + |
| 118 | +### nextButtonEvents |
| 119 | + |
| 120 | +An object containing the event listeners for the next button: |
| 121 | + |
| 122 | +* `click` - Event listener for the `click` event |
| 123 | + |
| 124 | +### pageButtonEvents |
| 125 | + |
| 126 | +An function containing the event listeners for a page button. Takes the `page` number as an argument: |
| 127 | + |
| 128 | +* `click` - Event listener for the `click` event |
0 commit comments