Vuetable-2: Vue 2.x Datatable Component
Vuetable-2 is a flexible datatable component designed for Vue 2.x applications, providing robust capabilities for displaying and interacting with tabular data. The package is currently at its stable version 1.7.5, with an active development branch (`v2.0.0-beta`) introducing features like SSR support and a more modular field component system. While a strict release cadence is not followed, updates are delivered to enhance features and resolve issues. Its key differentiators include comprehensive API-mode handling, customizable field definitions, integrated pagination (including zero-based options in v2), a rich event system for interaction, and support for fixed table headers, making it adaptable for complex data presentation scenarios. It simplifies data management tasks in Vue 2.x SPAs.
Common errors
-
[Vue warn]: Unknown custom element: <vuetable>
cause The Vuetable component has not been correctly registered with your Vue application.fixEnsure `Vuetable` is imported and added to the `components` option in your Vue instance or component definition: `components: { Vuetable }`. -
Cannot read property 'data' of undefined (when fetching API data)
cause The API response structure does not match `vuetable-2`'s expected format, particularly the `data` and `pagination` keys, or the `pagination-path` is incorrect.fixConfigure the `data-path` and `pagination-path` props to match your API response structure, or transform your API response to fit the default `data` and `pagination` top-level keys. -
Sorting does not work when clicking table headers
cause Sorting is either not enabled for the field, or the `sort-order` prop is not correctly configured for client-side sorting, or the API is not handling the sort parameters.fixEnsure `sortable: true` is set in the `fields` definition. If using API mode, verify the `api-url` correctly receives and processes `sort` parameters (e.g., `sort=id|desc`). For client-side sorting (`api-mode='false'`), ensure `sort-order` is properly defined.
Warnings
- breaking Major internal refactor for `v2.0.0` converts 'special fields' into customizable 'Field components'. While backward compatibility is attempted, custom field implementations or advanced usage of internal field mechanisms might require migration.
- gotcha The `query-params` prop, which was typically an object, can also be a `Function` since `v1.7.2`. This function receives `sortOrder`, `currentPage`, and `perPage` as parameters and should return the query parameters object.
- gotcha In `v2.0.0-beta.2` and later, the internal `__sequence` field, used for row numbering, now starts from `1` instead of `0` when pagination is active. This can affect custom calculations or displays relying on zero-based indexing for row sequence.
- gotcha The `data-manager` prop in `v2.0.0-beta.2` onwards now supports Promises. While this is an enhancement, it might introduce subtle timing issues or require adjustments if your `data-manager` implementation was strictly synchronous or had different asynchronous handling expectations.
Install
-
npm install vuetable-2 -
yarn add vuetable-2 -
pnpm add vuetable-2
Imports
- Vuetable
const Vuetable = require('vuetable-2');import Vuetable from 'vuetable-2';
- VuetablePagination
import { VuetablePagination } from 'vuetable-2';import VuetablePagination from 'vuetable-2/src/components/VuetablePagination';
- VuetablePaginationMixin
import { VuetablePaginationMixin } from 'vuetable-2';import VuetablePaginationMixin from 'vuetable-2/src/components/VuetablePaginationMixin';
Quickstart
import Vue from 'vue';
import Vuetable from 'vuetable-2';
import VuetablePagination from 'vuetable-2/src/components/VuetablePagination';
new Vue({
el: '#app',
components: { Vuetable, VuetablePagination },
data: {
fields: [
{ name: 'id', title: 'ID' },
{ name: 'name', title: 'Name' },
{ name: 'email', title: 'Email' },
],
apiUrl: 'https://example.com/api/users',
css: {
table: 'table table-striped table-bordered',
// ... other css classes as per documentation
},
},
template: `
<div>
<vuetable ref="vuetable"
:api-url="apiUrl"
:fields="fields"
pagination-path="pagination"
@vuetable:pagination-data="onPaginationData"
></vuetable>
<vuetable-pagination ref="pagination"
@vuetable:change-page="onChangePage"
></vuetable-pagination>
</div>
`,
methods: {
onPaginationData(paginationData) {
this.$refs.pagination.setPaginationData(paginationData);
},
onChangePage(page) {
this.$refs.vuetable.changePage(page);
},
},
});