≡

Quick Start: Vue

Let's get dynamic and interactive data-driven tables into your Vue app!

A Vue to a Grid

This quick start guide will get you started in just 60 seconds and then explore how you can make use of DataTables features to customise the data grid for your exact needs.

1. Install

First install DataTables and its Vue component:

npm install datatables.net-dt datatables.net-vue3

2. Load

We'll use a Single-File Component (i.e. a .vue file) in this quick-start, but you can of course also use a .tsx, .ts or .js file, as your project needs.

With that in mind, let's make DataTables available in your own component. This is done by importing the two libraries we installed, assigning DataTables to the Vue component and including the styling:

<script setup lang="ts">
import DtCore from 'datatables.net';
import DataTable, { Column } from 'datatables.net-vue3';

DataTable.use(DtCore);
</script>

<style>
    @import url('datatables.net-dt');
</style>

<template>

</template>

3. Data

You'll have your own data that you want to display, but for this guide, let's use some simple data to show some of the principles of how to use DataTables in Vue:

const data = [
    { id: 1, title: 'Designing Data-Intensive Applications', author: 'Martin Kleppmann', releaseDate: '2017-03-16', price: 29.99, finished: true },
    { id: 2, title: 'The Phoenix Project', author: 'Gene Kim et al.', releaseDate: '2013-01-15', price: 14.95, finished: true },
    { id: 3, title: 'Project Hail Mary', author: 'Andy Weir', releaseDate: '2021-05-04', price: 18.00, finished: false },
    { id: 4, title: 'Refactoring (2nd Ed)', author: 'Martin Fowler', releaseDate: '2018-11-20', price: 24.50, finished: false },
    { id: 5, title: 'Staff Engineer', author: 'Will Larson', releaseDate: '2021-03-08', price: 12.99, finished: true },
];

4. Display

Finally, let's get that data displayed in a DataTable using the template:

<template>
    <DataTable :data="data" className="display">
        <Column title="Title" data="title" />
        <Column title="Author" data="author" />
        <Column title="Released" data="releaseDate" :render="DtCore.render.date()" />
        <Column title="Price" data="price" :render="DtCore.render.number(null, null, 2, '$')" />
        <Column title="Status" data="finished" v-slot="{cellData}">
            {{cellData ? '✓ Read' : '⏳ Unread'}}
        </Column>
    </DataTable>
</template>

The end result is a simple Vue app with the table displayed:

There are a couple of points in the component code used above that are worth detailing to explain their usage so you can build tables that will be driven from your own data:

  • Each column you wish to show is defined by a <Column> component, as a child of the DataTable (more details on columns).
  • The title property is used to define the text for the column header cell.
  • The data property is used to map to the data in the source object for each row.
  • Data renderers can be defined by the render property. In this case date and number formatters are used to make sure the information is displayed in a format suitable for the user's locale.
  • The final column uses a child template to compute and display the value to show. This template can also contain other components if you wish to show a Vue component in a column (e.g. a button or tag).

Just scratching the surface

You've now created your first DataTable in Vue - a high performance, fully interactive and dynamic table. Hurrah! This is just the start of your journey though.

For your next steps:

This quick start guide is new! If you've followed along and have any feedback, please get in touch, so it can be improved to help others as well!