Vuetify is a popular Vue.js
framework known for its powerful and flexible Material Design components. It follows
Data tables in Vuetify are components that provide a way to display, sort, filter, and paginate data in a tabular format. These tables are highly customizable and can handle a wide variety of data types and structures. They are designed to be responsive, accessible, and efficient, making them suitable for both simple and complex data presentation needs.
Let's look at some of the key features of Vuetify data tables:
Sorting: Allows to sort the columns of the data table in ascending or descending order.
Filtering: Allows to filter the data based on the user input.
Pagination: Allows to break large datasets into manageable pages.
Selectable rows: Allows to select the rows for further actions.
Customizable columns: Allows to dynamically add, remove, or customize the columns.
Let's see how we can create data tables in Vuetify.
Step 1: Install Vuetify using the following npm
command:
npm install vuetify
Step 2: Set up a basic Vue.js project, if not already done, as follows:
vue create my-project
Step 3: Once our project is set up, add Vuetify to it as follows:
cd my-projectvue add vuetify
Step 4: Once Vuetify is added to our project, we can create the data table using the v-data-table
component as follows:
<v-data-table:items="items" // The items of the table></v-data-table>
In line 2, :items
is a Vue directive that binds the items
property (a data variable) from the component's script section to the v-data-table
component. This directive tells Vue to dynamically use the data stored in items
to populate the table's rows.
Step 4: Add a script that will have the definition of items
as follows:
<template><v-container><v-data-table:headers="headers" // Binds the column definitions from the script section:items="items" // Binds the data to be displayed from the script section></v-data-table></v-container></template><script>export default {data() {return {headers: [{ text: "Name", value: "name" }, // Column definition for "Name"{ text: "Email", value: "email" }, // Column definition for "Email"{ text: "Role", value: "role" }, // Column definition for "Role"],items: [ // Data to be displayed in the table{ name: "John Doe", email: "john.doe@example.com", role: "Admin" },{ name: "Jane Smith", email: "jane.smith@example.com", role: "Editor" },{ name: "Sam Johnson", email: "sam.johnson@example.com", role: "Viewer" },],};},};</script>
This setup creates a simple Vuetify data table displaying names, emails, and roles. We can expand this with additional features like sorting, filtering, and pagination as needed.
Let's look at an example demonstrating a Vuetify data table. Click the “Run” button to execute the example.
The code above displays a data table with four columns: Name
, Calories
, Fat(g)
and Carbohydrates(g)
. This table demonstrates the use of various Vuetify features such as filtering, sorting, pagination, extendable rows, and selectable rows.
The code in App.vue
can be divided into three sections—template, script and style. Let's dive into the template and the script section.
Template section
Lines 3–31: Inside <v-app>
, there's a <v-data-table>
component. This component displays tabular data.
Lines 4–10: Various props like headers
, items
, search
, pagination
, expanded
, and selected
are bound to the respective data properties in the Vue instance.
Lines 4–5: :headers
and :items
props bind to arrays of objects representing table headers and data rows, respectively.
Line 6: :search
prop binds to a search query string used to filter table data.
Line 7: :pagination.sync
binds to a pagination object with properties for page number, items per page, and total items.
Lines 9–10: v-model:expanded
and v-model:selected
are used for managing expanded rows and selected rows, respectively.
Line 11: item-value="name"
specifies which property of the item object should be used as a unique identifier for each row.
Lines 12–13: show-expand
and show-select
enable features to expand rows and select rows, respectively.
Lines 16–21: Inside <v-data-table>
, there's a <template v-slot:top>
which contains a text field used for searching/filtering the table.
Lines 23–29: Another template <template v-slot:expanded-row>
is used to customize the expanded row content.
Script section
The script section contains a Vue.js component definition using export default {}
.
Lines 37–71: Inside the data()
function, initial data properties like headers
, items
, search
, pagination
, expanded
, and selected
are defined and returned.
headers
and items
are arrays containing objects representing table headers and data rows, respectively.
search
is initialized as an empty string to store the search query.
pagination
object contains properties for page number, items per page, and total items.
expanded
and selected
are arrays used to manage expanded rows and selected rows, respectively.
That's about the creation of data tables in Vuetify.
To get a deeper understanding of pagination and filtering, we may explore the following answers:
In conclusion, Vuetify allows us to create highly interactive and dynamic data tables tailored to the application’s needs. By leveraging the features provided by Vuetify, we can build robust and user-friendly web applications that meet the needs of the end users.
Free Resources