【发布时间】:2018-05-01 10:45:11
【问题描述】:
我有一个 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 对象。
【问题讨论】:
标签: javascript vue.js