Creating tables from scratch is a pain.
This is why there are many table plugins to let us add tables easily.
In this article, we’ll look at how to add tables to a Vue app with the vue-good-table plugin.
Getting Started
We can get started by installing the package.
To do this, we run:
npm install --save vue-good-table
Then we can add the plugin to main.js
by writing:
import Vue from "vue";
import App from "./App.vue";
import VueGoodTablePlugin from "vue-good-table";
import "vue-good-table/dist/vue-good-table.css";
Vue.use(VueGoodTablePlugin);
Vue.config.productionTip = false;
new Vue({
render: (h) => h(App)
}).$mount("#app");
to add the styles and the plugin.
We can also import it into our component with:
import 'vue-good-table/dist/vue-good-table.css'
import { VueGoodTable } from 'vue-good-table';
//...
components: {
VueGoodTable,
}
Now we can add a simple table by writing:
<template>
<div>
<vue-good-table :columns="columns" :rows="rows"/>
</div>
</template>
<script>
export default {
data() {
return {
columns: [
{
label: "Name",
field: "name"
},
{
label: "Age",
field: "age",
type: "number"
},
{
label: "Birthday",
field: "birthDay",
type: "date",
dateInputFormat: "yyyy-MM-dd",
dateOutputFormat: "MMM do yy"
}
],
rows: [
{ id: 1, name: "John", age: 20, birthDay: "2000-01-20" },
{ id: 2, name: "Jane", age: 24, birthDay: "1996-10-31" },
{ id: 3, name: "Susan", age: 16, birthDay: "2004-10-30" }
]
};
}
};
</script>
We add the column definitions with the columns
pro[.
label
is the heading that’s displayed.
field
is the [property name of the field to display.
dateInputFormat
is the format of the date in the table row objects.
And dateOutputFormat
is the date output format that’s displayed.
The rows
prop has an array of objects which are displayed as rows.
Table Options
We can change many options with vue-good-table.
One of them is the max height.
This can be changed with the max-height
prop:
<template>
<div>
<vue-good-table :columns="columns" :rows="rows" max-height="300px"/>
</div>
</template>
<script>
export default {
data() {
return {
columns: [
{
label: "Name",
field: "name"
},
{
label: "Age",
field: "age",
type: "number"
},
{
label: "Birthday",
field: "birthDay",
type: "date",
dateInputFormat: "yyyy-MM-dd",
dateOutputFormat: "MMM do yy"
}
],
rows: [
{ id: 1, name: "John", age: 20, birthDay: "2000-01-20" },
{ id: 2, name: "Jane", age: 24, birthDay: "1996-10-31" },
{ id: 3, name: "Susan", age: 16, birthDay: "2004-10-30" }
]
};
}
};
</script>
We can keep the header fixed with the fixed-header
prop:
<template>
<div>
<vue-good-table :columns="columns" :rows="rows" fixed-header/>
</div>
</template>
<script>
export default {
data() {
return {
columns: [
{
label: "Name",
field: "name"
},
{
label: "Age",
field: "age",
type: "number"
},
{
label: "Birthday",
field: "birthDay",
type: "date",
dateInputFormat: "yyyy-MM-dd",
dateOutputFormat: "MMM do yy"
}
],
rows: [
{ id: 1, name: "John", age: 20, birthDay: "2000-01-20" },
{ id: 2, name: "Jane", age: 24, birthDay: "2011-10-31" },
{ id: 3, name: "Susan", age: 16, birthDay: "2011-10-30" }
]
};
}
};
</script>
Conclusion
We can add tables easily into a Vue app with the vue-good-table plugin.