π Pagination Component β Usage Guide β
A Reusable Pagination component that works for both:
- API-based Pagination β when data comes from a backend (e.g., Laravelβs
->paginate(20)->withPath('');). - Static Pagination β when data is already available locally.
βοΈ Component Props β
| Prop | Type | Default | Description |
|---|---|---|---|
meta | Object | null | Pagination metadata (from API) (optional for static). |
apiUrl | String | '' | Base API URL for dynamic pagination (optional for static). |
perPage | Number/String | 10 | Default per-page count. (null for API-base) |
perPageOptions | Array | [5,10,15,25,50,100,'All'] | Dropdown values for "Per Page" selector. |
autoScrollTop | Boolean | true | Scrolls to top when page changes. |
allData | Array | [] | All Data for Static Pagination |
paginatedData | Array | [] | Return Paginated Data |
π€ Events Emitted β
| Event | Description | Payload |
|---|---|---|
page-change | Triggered when a pagination button or page link is clicked. | Full or relative URL (e.g., ?page=2&per_page=10). |
π 1. Using with API-Based Pagination β
π§ When to Use β
Laravel returns paginated data with a structure like:
json
{
"data": [...],
"meta": {
"current_page": 2,
"last_page": 10,
"per_page": 25,
"total": 240,
"links": [...]
}
}vue
<template>
<div>
<Pagination
:meta="pagination.meta"
:api-url="pagination.apiUrl"
:per-page="pagination.perPage"
@page-change="getData"
/>
</div>
</template>
<script>
export default {
data() {
return {
users: [],
pagination: {
apiUrl: '/api/users',
meta: {}
perPage: null,
},
}
},
mounted() {
this.getData()
},
methods: {
async getData(url = null) {
const api = url ? url : this.pagination.apiUrl
await axios.post(api)
.then((response) => {
this.users = response.data.data;
this.pagination.meta = response.data.meta
})
},
},
}
</script>π§Ύ Description β
- The component emits page-change with the next/previous page URL.
- The parent method (getData) re-fetches the data from the server.
- Works with any backend that returns a standard pagination object.
π 2. Using with Static Data Pagination β
π§ When to Use β
Use Static Mode when all your data already exists in the frontend (for example, in a local array).
vue
<template>
<div>
<table class="table table-bordered">
<tr v-for="user in visibleUsers" :key="user.id">
<td>{{ user.name }}</td>
</tr>
</table>
<Pagination
:all-data="allUsers"
:paginated-data.sync="visibleUsers"
:per-page="10"
/>
</div>
</template>
<script>
export default {
data() {
return {
allUsers: [],
visibleUsers: [],
}
},
}
</script>π§Ύ Description β
- No API calls are made.
- The dataset is sliced manually based on the selected page and per-page count.
Campus On Click