@2alheure/vue-datatable 中文文档教程

发布于 3年前 浏览 20 项目主页 更新于 3年前

Vue.js DataTable

Vue.js DataTable 是一个 Vue.js 组件,它为表格提供功能,例如排序、搜索或分页。
它的灵感来自 jQuery 插件 DataTables
该组件兼容 Vue 3。

Table of contents

Installation

首先运行 npm install @2alheure/vue-datatable。 然后您可以使用 import DataTable from "@2alheure/vue-datatable"; 导入组件。

Dependencies

此包依赖于另外两个:此包中

Test

test 命令使用 @pixelastic

Usage

Basic Usage

最低配置用法如下:

<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>

请注意 v-model 指令。 users 对象将被组件修改。 如果需要,请考虑复制您的数据,而不是将其直接传递给组件。
请注意,在此配置中,headers 属性的顺序很重要。 列将按该顺序显示。
headers.linkTo 属性必须对应于 v-model 链接对象的属性。

Template / Slot usage

您也可以更冗长并编写自己的行模板。
下面是一个示例,对应于上面

<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>

的那个:

  <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

headers prop is ma​​ndatory when you use the DataTable without指定

  • 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

翻译可以是一个字符串,对应于以下值之一:

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

您也可以提供一个对象,它将替换默认的翻译句子。
翻译对象如下所示(这是默认的):

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

如果您提供一个对象,它必须匹配此模板,并且所有键都是必需的。

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

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
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文