如何在vuetify中使用v-btn过滤产品列表?

时间:2020-02-24 22:37:24

标签: vue.js filter vuetify.js v-btn

我目前正在尝试使用按钮过滤对象列表。

我知道v-btn和v-card循环当前并不相互依赖,但是我无法弄清楚如何链接这两个部分以在按下相应的按钮时仅正确显示正确的类别。

感谢您的帮助!

        <v-row class="my-4">
          <v-btn
          v-for="category in categories"
          text
          class="underline"
          route :to="category.route"
          @click="category.function"
          >{{ category.text }}</v-btn>

        </v-row>
        <v-row 
        justify="start">
          <v-card
          v-for="product in products"
          :key="product.name"
          class="ma-3 text-center">
          <v-img
          :src="product.src"
          class="mc mt-2"
          >
          </v-img>
          <v-card-title
          class="bold justify-center">
            <h4>{{ product.name }}</h4>
          </v-card-title>
          </v-card>
        </v-row>

categories: [
                { text: 'All', function: "all()" },
                { text: 'Fruits & Veggies', function: "fruitsVeggies()"  },
                { text: 'Baking', function: "baking"  },
                { text: 'Gadgets', function: "gadgets"  },
                { text: 'Cutting Boards', function: "cuttingBoards"}],  


products: [{...}, {...}, etc]


computed: {
      all() {
        return this.products.filter(product => {
          return product
        })
      },
      fruitsVeggies() {
        return this.products.filter(product => {
          return product.category === 'Fruits & Veggies'
        })
      },
      baking() {
        return this.products.filter(product => {
          return product.category === 'Baking'
        })
      },
      cuttingBoards() {
        return this.products.filter(product => {
          return product.category === 'Cutting Boards'
        })
      }

1 个答案:

答案 0 :(得分:0)

Vue computed properties返回动态值
当您将baking作为函数调用时,它将在控制台中引发错误;它不是函数,而是属性

相反,您可以定义一个称为filteredProducts的计算属性,该属性会根据数据中存储的内容更改过滤器,然后在其上循环

data() {
  productFilter: "all"
}

computed: {
  // your computed values
  filteredProducts() {
    if (this.productFilter === "all") {
      return this.all;
    }
    // etc. For all filters
  }
}

然后在您的模板中:

<v-row 
  justify="start"
>
  <v-card
  v-for="product in filteredProducts"