简体   繁体   English

将条件语句应用于 Vuetify v-for 循环无法正常工作

[英]Applying a conditional statement to a Vuetify v-for loop not working correctly

I am using Vuetify to create two v-radio-groups where the items are comprised of filtered arrays of this.listOfCompanies.我正在使用 Vuetify 创建两个 v-radio-groups,其中项目由 this.listOfCompanies 的过滤 arrays 组成。 More specifically, an array of items from below where axis_type = 'series' and an array of items from below where axis_type = 'category'.更具体地说,来自下面的项目数组,其中axis_type = 'series',以及来自下面的项目数组,其中axis_type = 'category'。

So in summary, the v-for for one radio group should be comprised of items 'Company A' and 'Company B' since they have axis_type series and the other radio group of 'Company C'.因此,总而言之,一个无线电组的 v-for 应该由项目“公司 A”和“公司 B”组成,因为它们具有轴类型系列和“公司 C”的另一个无线电组。

this.listOfCompanies = [
  {
    "id": 352,
    "grid_id": 28,
    "header": "Company C",
    "axis_type": "category",
  },
  {
    "id": 353,
    "grid_id": 28,
    "header": "Company B",
    "axis_type": "series",
  },
  {
    "id": 354,
    "grid_id": 29,
    "header": "Company A",
    "axis_type": "series",
  }
]

For the radio groups I have the following:对于广播组,我有以下内容:

<v-radio-group v-if="this.listOfCompanies.map(a=>a.axis_type == 'category')">
    <v-radio
        v-for="category in listOfCompanies"
        :key="category"
        :label="category"
    ></v-radio>
</v-radio-group>

<v-radio-group v-if="this.listOfCompanies.map(a=>a.axis_type == 'series')">
    <v-radio
        v-for="series in listOfCompanies"
        :key="series"
        :label="series"
    ></v-radio>
</v-radio-group>

However it doesn't filter this.listOfCompanies correctly.但是它没有正确过滤 this.listOfCompanies。 What might I be doing wrong in my solution?我的解决方案可能做错了什么?

You should use computed property.您应该使用计算属性。 here is the codepen code这是codepen代码

new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data () {
    const listOfCompanies = [
  {
    "id": 352,
    "grid_id": 28,
    "header": "Company C",
    "axis_type": "category",
  },
  {
    "id": 353,
    "grid_id": 28,
    "header": "Company B",
    "axis_type": "series",
  },
  {
    "id": 354,
    "grid_id": 29,
    "header": "Company A",
    "axis_type": "series",
  }
];
    return {
      selSeries: 353,
      selCatgory:352,
      listOfCompanies
    }
  },
  computed:{
    compSeries(){
      return this.listOfCompanies.filter(item=>item.axis_type==='series')
    },
    compCategory(){
      return this.listOfCompanies.filter(item=>item.axis_type==='category')
    }
  }
})

template模板

<div id="app">
  <v-app id="inspire">
    <v-container
      class="px-0"
      fluid
    >
      <v-radio-group v-model="selSeries">
        <v-radio
          v-for="item in compSeries"
          :key="item.id"
          :label="item.header"
          :value="item.id"
        ></v-radio>
      </v-radio-group>
       <v-radio-group v-model="selCatgory">
        <v-radio
          v-for="item in compCategory"
          :key="item.id"
          :label="item.header"
          :value="item.id"
        ></v-radio>
      </v-radio-group>
    </v-container>
  </v-app>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM