JavaScript

Creating Bootpag Like Pagination Component Using Vuejs

Introduction

In this article, I will try to create a pagination Vuejs component similar to the one supported by jQuery Bootpag plugin.

Component State

The component should track the following:

  • Current Page
  • Callback to be executed page click
  • Max visible page numbers
  • Start and end of the range of page numbers visible

Component Event Handlers

The component handles 3 events:

  • Two events related to navigating Next and Previous set of pages
  • One event to change the page – the event handler is provided by the parent.

Component Dependencies

The component requires the following dependencies:

Component HTML Template

We will be using Bootstrap for designing the component, so the HTML template for the component is:

var paginationTemplate = '\
    <nav aria-label="Page navigation example">\
        <ul class="pagination">\
        <li class="page-item" :class="{disabled: disablePrev}">\
            <a class="page-link" href="javascript:void(0);"\
                v-on:click="previous">Previous</a>\
        </li>\
        <li class="page-item" :class="{ active: (p == current)}" v-for="p in range">\
            <a class="page-link" href="javascript:void(0);" \
                v-on:click="callback(p)">{{ p }}</a>\
        </li>\
        <li class="page-item" :class="{disabled: disableNext}">\
            <a class="page-link" v-on:click="next" href="javascript:void(0)">Next</a>\
        </li>\
        </ul>\
    </nav>\
';

We have assigned the HTML template to a javascript variable. Let me explain the Vuejs Directives used above:

  • :class – this is used to assign class names dynamically to the HTML element. The class names identified by this directive get merged with the class names defined statically using the class HTML attribute. I have provided a JSON object where key is the class name and the value is a boolean indicating whether the class should be present or not.
  • v-on:click – this directive is used to assign click event handler
  • v-for – this is used for looping over list of items or a range of values

Component Definition using Vuejs

In the previous sections, we saw what the component stores, what events it handles and the HTML for the component, let’s go ahead and define the Vuejs component:

Vue.component('pagination', {
    template: paginationTemplate,
    props: ["total", "current", "callback", "maxVisible"],
    data: function(){
        return {
            start: 1,
            end: (this.maxVisible >= this.total? this.total : this.maxVisible)
        }
    },
    watch:{
        total: function(){
            this.end = (this.maxVisible >= this.total? this.total : this.maxVisible);
        }
    },
    computed: {
        range: function(){
            return _.range(this.start, this.end + 1, 1);
        },
        disablePrev: function(){
            return this.start <= 1;
        },
        disableNext: function(){
            return this.end >= this.total;
        }
    },
    methods:{
        next: function(){
            var newEnd = this.end + this.maxVisible;
            if ( newEnd <= this.total){
                this.start = this.start + this.maxVisible;
                this.end = newEnd;  
            }
            
        },
        
        previous: function(){
            var newStart = this.start - this.maxVisible;
            if ( newStart >= 1){
                this.start = newStart;
                this.end = this.end - this.maxVisible;  
            }
        }
        
    }
    
});

Let me elaborate on the properties of the JSON object defined above:

  • template – HTML code for the component
  • props – properties accepted by the component
  • data – the internal state maintained by the component
  • watch – the handlers which are executed when the watched property value changes
  • computed – the computed properties of the component. This are recomputed whenever their dependent state changes
  • methods – the methods which are bound as event handlers

Component in Action

Let us use the above component in an HTML page:

<div class="row">
    <div class="col-md-offset-3 col-md-6">
        <pagination :total="totalPages" :current="currentPage" 
            :max-visible="maxVisible" :callback="paginationCallback">
        </pagination>
    </div>
</div>
Published on Web Code Geeks with permission by Mohamed Sanaulla, partner at our WCG program. See the original article here: Creating Bootpag Like Pagination Component Using Vuejs

Opinions expressed by Web Code Geeks contributors are their own.

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button