Dynamic Pagination With Vue.js
Learn how to set up dynamic pagination using Vue.js and Nuxt.js. In this tutorial, we'll go over the code to set up a web page, then add dynamic pagination.
Join the DZone community and get the full member experience.Join For Free
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?
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.
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
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.
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
Add the following code inside
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
3 and there are a total of 12 items in the
articles array, then the
totalPage element will be
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.
Opinions expressed by DZone contributors are their own.