Javascript 使用计算属性向Vue.js表中添加筛选器
我有一个使用对象数组生成的表。我很难弄明白如何使用计算属性来过滤对象数组。我正在使用VuE.js。我不知道如何正确使用计算属性中的filter()来筛选表Javascript 使用计算属性向Vue.js表中添加筛选器,javascript,vue.js,vuejs2,Javascript,Vue.js,Vuejs2,我有一个使用对象数组生成的表。我很难弄明白如何使用计算属性来过滤对象数组。我正在使用VuE.js。我不知道如何正确使用计算属性中的filter()来筛选表 new Vue({ el:"#app", data: () => ({ search:'', programs: [], editableKeys: ['date', 'company', 'funding', 'funded', 'recruit', 'program'], }), create
new Vue({
el:"#app",
data: () => ({
search:'',
programs: [],
editableKeys: ['date', 'company', 'funding', 'funded', 'recruit', 'program'],
}),
created () {
this.getPrograms();
},
methods: {
getPrograms() {
axios.get("https://my-json-server.typicode.com/isogunro/jsondb/Programs").then(response => {
this.programs = response.data.map(program => ({
...program,
isReadOnly: true,
dropDowns: ["Apple","Google"]
}));
}).catch(error => {
console.log(error);
});
},
editItem (program) {
program.isReadOnly = false
},
saveItem (program) {
program.isReadOnly = true
console.log(program)
alert("New Value: "+program.company)
alert("Previous Value: "+program.company)
},
bgColor (program) {
return program.funded === program.funding ? 'yellow' : 'white'
},
formatDate(program){
var formatL = moment.localeData().longDateFormat('L');
var format2digitYear = formatL.replace(/YYYY/g,'YY');
return moment(program.Date).format(format2digitYear);
},
updateField(program){
console.log(program)
alert(program)
}
},
computed: {
searchContents(){
this.programs.filter(this.search === )//??? not sure how to filter correctly
}
}
})
以下是计算属性必须返回的值,您可以将其与数据和道具一样使用。因此,您需要做的是返回过滤器的结果。对于没有
搜索
选项的情况,您可以返回未经筛选的原始程序
计算属性如下所示:
(如果您按其资金属性筛选计划。)
计算:{
searchContents(){
如果(this.search==''){
把这个还给我
}
返回this.programs.filter(x=>this.search==x.filter)
}
}
然后您可以在v-for
中使用该计算属性:
<tr v-for="(program, index) in searchContents">
计算属性必须返回一个值,您可以将其与数据和道具一样使用。因此,您需要做的是返回过滤器的结果。对于没有搜索
选项的情况,您可以返回未经筛选的原始程序
计算属性如下所示:
(如果您按其资金属性筛选计划。)
计算:{
searchContents(){
如果(this.search==''){
把这个还给我
}
返回this.programs.filter(x=>this.search==x.filter)
}
}
然后您可以在v-for
中使用该计算属性:
<tr v-for="(program, index) in searchContents">
这是否回答了您的问题?这回答了你的问题吗?我做了改变,但什么也没发生。看起来很有效。当您在搜索框中键入NO
时,该表仅显示NO
作为funding
值的数据。哦……我希望进行逐步搜索。因此,如果我键入N
,我会看到任何以N
开头的数据。我做了更改,但什么都没有发生。看起来很有效。当您在搜索框中键入NO
时,该表仅显示NO
作为funding
值的数据。哦……我希望进行逐步搜索。因此,如果键入N
,我会看到任何以N
开头的数据