DEV Community

Pulkit Singh
Pulkit Singh

Posted on • Edited on

Easy and Simple Pagination in Vue

How to paginate simply by using vue js?

In this tutorial I will show you how to Add custom Pagination in a vue component.

<template>
  <div id="app">
    <table>
      <thead>
        <th>#</th>
        <th>Name</th>
        <th>Age</th>
      </thead>
      <tbody>
        <tr v-for="(item, index) in currentList" :key="index">
          <td>{{ item.index }}</td>
          <td>{{ item.name }}</td>
          <td>{{ item.age }}</td>
        </tr>
      </tbody>
    </table>
    <button v-on:click="previousPage()" v-if="currentPage > 1">previous</button>
    <button v-on:click="nextPage()">Next</button>
  </div>
</template>

<script>
export default {
  created() {
    this.persons.map((n) => {
      this.num++;
      n.index = this.num;
      return n;
    });
    this.makeCurrentList();
  },
  data() {
    return {
      persons: [
        { name: "kapil", age: 22 },
        { name: "pulkit", age: 23 },
        { name: "rajat", age: 24 },
        { name: "raj", age: 25 },
        { name: "ankit", age: 26 },
        { name: "abhay", age: 27 },
        { name: "anmol", age: 28 },
        { name: "ishaan", age: 29 },
        { name: "ayush", age: 30 },
        { name: "rajat", age: 24 },
        { name: "raj", age: 25 },
        { name: "ankit", age: 26 },
        { name: "abhay", age: 27 },
        { name: "anmol", age: 28 },
        { name: "kapil", age: 22 },
        { name: "pulkit", age: 23 },
        { name: "rajat", age: 24 },
        { name: "raj", age: 25 },
        { name: "ankit", age: 26 },
        { name: "abhay", age: 69 },
      ],
      itemPerPage: 5,
      currentPage: 1,
      currentList: [],
      startIndex: null,
      num: 0,
    };
  },
  methods: {
    previousPage() {
      this.currentPage--;
      this.makeCurrentList(this.currentPage);
    },
    nextPage() {
      this.currentPage++;
      this.makeCurrentList(this.currentPage);
    },
    makeCurrentList() {
      this.currentList = [];
      console.log(this.currentPage);
      this.startIndex = this.currentPage * this.itemPerPage - this.itemPerPage;
      this.currentList = this.persons.slice(
        this.startIndex,
        this.currentPage * this.itemPerPage
      );
      console.log("start Index => ", this.startIndex);
      console.log("current Page => ", this.currentPage);
      console.log("Current List => ", this.currentList);
    },
  },
  name: "App",
  components: {},
};
</script>
Enter fullscreen mode Exit fullscreen mode

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️