Site icon Experiences Unlimited

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:

Component Event Handlers

The component handles 3 events:

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:

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:

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>

The above component renders as:

The HTML and the Javascript code which defined the component can be found on Github here

Exit mobile version