Saya mahu menukar course.length
从 api 添加到 v-select
上的每个项目 option
dan memaparkan data kepada penapis berasaskan aktiviti.
Contohnya: v-select
中的项目包含从 online.passed
过滤的选项 all(count)
、passed(count)
、notcomplete(count)
Tapis daripada online.complete
dsb.
vue.js:
<template> <v-select v-model="filter" :items="items" ></v-select> <v-card v-for="online in onlineCourse" :key="online.id"> <v-card-title>{{online.title}</v-card-title> <p v-if="online.complete === 100">{{online.complete}}%</p> <p v-else-if="online.complete < 100 && online.complete > 0">{{online.complete}}%</p> <p v-else>{{online.complete}}%</p> </v-card> </template> <script> data () { return { onlineCourse: [], filter: 'All', items: [ 'All', 'passed', 'not complete', ], } } method: { async getDataOnline () { try { const request = await Axios.get('v1/courses') this.onlineCourse = request.courses } catch (error) { console.log(error.message); } } } </script>
Jawapan JSON:
"courses": [ { "id": 1, "title": "title1", "passed": false, "completed": 0 }, { "id": 2, "title": "title2", "passed": true, "completed": 100 }, { "id": 3, "title": "title3", "passed": false, "completed": 50 } ],
Beberapa pemerhatian daripada kod semasa yang anda siarkan:
online.complete === 100
但在线对象中没有complete
属性。因此,将其更正为completed
而不是complete
.online.title
Hilang pendakap penutup dalam ekspresi.Sekarang kembali kepada soalan asal:
Dalam
v-select
选项中实现计数。您必须将项目数组从elements
数组转换为objects
array.kepada