Vue Ads Pagination Component
vue-ads-pagination is a Vue.js component designed to provide comprehensive pagination functionality for displaying large lists of items efficiently. It allows users to navigate through pages, showing information about the current item range (e.g., 'Items 1 to 10 of 100') and offering buttons for first, last, next, previous, and specific page selection. The current stable version, 2.1.7, is focused on Vue 2 compatibility. This package distinguishes itself by offering extensive customizability through slots, allowing developers to completely override default components (like the page buttons or item range display) with their own Vue components or apply custom styles to the default elements, making it highly adaptable to various UI designs. It provides distinct events for `page-change` and `range-change` to manage state updates effectively in the parent component. There's no explicit release cadence mentioned, but it acts as a mature, single-purpose component.
Common errors
-
[Vue warn]: Unknown custom element: <vue-ads-pagination> - did you register the component correctly?
cause The `VueAdsPagination` component was not imported and/or registered in the `components` option of the Vue instance or parent component where it is being used.fixEnsure `import VueAdsPagination from 'vue-ads-pagination';` is present in your script and `VueAdsPagination` is listed in the `components: {}` object of your Vue component. -
Property 'total-items' is missing in type 'VueAdsPaginationProps'
cause The `total-items` prop is marked as required by the component but was not provided, or was provided with an incorrect name (e.g., `totalItems` instead of `total-items`).fixAdd the `:total-items="yourTotalCount"` prop to the `<vue-ads-pagination>` component, ensuring its value is a number representing the total count of items. -
TypeError: Cannot read properties of undefined (reading 'start') or TypeError: Cannot read property 'start' of undefined
cause This error typically occurs when attempting to access properties of the scoped slot (like `props.start`, `props.end`, `props.total`) without correctly defining the `slot-scope` attribute on the `<template>` tag.fixEnsure your custom slot template uses `slot-scope="props"` (Vue 2) or `#default="props"` (Vue 2.6+ / Vue 3) to correctly destructure the scoped slot properties.
Warnings
- gotcha The `page` property is zero-based, meaning the first page is 0, the second is 1, and so on. Ensure your application logic and any user-facing page numbers account for this indexing difference.
- gotcha The `page` prop is not automatically updated by the component. It functions as a one-way prop. You must listen to the `page-change` event and explicitly update the `page` data property in your parent component to reflect the new active page.
- gotcha The default styles for `vue-ads-pagination` are not automatically included. You must manually import `vue-ads-pagination/dist/vue-ads-pagination.css` in your project's main entry file or a root component.
- gotcha For the default button rendering to display icons (e.g., for 'first', 'last', 'next', 'previous' page buttons), the component's internal templates assume the availability of Font Awesome CSS. If Font Awesome is not included in your project, these icons will not render.
Install
-
npm install vue-ads-pagination -
yarn add vue-ads-pagination -
pnpm add vue-ads-pagination
Imports
- VueAdsPagination
const VueAdsPagination = require('vue-ads-pagination');import VueAdsPagination from 'vue-ads-pagination';
- VueAdsPageButton
import VueAdsPageButton from 'vue-ads-pagination/VueAdsPageButton';
import { VueAdsPageButton } from 'vue-ads-pagination'; - CSS
import 'vue-ads-pagination/src/vue-ads-pagination.css';
import 'vue-ads-pagination/dist/vue-ads-pagination.css';
Quickstart
<template>
<div id="app">
<vue-ads-pagination
:total-items="200"
:max-visible-pages="5"
:page="currentPage"
:loading="loading"
@page-change="pageChange"
@range-change="rangeChange"
>
<template slot-scope="props">
<div class="vue-ads-pr-2 vue-ads-leading-loose">
Items {{ props.start }} to {{ props.end }} of {{ props.total }}
</div>
</template>
<template
slot="buttons"
slot-scope="props"
>
<vue-ads-page-button
v-for="(button, key) in props.buttons"
:key="key"
v-bind="button"
@page-change="currentPage = button.page"
/>
</template>
</vue-ads-pagination>
<p>Current Page: {{ currentPage + 1 }}</p>
</div>
</template>
<script lang="ts">
import Vue from 'vue';
import '../node_modules/@fortawesome/fontawesome-free/css/all.css'; // For default button icons
import 'vue-ads-pagination/dist/vue-ads-pagination.css'; // For default styling
import VueAdsPagination, { VueAdsPageButton } from 'vue-ads-pagination';
export default Vue.extend({
name: 'App',
components: {
VueAdsPagination,
VueAdsPageButton,
},
data () {
return {
loading: false,
currentPage: 4, // 0-based, so 5th page
};
},
methods: {
pageChange (page: number) {
this.currentPage = page; // Update the bound prop
console.log('Page changed to:', page + 1);
},
rangeChange (start: number, end: number) {
console.log('Displaying items from', start + 1, 'to', end + 1);
},
},
});
</script>