Dynamic Pagination With Vue.js

(source)
For this tutorial, I will be using Atom.
- Download the Atom code editor.
- Install the Vue-fmt plugin if you do not see code being highlighted.
Firstly, What Is Vue.js?
Vue.js is a single page application JavaScript framework. It has many similarities to Angular and React. It is a progressive web application framework that is useful for developing more interactive web apps. Along with web applications, Vue.js is widely used by many websites like GitLab, 9gag, Behance, and many others.
What Is Nuxt.js?
Nuxt.js is a framework for Vue.js. It is mainly used for server-side rendering of Vue.js apps. As this is a framework for Vue.js, there are many similarities between Nuxt.js and Vue.js. You do not need to make many changes in your app if you are migrating from Vue.js to Nuxt.js. This page is an example of server-side rendered app.
Getting Started
Now, we can start developing the project. Developing dynamic pagination requires you to have basic knowledge of Vue.js and Nuxt.js. You should know the installation process and basic HTML, CSS, and router-links. I have to tell you that Vue.js has one of the best tutorials, you can visit here to learn more.
In this article, we have used w3schools’s CSS material. Copy and paste the following code if you are developing an app with Nuxt.js.
And in case you are using Vue.js then update index.html.
Let’s dive into the code,
The template tag contains all the basic HTML about laptops, RAM, operating system, and price, there is nothing “Vueish” about it except v-for.
You can use the v-for directive to render a list of items based on an array. The v-for directive requires “item in items,” in this case, the first v-for directive is “pageNumber in totalPages” and another v-for directive is “article in paginate,” which will get all the information about a laptop. When currentPage is set to 1, dynamic pagination will start from the first page. As soon as a user clicks on another page, currentPage updates to the page number clicked by the user.
Template HTML:
Have a look at the following code, it is a data function with a return object which is used for two-way data binding. This object contains the articles array, which has all the information about a laptop's operating system, price, weight, size, storage, and RAM. You can update itemsPerPage to any number. If you want to show five items per page, then you can update that to 5.
Add the following code inside methods. The setPage function is called to update the current page.
Here is the computed property. totalPage is the logic for total dynamic pages. For example, if itemsPerPage is 3 and there are a total of 12 items in the articles array, then the totalPage element will be 4.
Add some styling to change the color of the current page.
And that’s a wrap! After implementing this code, if you have any doubts then do not hesitate to reach us by dropping a comment below.
Advertising by Adpathway




