Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Creating Bootpag Like Pagination Component Using Vue.js

DZone's Guide to

Creating Bootpag Like Pagination Component Using Vue.js

In this article, we look at how you can create a component in your web application that allows users to go from one page to another.

· Web Dev Zone ·
Free Resource

Jumpstart your Angular applications with Indigo.Design, a unified platform for visual design, UX prototyping, code generation, and app development.

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

Component State

The component should track the following:

  • Current Page.
  • Callback to be executed on 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 the 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 Vue.js 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 a click event handler.
  • v-for - this is used for looping over a list of items or a range of values.

Component Definition Using Vue.js

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 Vue.js 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. These 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>

The above component renders as:

The HTML and the JavaScript code which defined the component can be found on GitHub here.

Take a look at the Indigo.Design sample applications to learn more about how apps are created with design to code software.

Topics:
vue.js ,bootstrap ,javascript ,web dev

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}