Vue - 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 components:
<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
Configuring Apache Web Server on Ubuntu and Debian
Publish Date:2025/04/05 Views:176 Category:OPERATING SYSTEM
-
This article shows you how to install Apache web server on Ubuntu and Debian, set it up, and access the access logs. Apache Web Server in Ubuntu and Debian Apache HTTP Server is a free and open source web server that is very popular. More t
How to use Docker to image a Node.js web application
Publish Date:2025/03/26 Views:107 Category:Docker
-
Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at
My understanding of webservice is this
Publish Date:2025/03/18 Views:150 Category:NETWORK
-
Recently, I encountered such a project at work (temporarily named Project A). Project A itself was developed in PHP, but its data came from another project developed in Java (temporarily named Project B). Project A could not operate the dat
Which technology do you choose to implement the web chat room?
Publish Date:2025/03/18 Views:62 Category:NETWORK
-
With the rise of HTML5 Websockets, web chat applications are becoming more and more popular. Recently, I am working on a mobile web application, the core function of which is to implement web chat on the mobile phone. Of course, the functio
How to redirect a website from HTTP to HTTPS
Publish Date:2025/03/16 Views:117 Category:NETWORK
-
HTTPS is a protocol for secure communication over computer networks and is widely used on the Internet. More and more website owners are migrating from HTTP to HTTPS, mainly due to the following 5 reasons: Google announced that websites usi
How to avoid soft 404 in the website during SEO
Publish Date:2025/03/17 Views:136 Category:NETWORK
-
This article shares an SEO problem, soft 404. A status code we often see on websites is 404. Whether we develop a website or not, this is a problem we have to face. What is a soft 404? Before talking about soft 404, we must first understand
Performance differences between HTTP REST API and WebSocket REST API
Publish Date:2025/03/17 Views:161 Category:NETWORK
-
What is a REST API? REST or RESTful API design (Representational State Transfer) is an architectural style that uses existing protocols. There are six key constraints for REST API design:
Webpack packages ES6 and CommonJs mixed React
Publish Date:2025/03/02 Views:179 Category:React
-
This article mainly introduces how to use webpack to package and compile React mixed with ES6 and CommonJs. It is a process of upgrading the React environment.
Details that need to be paid attention to when compiling react with webpack
Publish Date:2025/03/02 Views:201 Category:React
-
It is very convenient to compile and package react using webpack. Both need to be installed using npm. However, if you do not pay attention to the installation location of webpack and react parser babel during use, problems will occur during