DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Join us tomorrow at 1 PM EST: "3-Step Approach to Comprehensive Runtime Application Security"
Save your seat
  1. DZone
  2. Coding
  3. JavaScript
  4. Creating Bootpag Like Pagination Component Using Vue.js

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.

Mohamed Sanaulla user avatar by
Mohamed Sanaulla
CORE ·
Dec. 12, 17 · Tutorial
Like (3)
Save
Tweet
Share
8.19K Views

Join the DZone community and get the full member experience.

Join For Free

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:

  • Bootstrap 4. I am using the Bootswatch Celurean theme.
  • Lodash JavaScript library.

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.

Vue.js HTML Event

Published at DZone with permission of Mohamed Sanaulla, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Real-Time Stream Processing With Hazelcast and StreamNative
  • The Role of Data Governance in Data Strategy: Part II
  • Mr. Over, the Engineer [Comic]
  • Last Chance To Take the DZone 2023 DevOps Survey and Win $250! [Closes on 1/25 at 8 AM]

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: