【问题标题】:Vue.js Filter by dropdown if word is found in array (not exact match only)如果在数组中找到单词,则 Vue.js 按下拉列表过滤(仅不完全匹配)
【发布时间】:2021-12-14 14:26:24
【问题描述】:

https://jsfiddle.net/75f3c2po/

如何使上述 Vue.js 代码过滤器通过下拉匹配整个数组,即使有逗号分隔其他单词?现在它只匹配type: BMW,但我希望它也显示可能有类似type: BMW, Ford的卡片。

另外,我是 Vue 的超级新手,希望有人也可以告诉我如何在选择 select 选项后立即更新它,而无需点击“搜索”按钮?

非常感谢

【问题讨论】:

  • 使用on-change 选项自动更新您的过滤器。在您的情况下,它看起来像这样<select v-model="selectedType" @change="search">

标签: javascript vue.js filter


【解决方案1】:

我根据你的小提琴在下面提供了一个 sn-p。

将您的searchResults 属性更改为计算属性,以便在检测到selectedType、selectedCountry 或selectedYear 发生更改时自动更新。

要检查类型,我将首先拆分您的 csv 列表,然后检查该类型是否存在于该数组中。我已经使用item.type.split(',').map(x => x.trim()).indexOf(filterType) !== -1;

确保您了解 Vue 的基础知识并阅读Computed Properties and Watchers

Vue.config.devtools = false;
Vue.config.productionTip = false;

new Vue({
  el: '#app',
  data: {
    selectedType: '',
    selectedCountry: '',
    selectedYear: '',
    items: [{
        name: 'Nolan',
        type: 'mercedes, ford',
        year: '2020',
        country: 'england'
      },
      {
        name: 'Edgar',
        type: 'bmw',
        year: '2020',
        country: 'belgium'
      },
      {
        name: 'John',
        type: 'bmw, audi',
        year: '2019',
        country: 'england'
      },
      {
        name: 'Axel',
        type: 'mercedes',
        year: '2020',
        country: 'england'
      }
    ],
  },
  computed: {
    searchResult: function() {
      let filterType = this.selectedType,
        filterCountry = this.selectedCountry,
        filterYear = this.selectedYear

      return this.items.filter(function(item) {
        let filtered = true
        if (filterType && filterType.length > 0) {
          filtered = item.type.split(',').map(x => x.trim()).indexOf(filterType) !== -1;
        }
        if (filtered) {
          if (filterCountry && filterCountry.length > 0) {
            filtered = item.country == filterCountry
          }
        }
        if (filtered) {
          if (filterYear && filterYear.length > 0) {
            filtered = item.year == filterYear
          }
        }
        
        return filtered;
      })
    }
  }
})
.list-item {
  margin-top: 50px;
}

.card {
  box-shadow: 0px 10px 16px rgba(0, 0, 0, 0.16);
  width: 400px;
  padding: 20px 30px;
  margin-bottom: 30px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <select v-model="selectedType">
    <option value="" disabled selected hidden>Type</option>
    <option value="mercedes">Mercedes</option>
    <option value="bmw">BMW</option>
  </select>

  <select v-model="selectedCountry">
    <option value="" disabled selected hidden>Country</option>
    <option value="belgium">Belgium</option>
    <option value="england">England</option>
  </select>

  <select v-model="selectedYear">
    <option value="" disabled selected hidden>Year</option>
    <option value="2019">2019</option>
    <option value="2020">2020</option>
  </select>

  <div class="list-item" v-for="item in searchResult">
    <div class="card">
      <p>Name: {{ item.name }}</p>
      <p>Car: {{ item.type }}</p>
      <p>Year: {{ item.year }}</p>
      <p>Country: {{ item.country }}</p>
    </div>
  </div>
</div>

【讨论】:

  • 谢谢!如果它正在寻找的单词是逗号分隔列表中的第一个单词,它似乎只会过滤。有没有办法让它以任何顺序查找单词?就像BMW, Ford 和Ford, BMW?
  • @adamwstl 我添加了一个.map(x =&gt; x.trim()) 来删除数组元素中的所有空格
  • 谢谢!感谢跟进
  • @adamwstl 并非所有商品都有specialties,因此您还需要检查它是否存在,即filtered = item.specialties &amp;&amp; item.specialties.split(',').map(x =&gt; x.trim()).indexOf(filterSpecialties) !== -1;
  • 非常感谢!
【解决方案2】:

为了能够按类型查找,您可以用逗号分隔类型字符串,并检查数组是否包含该值:

      this.searchResult = this.items.filter(function(item) {
        let filtered = true
        if (filterType) {
          filtered = item.type.split(',').includes(filterType)
        }
        if (filtered) {
          if (filterCountry && filterCountry.length > 0) {
            filtered = item.country == filterCountry
          }
        }
        if (filtered) {
          if (filterYear && filterYear.length > 0) {
            filtered = item.year == filterYear
          }
        }
        return filtered
      })

为了能够立即搜索,您可以在您的选择中使用 @change 处理程序:

<select v-model="selectedType" @change="search">

【讨论】:

  • 非常感谢!
  • 它似乎只在它正在寻找的单词在数组中的第一个时才进行过滤。你能帮我得到它,让它以任何顺序查找单词吗?见这里:jsfiddle.net/9vLhkomn - 谢谢!
  • 是的,JS 是区分大小写的语言的问题,你应该在相同的情况下有你的值。例如使用.toLowerCase() 方法(filtered = item.type.split(',').includes(filterType.toLowerCase()))。此外,当您用逗号分割字符串时,您应该从数据列表中删除空格,或使用.trim() 删除不必要的空格。检查我们的jsfiddle.net/cqp29d7t/3
  • 非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-26
  • 1970-01-01
  • 1970-01-01
  • 2021-06-27
  • 2018-07-12
  • 2017-10-22
  • 1970-01-01
相关资源
最近更新 更多