Introduce
Hello everyone, as everyone knows, handling display and interacting with data tables (datatable) is an important part of Website applications, especially data management applications (e.g. : Admin page, …). Many libraries were created to support programmers to quickly and fully fill these data tables with necessary functions.
And in VueJs
, there are also many packages that support displaying and interacting with data tables quickly, fully supporting functions such as searching, sorting, exporting, … data. In this article, I will introduce to you such a package, which is vue-materialize-datatable
, let’s learn about it.
Setting
You need to copy and paste the following command into the terminal to install vue-materialize-datatable
:
1 2 | npm i vue-materialize-datatable --save |
Note: vue-materialize-datatable
requires VueJs 2
and materialize-css library.
In addition, vue-materialize-datatable
also requires you to import Material Design icons
. You can import directly via Google’s CDN link or install the package and import files into the application:
- CDN: import in Html file or Css / Sass file:
- Html:12<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
- Css / Sass:12@import url(http://fonts.googleapis.com/icon?family=Material+Icons);
- Html:
- NPM package:
- Setting12npm i material-design-icons-iconfont -S
- Import:12@import "~material-design-icons-iconfont/dist/material-design-icons";
- Setting
Use
After installing vue-materialize-datatable
, you just need to import and declare it into your Vue application. And then you can use it via the <datatable>
component:
1 2 | import DataTable from "vue-materialize-datatable"; |
1 2 3 4 5 6 7 8 | { ... components: { ... "datatable": DataTable } } |
1 2 | <datatable></datatable> |
A simple example using vue-materialize-datatable
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | <template> <div id="app"> <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet"> <div class="content"> <datatable title="List Student" :columns="tableColumns1" :rows="tableRows1" /> </div> </div> </template> <script> import Vue from 'vue' import App from './App.vue' import 'materialize-css'; import 'materialize-css/dist/css/materialize.css'; import DataTable from "vue-materialize-datatable"; export default { name: 'app', data() { return { list: [], tableColumns1: [ { label: "Character name", field: "charName", numeric: false, html: false }, { label: "First appearance", field: "firstAppearance", numeric: false, html: false }, { label: "Created by", field: "createdBy", numeric: false, html: false }, { label: "Voiced by", field: "voicedBy", numeric: false, html: false } ], tableRows1: [ { charName: "Abu", firstAppearance: "Alladin (1992)", createdBy: "Joe Grant", voicedBy: "Frank Welker" }, { charName: "Magic Carpet", firstAppearance: "Peter (1994)", createdBy: "Randy Cartwright", voicedBy: "N/A" }, { charName: "The Sultan", firstAppearance: "John (1995)", createdBy: "Navid Negahban", voicedBy: "Douglas Seale" } ], } }, components: { "datatable": DataTable } } </script> <style> html, body { padding: 0; margin: 0; } .content { padding: 20px; } </style> |
Properties
vue-materialize-datatable
gives us the following properties to use and customize:
title
: The title of the data sheet (Ex: “List Student”)columns
: Set the information of the column headers (headers) of the data table, which is an array of objects. For example:12345678910[{label: "Name", // Tên cộtfield: "name", // Tên trường sẽ hiển thị từ dữ liệu của mỗi hàngnumeric: false, // Ảnh hưởng đến sortinghtml: false // Có hiển thị dữ liệu html hay không.},...];rows
: Set up the data information of rows (body) in the data table, whose values are an array of objects. For example:123456789[{name: 'test', // "name" sẽ mapping với data của "field" của các cột ở trên.age: 23,...},...];perPage
: Set the value for the selectBox showing the number of records of the data table on the page. (The default value is[10, 20, 30, 40, 50]
).defaultPerPage
: Set the default record number on the page (The default value is10
). If not set, then by default it will be the first value ofperPage
.initSortCol
: Set the column in which data will be sorted at initialization. (The default value is-1
– the table of data will not sort by any column.)onClick
: Listen for the click event on the data rows.clickable
: Set to allow click events on the data row (The default value istrue
).sortable
:sortable
whether or not to sort the data on the table (The default value istrue
).searchable
: Set the display of the search function (default value istrue
).exactSearch
: Disable fuzzy search (Default value istrue
).serverSearch
: Used when you want to search and retrieve data from the server (The default value isfalse
).serverSearchFunc
: Function used to search and retrieve data from the server. Note to use this function, you need to set theserverSearch
above totrue
.paginate
: Sets the display ofnext
/prev
buttons at the bottom of the data table (The default value istrue
).exportable
: Set to show the export function of data (The default value istrue
).printable
: Set to display the print function (default value istrue
).customButtons
: Custom setting of buttons, whose value is an array of objects. For example:123456789[{hide: false, // Có ẩn button khôngicon: "search", // Materialize icononclick: aFunc() // Lắng nghe sự kiện onclick của button},...];
Localization
vue-materialize-datatable
also supports multiple languages, you can set the display language via locale
attribute. Available languages that vue-materialize-datatable
support:
en
(English, default)ar
(Arabic)es
(Spanish)cat
(Catalan)br
(Brazilian Portuguese)nl
(Netherlands)fr
(French)
Row-click Event
If you set the clickable
property to true
(the default), you can bind the event row-click
as shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <datatable v-on:row-click="onRowClick"></datatable> <script> var app = new Vue({ el: '#app', ... methods: { onRowClick: function (row) { //row contains the clicked object from `rows` console.log(row) } }, }) </script> |
Row buttons
vue-materialize-datatable
also allows you to easily add button actions to the data table like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <datatable title="News" ...> <th slot="thead-tr"> Actions </th> <template slot="tbody-tr" scope="props"> <td> <button class="btn red darken-2 waves-effect waves-light compact-btn" @click="(e) => deleteItem(props.row, e)"> <i class="material-icons white-text">delete</i> </button> </td> </template> </datatable> |
Demo
I did a small demo using vue-materialize-datatable
as follows
Conclude
Through this article, I have introduced you to vue-materialize-datatable
, a package used to display and interact with data tables (datatable) in VueJs
applications quickly, simply, and fully. Full of necessary functions and easy customization.
Hope this article will be helpful for you