Fügen Sie die Anzahl (.length) jedes Elements in den Optionen von v-select hinzu
P粉197639753
P粉197639753 2024-03-31 21:05:16
0
1
529

Ich möchte die Daten konvertieren course.length 从 api 添加到 v-select 上的每个项目 option und in einem aktivitätsbasierten Filter anzeigen.

Zum Beispiel: v-select 中的项目包含从 online.passed 过滤的选项 all(count)passed(count)notcomplete(count)

Filtern nach online.complete usw.

vue.js-Vorlage:

<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>

Antwort 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
    }
],

P粉197639753
P粉197639753

Antworte allen(1)
P粉787934476

您发布的当前代码中的一些观察结果:

  • 您正在检查 online.complete === 100 但在线对象中没有 complete 属性。因此,将其更正为 completed 而不是 complete
  • online.title 表达式中缺少右大括号。

现在回到原来的问题:

v-select选项中实现计数。您必须将项目数组从 elements 数组转换为 objects 数组。

items: ['All', 'passed', 'not complete']

items: [{
        name: 'All',
        count: this.onlineCourse.length
      }, {
        name: 'passed',
        count: this.onlineCourse.filter((course) => course.passed)
      }, {
        name: 'not complete',
        count: this.onlineCourse.filter((course) => course.completed === 0)
      }]
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage