我有一个 Vue.js 应用程序。在此应用程序中,我试图将过滤器值动态应用于对象的 Array
。 Array 中的每个对象都有字段。我正在尝试按字段值过滤这些对象。每个字段都可以按多个值过滤。
在这个时候,我一直没有弄清楚如何进行这种过滤。我试过使用 JavaScript 内置的 filter
函数。但是,这总是为我返回一个空的结果集。我把这个 Fiddle 放在一起,其中包括以下代码:
new Vue({
el: '#app',
data: {
currentFilterProperty: '',
currentFilterValue: '',
cols: [
{ title: 'Name', prop:'name' },
{ title: 'Age', prop:'age' },
{ title: 'Birthday', prop:'birthday' },
],
dataFilters: [],
data: [
{ name:'Patricia Miller', age:69, birthday:'04-15-1948' },
{ name:'Bill Baggett', age:62, birthday:'05-07-1955' },
{ name:'Maxine Thies', age:21, birthday:'11-28-1995' },
{ name:'Alison Battle', age:65, birthday:'08-07-1952' },
{ name:'Dick Triplett', age:25, birthday:'08-27-1982' }
]
},
methods: {
addFilter: function() {
var f = this.dataFilters[this.currentFilterProperty];
if (!f) {
this.dataFilters = {};
this.dataFilters[this.currentFilterProperty] = [ this.currentFilterValue ];
} else {
this.dataFilters[this.currentFilterProperty].push(this.currentFilterValue);
}
// How to apply filter?
}
}
})
我不确定如何将过滤器应用于 data
对象。
原文由 user687554 发布,翻译遵循 CC BY-SA 4.0 许可协议
完整的解决方案。最佳测试:添加过滤器 Age 62,然后添加 Birthday 04-15-1948,然后在 Name Patricia 中添加“tri”。