组件 this指向vue
VUE表组件 (vue-table-component)
A straightforward Vue component to filter and sort tables.
一个简单的Vue组件,用于过滤和排序表。
This repo contains a Vue component that can render a filterable and sortable table. It aims to be very lightweight and easy to use. It has support for retrieving data asynchronously and pagination.
此仓库包含一个Vue组件,该组件可以呈现可过滤和可排序的表。 它的目标是非常轻巧且易于使用。 它支持异步检索数据和分页。
Here's an example of how you can use it:
这是一个如何使用它的示例:
<table-component
:data="[
{ firstName: 'John', lastName: 'Lennon', instrument: 'Guitar', birthday: '04/10/1940', songs: 72 },
{ firstName: 'Paul', lastName: 'McCartney', instrument: 'Bass', birthday: '18/06/1942', songs: 70 },
{ firstName: 'George', lastName: 'Harrison', instrument: 'Guitar', birthday: '25/02/1943', songs: 22 },
{ firstName: 'Ringo', lastName: 'Starr', instrument: 'Drums', birthday: '07/07/1940', songs: 2 },
]"
sort-by="songs"
sort-order="asc"
>
<table-column show="firstName" label="First name"></table-column>
<table-column show="lastName" label="Last name"></table-column>
<table-column show="instrument" label="Instrument"></table-column>
<table-column show="songs" label="Songs" data-type="numeric"></table-column>
<table-column show="birthday" label="Birthday" data-type="date:DD/MM/YYYY"></table-column>
<table-column label="" :sortable="false" :filterable="false">
<template slot-scope="row">
<a :href="`#${row.firstName}`">Edit</a>
</template>
</table-column>
</table-component>
A cool feature is that the table caches the used filter and sorting for 15 minutes. So if you refresh the page, the filter and sorting will still be used.
一项很酷的功能是该表将使用的过滤器和排序缓存15分钟。 因此,如果刷新页面,则仍将使用过滤器和排序。
演示版 (Demo)
Want to see the component in action? No problem. Here's a demo.
是否希望看到实际的组件? 没问题。 这是一个演示 。
安装 (Installation)
You can install the package via yarn:
您可以通过yarn安装软件包:
yarn add vue-table-component
or npm:
或npm:
npm install vue-table-component --save
Next, you must register the component. The most common use case is to do that globally.
接下来,您必须注册该组件。 最常见的用例是全局执行此操作。
//in your app.js or similar file
import Vue from 'vue';
import { TableComponent, TableColumn } from 'vue-table-component';
Vue.component('table-component', TableComponent);
Vue.component('table-column', TableColumn);
Alternatively you can do this to register the components:
或者,您可以执行以下操作来注册组件:
import TableComponent from 'vue-table-component';
Vue.use(TableComponent);
浏览器支持 (Browser Support)
vue-table-component
has the same browser support as Vue (see https://github.com/vuejs/vue). However, you might need to polyfill the Array.prototype.find
method for IE support.
vue-table-component
与Vue具有相同的浏览器支持(请参阅https://github.com/vuejs/vue )。 但是,您可能需要为IE支持多Array.prototype.find
方法。
用法 (Usage)
Here's a simple example on how to use the component.
这是有关如何使用组件的简单示例。
<table-component
:data="[
{ firstName: 'John', birthday: '04/10/1940', songs: 72 },
{ firstName: 'Paul', birthday: '18/06/1942', songs: 70 },
{ firstName: 'George', birthday: '25/02/1943', songs: 22 },
{ firstName: 'Ringo', birthday: '07/07/1940', songs: 2 },
]"
sort-by="songs"
sort-order="asc"
>
<table-column show="firstName" label="First name"></table-column>
<table-column show="songs" label="Songs" data-type="numeric"></table-column>
<table-column show="birthday" label="Birthday" :filterable="false" data-type="date:DD/MM/YYYY"></table-column>
</table-component>
This will render a table that is both filterable and sortable. A filter field will be displayed right above the table. If your data contains any html we will filter that out when filtering. You can sort the table by clicking on the column headers. By default it will remember the used filter and sorting for the next 15 minutes.
这将呈现一个既可过滤又可排序的表。 表格上方会显示一个过滤字段。 如果您的数据包含任何html,我们将在过滤时将其过滤掉。 您可以通过单击列标题对表进行排序。 默认情况下,它将在接下来的15分钟内记住使用的过滤器和排序。
道具 (Props)
You can pass these props to table-component
:
您可以将这些道具传递给table-component
:
data
: (required) the data the component will operate on. This can either be an array or a functiondata
:(必需)组件将在其上运行的数据。 这可以是数组或函数show-filter
: set this tofalse
to not display thefilter
field.show-filter
:将其设置为false
以不显示filter
字段。show-caption
: set this tofalse
to not display thecaption
field which shows the current active filter.show-caption
:将其设置为false
以不显示显示当前活动过滤器的caption
字段。sort-by
: the property in data on which to initially sort.sort-by
:数据的初始排序属性。sort-order
: the initial sort order.sort-order
:初始排序顺序。cache-lifetime
: the lifetime in minutes the component will cache the filter and sorting.cache-lifetime
:组件将缓存过滤器和排序的生存时间(以分钟为单位)。cache-key
: if you use multiple instances oftable-component
on the same page you must set this to a unique value per instance.cache-key
:如果您在同一页面上使用table-component
多个实例,则必须将其设置为每个实例的唯一值。table-class
: the passed value will be added to theclass
attribute of the rendered tabletable-class
:传递的值将添加到呈现表的class
属性中thead-class
: the passed value will be added to theclass
attribute of the rendered table head.thead-class
:传递的值将添加到呈现的表头的class
属性中。tbody-class
: the passed value will be added to theclass
attribute of the rendered table body.tbody-class
:传递的值将添加到呈现的表主体的class
属性中。filter-placeholder
: the text used as a placeholder in the filter fieldfilter-placeholder
:在过滤器字段中用作filter-placeholder
的文本filter-input-class
: additional classes that you will be applied to the filter text inputfilter-input-class
:将应用于过滤器文本输入的其他类filter-no-results
: the text displayed when the filtering returns no resultsfilter-no-results
:过滤不返回任何结果时显示的文本
For each table-column
a column will be rendered. It can have these props:
对于每个table-column
列,将呈现一列。 它可以具有以下道具:
show
: (required) the property name in the data that needs to be shown in this column.show
:(必需)需要在此列中显示的数据中的属性名称。formatter
: a function the will receive the value that will be displayed and all column properties. The return value of this function will be displayed. Here's an exampleformatter
:一个函数,它将接收将要显示的值和所有列属性。 将显示此函数的返回值。 这是一个例子label
: the label that will be shown on top of the column. Set this to an empty string to display nothing. If this property is not present, the string passed toshow
will be used.label
:将在列顶部显示的标签。 将此设置为空字符串不显示任何内容。 如果不存在此属性,则将使用传递给show
的字符串。data-type
: if your column should be sorted numerically set this tonumeric
. If your column contains dates set it todate:
followed by the format of your datedata-type
:如果您的列应按numeric
排序,则将其设置为numeric
。 如果您的列中包含日期,则将其设置为date:
后跟date:
格式sortable
: if you set this tofalse
then the column won't be sorted when clicking the column headersortable
:如果将其设置为false
则在单击列标题时不会对该列进行排序sort-by
: you can set this to any property present indata
. When sorting the column that property will be used to sort on instead of the property inshow
.sort-by
:您可以将其设置为data
存在的任何属性。 对列进行排序时,将使用属性代替show
中的属性进行排序。filterable
: if this is set tofalse
than this column won't be used when filteringfilterable
:如果将其设置为false
,则过滤时将不使用此列filter-on
: you can set this to any property present indata
. When filtering the column that property will be used to filter on instead of the property inshow
.filter-on
:您可以将其设置为data
存在的任何属性。 过滤列时,将使用属性代替show
中的属性进行过滤。hidden
: if you set this totrue
then the column will be hidden. This is useful when you want to sort by a field but don't want it to be visible.hidden
:如果将其设置为true
则该列将被隐藏。 当您想按字段排序但不希望其可见时,此功能很有用。header-class
: the passed value will be added to theclass
attribute of the columnsth
element.header-class
:传递的值将添加到th
列的class
属性。cell-class
: the passed value will be added to theclass
attribute of the columnstd
element.cell-class
:传递的值将添加到td
列元素的class
属性中。
听众 (Listeners)
The table-component
currently emits one custom event:
table-component
当前发出一个自定义事件:
@rowClick
: is fired when a row is clicked. Receives the row data as it's event payload.@rowClick
:单击某行时将触发。 接收行数据作为事件有效负载。
修改使用的文本和CSS类 (Modifying the used texts and CSS classes)
If you want to modify the built in text or classes you can pass settings globally. You can use the CSS from the docs as a starting point for your own styling.
如果要修改内置文本或类,则可以全局传递设置。 您可以将文档中的CSS用作自己样式的起点。
import TableComponent from 'vue-table-component';
TableComponent.settings({
tableClass: '',
theadClass: '',
tbodyClass: '',
filterPlaceholder: 'Filter table…',
filterNoResults: 'There are no matching rows',
});
You can also provide the custom settings on Vue plugin install hook:
您还可以在Vue插件安装挂钩上提供自定义设置:
import Vue from 'vue';
import TableComponent from 'vue-table-component';
Vue.use(TableComponent, {
tableClass: '',
theadClass: '',
tbodyClass: '',
filterPlaceholder: 'Filter table…',
filterNoResults: 'There are no matching rows',
});
异步检索数据 (Retrieving data asynchronously)
The component can fetch data in an asynchronous manner. The most common use case for this is fetching data from a server.
该组件可以异步方式获取数据。 最常见的用例是从服务器获取数据。
To use the feature you should pass a function to the data
prop. The function will receive an object with filter
, sort
and page
. You can use these parameters to fetch the right data. The function should return an object with the following properties:
要使用该功能,您应该将一个函数传递给data
道具。 该函数将接收带有filter
, sort
和page
的对象。 您可以使用这些参数来获取正确的数据。 该函数应返回具有以下属性的对象:
data
: (required) the data that should be displayed in the table.data
:(必需)应在表中显示的数据。pagination
: (optional) this should be an object with keyscurrentPage
andtotalPages
. IftotalPages
is higher than 1 pagination links will be displayed.pagination
:(可选)这应该是一个具有键currentPage
和totalPages
的对象。 如果totalPages
大于1,则将显示分页链接。
Here's an example:
这是一个例子:
<template>
<div id="app">
<table-component :data="fetchData">
<table-column show="firstName" label="First name"></table-column>
</table-component>
</div>
</template>
<script>
import axios from 'axios';
export default {
methods: {
async fetchData({ page, filter, sort }) {
const response = await axios.get('/my-endpoint', { page });
// An object that has a `data` and an optional `pagination` property
return response;
}
}
}
</script>
If you for some reason need to manually refresh the table data, you can call the refresh
method on the component.
如果出于某种原因需要手动刷新表数据,则可以在组件上调用refresh
方法。
<table-component :data="fetchData" ref="table">
<!-- Columns... -->
</table-component>
this.$refs.table.refresh();
格式化值 (Formatting values)
You can format values before they get displayed by using scoped slots. Here's a quick example:
您可以使用作用域槽将值格式化后再显示。 这是一个简单的例子:
<table-component
:data="[
{ firstName: 'John', songs: 72 },
{ firstName: 'Paul', songs: 70 },
{ firstName: 'George', songs: 22 },
{ firstName: 'Ringo', songs: 2 },
]"
>
<table-column label="My custom column" :sortable="false" :filterable="false">
<template slot-scope="row">
{{ row.firstName }} wrote {{ row.songs }} songs.
</template>
</table-column>
</table-component>
Alternatively you can pass a function to the formatter
prop. Here's an example Vue component that uses the feature.
或者,您可以将一个函数传递给formatter
prop。 这是使用该功能的Vue组件示例。
<template>
<table-component
:data="[{ firstName: 'John' },{ firstName: 'Paul' }]">
<table-column show="firstName" label="First name" :formatter="formatter"></table-column>
</table-component>
</template>
<script>
export default {
methods: {
formatter(value, rowProperties) {
return `Hi, I am ${value}`;
},
},
}
</script>
This will display values Hi, I am John
and Hi, I am Paul
.
这将显示值Hi, I am John
和Hi, I am Paul
。
添加表页脚<tfoot>
信息 (Adding table footer <tfoot>
information)
Sometimes it can be useful to add information to the bottom of the table like summary data. A slot named tfoot
is available and it receives all of the rows
data to do calculations on the fly or you can show data directly from whatever is available in the parent scope.
有时,将信息(如摘要数据)添加到表的底部非常有用。 一个名为tfoot
的插槽是可用的,它可以接收所有rows
数据以进行即时计算,或者您可以直接从父级作用域中的任何内容显示数据。
<table-component
:data="[{ firstName: 'John', songs: 72 },{ firstName: 'Paul', songs: 70 }]">
<table-column show="firstName" label="First name"></table-column>
<table-column show="songs" label="Songs" data-type="numeric"></table-column>
<template slot="tfoot" slot-scope="{ rows }">
<tr>
<th>Total Songs:</th>
<th>{{ rows.reduce((sum, value) => { return sum + value.data.songs; }, 0) }}</th>
<th> </th>
<th> </th>
</tr>
</template>
</table-component>
OR
要么
<template>
<table-component
:data="tableData">
<table-column show="firstName" label="First name"></table-column>
<table-column show="songs" label="Songs" data-type="numeric"></table-column>
<template slot="tfoot">
<tr>
<th>Total Songs:</th>
<th>{{ totalSongs }}</th>
</tr>
</template>
</table-component>
</template>
<script>
export default {
computed: {
totalSongs () {
return this.tableData.reduce(sum, value => {
return sum + value.songs;
}, 0);
}
},
data () {
return {
tableData: [{ firstName: 'John', songs: 72 },{ firstName: 'Paul', songs: 70 }]
}
}
}
</script>
Note: rows
slot scope data includes more information gathered by the Table Component (e.g. columns
) and rows.data
is where the original data
information is located.
注意: rows
槽位作用域数据包括由表组件(例如columns
)收集的更多信息,而rows.data
是原始data
信息所在的位置。
翻译自: https://vuejsexamples.com/a-straight-to-the-point-vue-component-to-display-tables/
组件 this指向vue