OLA*_*OLA 1 javascript vue.js vuejs2
我有一个使用对象数组生成的表。我很难弄清楚如何使用计算属性来过滤对象数组。我正在使用 VuE.js。我不确定如何在计算属性中正确使用 filter() 来过滤表。
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
}
}
})
Run Code Online (Sandbox Code Playgroud)
这是笔
计算属性必须返回一个值,您可以像 data 和 props 一样使用它。所以你需要做的就是返回过滤器的结果。对于没有选项的情况search,您可以返回programs不带过滤器的原始数据。
计算属性将如下所示:(如果您按其属性过滤程序funding。)
computed: {
searchContents(){
if (this.search === '') {
return this.programs
}
return this.programs.filter(x => this.search === x.funding)
}
}
Run Code Online (Sandbox Code Playgroud)
然后您可以在以下位置使用该计算属性v-for:
<tr v-for="(program, index) in searchContents">
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
708 次 |
| 最近记录: |