如何解决Vuetify数据表:如何应用标头过滤器功能
根据custom filters in v-data-table的Vuetify文档,可以通过在标题项上提供 filter 属性来自定义特定表列的过滤。
在我的应用程序中,我想使用它为单个列自定义过滤。对于所有其他列,我想保留默认的字符串匹配行为。
作为起点(see codepen),我提供了一个自定义过滤器,该过滤器仅返回 true :
new Vue({
el: '#app',vuetify: new Vuetify(),data(){
return {
headers: [
{
text: 'Car Manufacturers',value: 'car',filter(){ return true }
},{
text: 'Country of Origin',value: 'country'
}
],items: [
{ car: 'Honda',country: 'Japan' },{ car: 'Audi',country: 'Germany' },{ car: 'Ford',country: 'USA' }
],search: ''
}
},})
和
<div id="app">
<v-app>
<v-container>
<v-text-field
label="Search"
v-model="search"
>
</v-text-field>
<v-data-table
:headers="headers"
:items="items"
:search="search"
>
</v-data-table>
</v-container>
</v-app>
</div>
我现在希望,汽车列上的过滤器与所有行匹配,因此无论我输入什么搜索字符串,都将显示整个表。
我观察到的是,该表仍处于过滤状态,但仅按国家/地区列进行过滤。
相反,当我更改过滤器以返回 false 时,将显示一个空表。然后,我会希望该表仅按国家/地区列进行过滤。
标题过滤器如何应用?或者,更准确地说:
对于每一行,各个列过滤器的结果如何合并以产生整行的过滤器结果?
解决方法
经过一番挖掘,我在关于v数据表过滤的错误报告中找到了this answer。
我仍然不了解其背后的想法,但是显然,自定义列过滤器的处理方式与没有自定义过滤器的列的处理方式不同:
行匹配,如果(1)匹配 every()指定的自定义列过滤器,并且(2)匹配 some()默认过滤器(即包含搜索字符串)
Vuetify中的匹配代码似乎是this:
return items.filter(item => {
// Headers with custom filters are evaluated whether or not a search term has been provided.
// We need to match every filter to be included in the results.
const matchesColumnFilters = headersWithCustomFilters.every(filterFn(item,search,defaultFilter))
// Headers without custom filters are only filtered by the `search` property if it is defined.
// We only need a single column to match the search term to be included in the results.
const matchesSearchTerm = !search || headersWithoutCustomFilters.some(filterFn(item,customFilter))
return matchesColumnFilters && matchesSearchTerm
})
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。