首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在vue-filter中添加范围?

如何在vue-filter中添加范围?
EN

Stack Overflow用户
提问于 2022-07-20 05:05:07
回答 2查看 91关注 0票数 0

我有一个应用程序,使用过滤器,我想得到的产品在一个特定的价格范围内使用复选框。例如,如果我单击复选框,则产品的价格范围为100-200。

这是我的模板:

代码语言:javascript
复制
<div class="filter float-end">
      <label><input
          type="radio"
          v-model="price"
          value="All"
        /> All</label>
      <label><input
          type="radio"
          v-model="price"
          value="100"
        /> 100</label>
       <label><input
          type="radio"
          v-model="price"
          value="200"
        /> 200</label>


<div
        class="column is-12-tablet is-4-desktop is-4-widescreen"
        v-for="product in filteredRecipes"
        :key="product._id"
      >
</div>

这是我的剧本标签。如果我单击一个特定的复选框,它只显示价格,即$100或$200,我曾经单击过。

代码语言:javascript
复制
<script>
export default {
  name: "Products",
  data() {
    return {
      products: [],
      price: null,      
    };
  },
 computed: {
    ...mapGetters(["isLoggedIn"]),
    filteredRecipes() {
      let tempRecipes = this.products;
      // Filter out by price
      if (this.price === "All") return tempRecipes;
      tempRecipes = tempRecipes.filter(item => {
        return item.price >= this.price;
      });
      if (this.price)
        tempRecipes = tempRecipes.filter(item => {
          return item.price <= this.price;
        });  
      return tempRecipes;
    }
  },

  
};
</script>

如何在100到200之间的价格范围内显示产品?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-07-20 07:14:21

我猜你想要的是:

代码语言:javascript
复制
<label><input type="radio" v-model="filterPrice" value="false"> All Price</label>

<label><input type="radio" v-model="filterPrice" value="true"> Filter Price</label>
<input type="input" v-model.number="minPrice" value="100" :disabled="filterPrice!=='true'"> -
<input type="input" v-model.number="maxPrice" value="200" :disabled="filterPrice!=='true'">
代码语言:javascript
复制
filteredRecipes() {
  if (this.filterPrice !== "true") return this.products;
  return this.products.filter(item => {
    return item.price >= this.minPrice && item.price <= this.maxPrice;
  });
}
票数 2
EN

Stack Overflow用户

发布于 2022-07-20 06:46:24

解决方法之一是将收音机的值赋值为value="100to200"

代码语言:javascript
复制
<label>
  <input type="radio" v-model="price" value="100to200"/>
  100 To 200
</label>

然后我们可以绑定一个事件@change来获得过滤的产品。

现场演示

代码语言:javascript
复制
new Vue({
  el: '#app',
  data: {
    price: null,
    filteredProducts: [],
    products: [{
        id: 1,
      name: 'Product 1',
      price: 50
    }, {
        id: 2,
      name: 'Product 2',
      price: 105
    }, {
        id: 3,
      name: 'Product 3',
      price: 180
    }, {
        id: 4,
      name: 'Product 4',
      price: 210
    }]
  },
  mounted() {
    this.filteredProducts = structuredClone(this.products)
  },
  methods: {
    getProducts() {
      const priceRange = this.price.split('to');
        this.filteredProducts = this.products.filter(({ price }) => {
         return priceRange[1] ? price > Number(priceRange[0]) && price < Number(priceRange[1]) : price === Number(priceRange[0])
      })
    }
  }
})
代码语言:javascript
复制
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <label>
    <input type="radio" v-model="price" value="50" @change="getProducts"/>
    50
  </label>
  <label>
    <input type="radio" v-model="price" value="210" @change="getProducts"/>
    210
  </label>
  <label>
    <input type="radio" v-model="price" value="100to200" @change="getProducts"/>
    100 To 200
  </label>
  <br><br>
  <div v-for="product in filteredProducts" :key="product.id">
    <li>{{ product.name }}</li>
  </div>
</div>

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73046232

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档