简体   繁体   English

根据 select 选项值过滤 Vue 列表

[英]Filter Vue list based on select option value

I try to filter my list with 2 select lists based on the selected value.我尝试根据所选值使用 2 个 select 列表过滤我的列表。 It seems like my computed filter is not working?我的计算过滤器似乎不起作用?

You should be able to filter the list on 'Price from' and 'Price to'您应该能够过滤“Price from”和“Price to”列表

List.vue My computed filter property: List.vue我计算的过滤器属性:

filteredData() {
  const LowerCaseSearch = this.search.toLowerCase();
  return this.products.filter(
    product =>
      (product.name.toLowerCase().includes(LowerCaseSearch) ||
        product.category.toLowerCase().includes(LowerCaseSearch)) &&
      (!this.checked.length || this.checked.includes(product.category)) &&
      (!this.selectedFrom.length || this.selectedFrom.includes(product.price)) &&
      (!this.selectedTo.length || this.selectedTo.includes(product.price))
  );
},

In my registered component I use v-model to bind to the computed property selectedFrom在我注册的组件中,我使用v-model绑定到计算属性selectedFrom

<Price v-model="selectedFrom" />

How do I bind to the other property selectedTo in one v-model and what's wrong with my filter?如何绑定到一个 v-model 中的另一个属性selectedTo以及我的过滤器有什么问题?

I also use a prefix 'From' and 'To' to put in front of the options.我还使用前缀“From”和“To”放在选项前面。

data: () => {
    return {
      selectedFrom: '0,00',
      priceFrom: [
        { prefix: 'From', text: '0,00', value: '0,00' },
        { prefix: 'From', text: '200,00', value: '200,00' },
        { prefix: 'From', text: '400,00', value: '400,00' }
      ],
      selectedTo: 'No max',
      priceTo: [
        { prefix: 'To', text: '400,00', value: '400,00' },
        { prefix: 'To', text: '600,00', value: '600,00' },
        { prefix: 'To', text: '800,00', value: '800,00' },
        { text: 'No max', value: 'No max' }
      ]
    }
  },

Is there a more elegant and D.R.Y way to do this?有没有更优雅和 D.R.Y 的方法来做到这一点?

Here is a sandbox what I have so far.这是我到目前为止所拥有的沙盒

You should bind an object to your v-model on the <price> component.您应该将 object 绑定到<price>组件上的v-model This way you can pass multiple values to and from your component, without having to use multiple props.通过这种方式,您可以将多个值传入和传出组件,而无需使用多个道具。

I would also suggest you convert your value in your selects to numbers, so it's easier to use them to compare to your prices.我还建议您将选择中的value转换为数字,以便更容易使用它们与您的价格进行比较。

You've also defined data properties and computed properties in the sandbox ( <price> component) with the same name, this is not possible.您还在沙盒( <price>组件)中定义了同名的数据属性和计算属性,这是不可能的。 So you should remove the data properties and stick to the computed ones to handle your data.因此,您应该删除数据属性并坚持计算的属性来处理您的数据。

Fork of your sandbox with my suggested changes.使用我建议的更改分叉您的沙箱

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

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