Implementing paging in Vue
Pagination is one of the common features in modern web applications. It allows users to browse large amounts of data by dividing the data into multiple pages. In Vue, we can use some libraries and components to implement pagination. In this article, we will take a deep dive into how to implement pagination in Vue.
Install and use the Vue pagination component
The Vue pagination component is an easy-to-use plugin that provides functions such as paginator, pagination navigation bar and pagination table. We can install it through the npm package manager:
$ npm install vue-pagination-component --save
Once installed, we can use it in our Vue component:
<template>
<div>
<pagination
:currentPage="currentPage"
:perPage="perPage"
:total="total"
@page-changed="onPageChanged"
></pagination>
</div>
</template>
<script>
import Pagination from 'vue-pagination-component';
export default {
components: {
Pagination,
},
data() {
return {
currentPage: 1,
perPage: 10,
total: 100,
};
},
methods: {
onPageChanged(page) {
this.currentPage = page;
},
},
};
</script>
In the code above, we use a component called pagination. It needs to be passed three properties: currentPage, perPage, and total. It also requires an event handler @page-changed
to update the current page number when the paginator's page number changes.
Implementing a custom paginator
If we need more customized functions, we can use a custom paginator to implement the paging function. In Vue, we can use calculated properties and methods to implement a custom paginator.
<template>
<div>
<ul>
<li v-for="page in pages" :key="page">
<a href="#" @click.prevent="onPageClick(page)">{{ page }}</a>
</li>
</ul>
</div>
</template>
<script>
export default {
data() {
return {
currentPage: 1,
perPage: 10,
total: 100,
};
},
computed: {
pages() {
let pages = [];
let from = Math.max(1, this.currentPage - 2);
let to = Math.min(this.currentPage + 2, this.totalPages);
if (from > 1) {
pages.push(1);
if (from > 2) {
pages.push('...');
}
}
for (let i = from; i <= to; i++) {
pages.push(i);
}
if (to < this.totalPages) {
if (to < this.totalPages - 1) {
pages.push('...');
}
pages.push(this.totalPages);
}
return pages;
},
totalPages() {
return Math.ceil(this.total / this.perPage);
},
},
methods: {
onPageClick(page) {
this.currentPage = page;
},
},
};
</script>
In the code above, we use a computed property called pages to calculate the page numbers in the paginator. We also use a computed property called totalPages to calculate the total number of pages. Finally, we use a onPageClick
method called to handle the page number event in the paginator.
Implementing server-side paging
If your data is obtained from the server, you can use server-side paging to implement the paging function. In Vue, we can use the Axios library to obtain server data and use calculated properties and methods to implement server-side paging.
<template>
<div>
<ul>
<li v-for="page in pages" :key="page">
<a href="#" @click.prevent="onPageClick(page)">{{ page }}</a>
</li>
</ul>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr v-for="user in users" :key="user.id">
<td>{{ user.id }}</td>
<td>{{ user.name }}</td>
<td>{{ user.email }}</td>
</tr>
</tbody>
</table>
</div>
</template>
<script>
import axios from 'axios';
export default {
data() {
return {
currentPage: 1,
perPage: 10,
total: 0,
users: [],
};
},
computed: {
pages() {
let pages = [];
let from = Math.max(1, this.currentPage - 2);
let to = Math.min(this.currentPage + 2, this.totalPages);
if (from > 1) {
pages.push(1);
if (from > 2) {
pages.push('...');
}
}
for (let i = from; i <= to; i++) {
pages.push(i);
}
if (to < this.totalPages) {
if (to < this.totalPages - 1) {
pages.push('...');
}
pages.push(this.totalPages);
}
return pages;
},
totalPages() {
return Math.ceil(this.total / this.perPage);
},
},
methods: {
onPageClick(page) {
this.currentPage = page;
this.getUsers();
},
getUsers() {
axios
.get(`/api/users?page=${this.currentPage}&per_page=${this.perPage}`)
.then((response) => {
this.total = response.data.total;
this.users = response.data.users;
})
.catch((error) => {
console.log(error);
});
},
},
mounted() {
this.getUsers();
},
};
</script>
In the code above, we use a getUsers
method called to fetch server data. We also use a lifecycle hook called mounted to fetch server data when the component is mounted. Finally, we use a onPageClick
method called to handle the page number event in the paginator and re-fetch the server data.
Summarize
In this article, we took a deep dive into how to implement paging in Vue. We introduced methods to implement paging using Vue paging components, custom paginators, and server-side paging. I hope this article can help you better understand the implementation of paging.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Detailed explanation of Vue life cycle hooks
Publish Date:2025/04/20 Views:60 Category:Vue
-
Like other frameworks, Vue has a number of lifecycle hooks that allow us to attach code to specific events that occur when creating or using a Vue application — for example, when a component loads, when a component is added to the DOM, or
How to use MUI methods in Vue
Publish Date:2025/04/20 Views:141 Category:Vue
-
MUI is a front-end framework based on HTML5 and CSS3. It provides a series of UI components and JS plug-ins to help developers quickly build beautiful and smooth mobile web applications. Using MUI in Vue can further improve development effi
Dynamically add properties and values to objects in Vue
Publish Date:2025/04/20 Views:199 Category:Vue
-
In Vue, we often need to manipulate the properties and values of objects. Sometimes we need to dynamically add properties and values to meet business needs. This article will introduce in detail how to dynamically add properties
How to scroll to the top or bottom of the page in Vue.js
Publish Date:2025/03/01 Views:96 Category:Vue
-
Vue.js is a popular front-end framework that helps developers build efficient and maintainable applications. In Vue.js, scrolling the page to the top or bottom is a common requirement. In this article, we will introduce how to implement this
Display element or text on mouse hover in vue
Publish Date:2025/03/01 Views:182 Category:Vue
-
Vue.js is a popular JavaScript framework that makes web application development easier and more efficient. In this tutorial, we will learn how to use Vue.js to display an element or text on mouse hover. This tutorial will cover the following
When watching an object in Vue, how to exclude certain properties from being watc
Publish Date:2025/03/01 Views:125 Category:Vue
-
When using watch in Vue, you may need to monitor an object, but only care about certain properties in the object, not all properties of the object. In this case, you can use deep monitoring and calculated properties, or add some options to w
What is the use of the immediate property of watch in Vue?
Publish Date:2025/03/01 Views:67 Category:Vue
-
In Vue, watch is a way to perform asynchronous tasks or trigger responsive dependencies when data changes. In most cases, watch will be delayed by default. This means that the watch will only be triggered when the value being monitored chang
Setting up checkbox functionality in Vue
Publish Date:2025/03/01 Views:189 Category:Vue
-
In Vue, checkboxes are a very common interactive component that allows users to select multiple options. This article will introduce how to set up checkbox functionality in Vue and provide some practical examples.
What happens if a child component changes the data in props in Vue?
Publish Date:2025/03/01 Views:141 Category:Vue
-
In Vue, when a child component changes the data in props, it will cause the responsiveness of the parent component and other child components to change. First, you need to understand that props is a way to pass data from a parent component t