JIYIK CN >

Current Location:Home > Learning > WEB FRONT-END > Vue >

Vue - Implementing paging in Vue

Author:JIYIK Last Updated:2025/02/25 Views:

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-changedto 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 onPageClickmethod 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 getUsersmethod 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 onPageClickmethod 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.

Article URL:

Related Articles

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.

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:179 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

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:186 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:140 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

How to refresh the page in Vue

Publish Date:2025/03/01 Views:91 Category:Vue

Vue is a popular JavaScript framework that provides many convenient tools and methods for building web applications. In Vue, page updates are usually achieved through data binding and responsive systems. But sometimes you need to refresh the

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial