1.1.2 • Published 3 years ago

@2alheure/vue-datatable v1.1.2

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

Vue.js DataTable

Vue.js DataTable is a Vue.js component that offers functionnalities for tables, such as ordering, searching or pagination.
It has been inspired by the jQuery plugin DataTables.
This component is compatible with Vue 3.

Table of contents

Installation

First run npm install @2alheure/vue-datatable. Then you can import the component with import DataTable from "@2alheure/vue-datatable";.

Dependencies

This package depends on two others:

Test

The test command in this package uses some random users provided by @pixelastic.

Usage

Basic Usage

The minimum configuration usage is like following:

<DataTable :headers="headers" v-model="users" />

<script>
import DataTable from "@2alheure/vue-datatable";

export default {
  components: {
    DataTable
  },
  data() {
    return {
      headers: [
        { html: "ID", linkTo: "id" },
        { html: "Gender", linkTo: "gender" },
        { html: "First Name", linkTo: "first_name" },
        { html: "Last Name", linkTo: "last_name" },
        { html: "Email", linkTo: "email" },
        { html: "IP Address", linkTo: "ip_address" }
      ],
      users: [
        {
          "id": 1,
          "first_name": "<b>Virginie</b>",
          "last_name": "Merrin",
          "email": "vmerrin0@howstuffworks.com",
          "gender": "Female",
          "ip_address": "86.87.125.205"
        },
        {
          "id": 2,
          "first_name": "<b>Mariana</b>",
          "last_name": "Dorgan",
          "email": "mdorgan1@indiegogo.com",
          "gender": "Female",
          "ip_address": "188.236.23.74"
        },
        {
          "id": 3,
          "first_name": "<b>Karalee</b>",
          "last_name": "Dod",
          "email": "kdod2@va.gov",
          "gender": "Female",
          "ip_address": "82.75.65.131"
        }
      ]      
    }
  }
};
</script>

Please note the v-model directive. The users object will be modified by the component. If needed, think of making a copy of your data, and not passing it directly to the component.
Please note that in this configuration, the order of the headers prop does matters. Columns will be displayed in that order.
headers.linkTo property must correspond to a property of the v-model linked object.

Template / Slot usage

You can also be more verbous and write your own rows' templates.
Here is an example, corresponding to the one above:

<DataTable v-model="users">
  <template #thead>
    <tr>
      <th>ID</th>
      <th>Gender</th>
      <th>First Name</th>
      <th>Last Name</th>
      <th>Email</th>
      <th>IP Address</th>
    </tr>
  </template>

  <tr v-for="(user, index) in users" :key="index">
  <td
      v-for="(header, headerIndex) in headers"
      :key="index+'-'+headerIndex"
      v-html="user[header.linkTo]"
  ></td>
  </tr>
</DataTable>

<script>
import DataTable from "@2alheure/vue-datatable";

export default {
  components: {
    DataTable
  },
  data() {
    return {
      users: [
        // Same as above
      ]
    }
};
</script>

The <template #thead> describes the content of the <thead> tag of the table.
The rest stands for the <tbody> tag. You can also specify the <template #default> tag but I am lazy so I don't (😜).
As the templates renders according to users and users is used in the v-model, it re-renders automatically with rows that need to be printed.
This will produce the same result as the previous example, except that you won't have the column ordering. To do so, you can use the data-order-by attribute on the header:

  <template #thead>
    <tr>
      <th data-order-by="id">ID</th>
      <th data-order-by="gender">Gender</th>
      <th data-order-by="first_name">First Name</th>
      <th data-order-by="last_name">Last Name</th>
      <th data-order-by="email">Email</th>
      <th data-order-by="ip_address">IP Address</th>
    </tr>
  </template>

Props

NameTypeDefault valueDescription
modelValueArraynoneThe prop linked to the v-model directive (mandatory).
headersArraynoneRefer to the "About the headers prop" section.
identifierStringA randomly generated stringThe id of the component. Used by default to distinguish two different DataTable components.
ascSymbolString&#8595;The symbol when a column is ordered ascendingly.
descSymbolString&#8593;The symbol when a column is ordered descendingly.
neutralSymbolString&#8597;The symbol when a column is not ordered.
byPageOptionsArray[10, 25, 50, 100]The possible values for the number of elements by page.
paginationPropsObjectnullSee the Pagination component props.
hoverBooleanfalseWhether an hovered row should be styled to be a bit more visible.
stripeBooleanfalseWhether the table should style rows such as even rows are greyish.
translationString | ObjectenRefer to the "About translations" section.

About the headers prop

The headers prop is mandatory when you use the DataTable without specifying the <template #thead>.
It must be an array of objects, each one containing two keys:

  • html: This will be the HTML content of the <th> tag
  • linkTo: This is the property of the corresponding key in the v-model linked prop for the column.

About translations

A translation can be a string, corresponding to one of the following values:

  • de: german
  • en: english (default value)
  • es: spanish
  • fr: french

You can also provide an object, which will replace the default translation sentences.
The translation object looks like following (which is the default one):

{
  "noContent": "There is nothing to print.",
  "showing": "Showing lines {0} to {1} of {2}.",
  "search": "Search"
}

If you provide an object, it must match this template, and all keys are required.

Events

NameValue typeValueDescription
update:modelValueArrayThe new value of the v-model linked propEmitted each time a change in made in the component
pageNumberThe page numberEmitted each time the page changes (including when searching)
by-pageNumberThe number of elements by pageEmitted each time the number of elements by page changes
order-byStringThe column to order byEmitted each time the order changes
searchStringThe search stringEmitted each time a search is made