Install vue3-easy-data-table via npm or yarn
mainTo use vue3-easy-data-table in your Vue 3 project, install it using your preferred package manager.
npm install vue3-easy-data-table
# or
yarn add vue3-easy-data-tablerepository·main·Indexed 20 days ago
https://github.com/hc200ok/vue3-easy-data-tableA customizable and easy-to-use data table component for Vue.js 3.x applications. It supports features such as server-side pagination and sorting via ServerOptions, advanced column filtering with FilterOption, and dynamic CSS class application for headers, rows, and cells. The component is available via npm, yarn, or CDN.
To use vue3-easy-data-table in your Vue 3 project, install it using your preferred package manager.
npm install vue3-easy-data-table
# or
yarn add vue3-easy-data-tableAfter installation, import the component and its required CSS file, then register it globally in your Vue application instance.
import Vue3EasyDataTable from 'vue3-easy-data-table';
import 'vue3-easy-data-table/dist/style.css';
const app = createApp(App);
app.component('EasyDataTable', Vue3EasyDataTable);For quick prototyping without a build step, you can include the component via CDN. Ensure you include the CSS, Vue 3, and the component script in the correct order.
<link href="https://unpkg.com/vue3-easy-data-table/dist/style.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/vue@3.2.1/dist/vue.global.js"></script>
<script src="https://unpkg.com/vue3-easy-data-table"></script>
<div id="app">
<easy-data-table
:headers="headers"
:items="items"
/>
</div>
<script>
const App = {
components: {
EasyDataTable: window['vue3-easy-data-table'],
},
data () {
return {
headers:[
{ text: "Name", value: "name" },
{ text: "Height (cm)", value: "height", sortable: true },
{ text: "Weight (kg)", value: "weight", sortable: true },
{ text: "Age", value: "age", sortable: true }
],
items: [
{ "name": "Curry", "height": 178, "weight": 77, "age": 20 },
{ "name": "James", "height": 180, "weight": 75, "age": 21 },
{ "name": "Jordan", "height": 181, "weight": 73, "age": 22 }
],
}
},
};
Vue.createApp(App).mount('#app');
</script>You can use Vue3EasyDataTable by importing the default export from the package. It is exported as the DataTable component.
Import the component directly into your Vue component:
import DataTable from 'vue3-easy-data-table';If you are using Vue via a CDN and have a global Vue object available, the component automatically registers itself globally as Vue3EasyDataTable if window.Vue is detected.
The EasyDataTable component requires two main props: :headers (an array of column definitions) and :items (the data array).
<template>
<EasyDataTable
:headers="headers"
:items="items"
/>
</template>
<script lang="ts">
import type { Header, Item } from "vue3-easy-data-table";
export default defineComponent({
setup() {
const headers: Header[] = [
{ text: "Name", value: "name" },
{ text: "Height (cm)", value: "height", sortable: true },
{ text: "Weight (kg)", value: "weight", sortable: true },
{ text: "Age", value: "age", sortable: true }
];
const items: Item[] = [
{ "name": "Curry", "height": 178, "weight": 77, "age": 20 },
{ "name": "James", "height": 180, "weight": 75, "age": 21 },
{ "name": "Jordan", "height": 181, "weight": 73, "age": 22 }
];
return {
headers,
items
};
},
});
</script>Use the Header type to define the columns of your data table. Each header object specifies the display text, the data field key, and optional properties for sorting, fixing the column, or setting a specific width.
import { Header } from 'vue3-easy-data-table';
const headers: Header[] = [
{ text: 'Name', value: 'name', sortable: true },
{ text: 'Age', value: 'age', width: 100, fixed: true },
];The FilterOption type defines how specific fields should be filtered. It supports several comparison modes:
between: Requires criteria to be a tuple of two numbers [number, number].= | !=: Requires criteria to be a number or string.> | >= | < | <=: Requires criteria to be a number.in: Requires criteria to be an array of number[] or string[].(value: any, criteria: string) => boolean can be used with a string criteria.export type FilterOption = {
field: string
comparison: 'between'
criteria: [number, number]
} | {
field: string
comparison: '=' | '!='
criteria: number | string
} | {
field: string
comparison: '>' | '>=' | '<' | '<='
criteria: number
} | {
field: number | string
comparison: 'in'
criteria: number[] | string[]
}| {
field: string
comparison: (value: any, criteria: string) => boolean
criteria: string
}The Item type represents a single row of data in the table. It is a generic record where keys are column values and values can be any type.
export type Item = Record<string, any>The Item type represents a single row of data in the table. It is a generic record where keys are column values and values can be any type.
import { Item } from 'vue3-easy-data-table';
const items: Item[] = [
{ name: 'John Doe', age: 30, email: 'john@example.com' },
{ name: 'Jane Smith', age: 25, email: 'jane@example.com' },
];You can dynamically apply CSS classes to different parts of the table using these callback types:
HeaderItemClassNameFunction: Apply classes to a header based on the Header object and its columnNumber.BodyRowClassNameFunction: Apply classes to an entire row based on the Item data and its rowNumber.BodyItemClassNameFunction: Apply classes to a specific cell based on the column key and rowNumber.export type HeaderItemClassNameFunction = (header: Header, columnNumber: number) => string
export type BodyRowClassNameFunction = (item: Item, rowNumber: number) => string
export type BodyItemClassNameFunction = (column: string, rowNumber: number) => stringThe UpdateSortArgument is used to capture changes in the table's sorting state, providing the new sort type and the field being sorted.
import { UpdateSortArgument } from 'vue3-easy-data-table';
function onSortUpdate(arg: UpdateSortArgument) {
console.log('Sorted by:', arg.sortBy);
console.log('Sort type:', arg.sortType);
}The FilterOption type allows you to define complex filtering logic for specific fields. Supported comparison types include:
between: Requires a criteria array of two numbers [number, number].=: Exact match with a number or string.!=: Inequality with a number or string.>, >=, <, <=: Comparison operators requiring a number.in: Checks if a value exists within a number[] or string[] array.(value: any, criteria: string) => boolean.import { FilterOption } from 'vue3-easy-data-table';
const filters: FilterOption[] = [
{ field: 'age', comparison: 'between', criteria: [18, 65] },
{ field: 'status', comparison: 'in', criteria: ['active', 'pending'] },
{ field: 'name', comparison: '=', criteria: 'John' }
];