简体   繁体   English

在Vue中显示具有多个选项的过滤数组

[英]Display filtered array with multiple options in Vue

Apologies if this is answered elsewhere. 抱歉,如果在其他地方回答。 Been googling and searching on here without any luck. 被谷歌搜索并在这里搜索没有任何运气。 Closest answer I could find is here but it requires quite a bit of rework with the setup I've got. 我能找到的最接近的答案是在这里,但这需要对我已有的设置进行大量的返工。

I'm trying to filter an array based on multiple inputs of a user. 我正在尝试根据用户的多个输入来过滤数组。 My demo assumes two inputs but the actual app should approx be five. 我的演示假定有两个输入,但实际应用应约为五个。 JSFiddle 的jsfiddle

HTML HTML

<div id="app">
   <h2>Continent</h2>
   <div v-for="filter in filters.continent">
    <input type="checkbox" :value="filter" v-model="search.continent"> {{filter}}
   </div>

   <h2>Budget</h2>
   <div v-for="filter in filters.budget">
    <input type="checkbox" :value="filter" v-model="search.budget"> {{filter}}
   </div><br/>

   <p>You would like to live in {{search.continent}} and your budget is {{search.budget}}</p>

   <div v-for="country in filteredCountries">
    {{country.name}}
   </div>
</div>

JS JS

new Vue({
  el: "#app",
  data() {
    return {
        search: {
        continent: [],
        budget: []
      },

      filters: {
        continent: ['Europe', 'Asia', 'North America'],
        budget: ['Low', 'Medium', 'High'],
      },

      countries: [
        {
            name: "Sweden",
          continent: "Europe",
          budget: ["Medium", "High"],
        },
        {
            name: "Hong Kong",
          continent: "Asia",
          budget: ["Medium", "Low"],
        },
        {
            name: "Thailand",
          continent: "Asia",
          budget: ["Low"],
        },
        {
            name: "Canada",
          continent: "North America",
          budget: ["Medium", "High"],
        },

        {
            name: "US",
          continent: "North America",
          budget: ["Medium", "Low"],
        }

      ]
    }
  },

  computed: {
    filteredCountries: function(){
                return this.countries.filter((country) => 

                    this.search.budget.some(el => country.budget.includes(el)),



            )

            },
  }
})

In my actual app I'm using different components for the results and filters with an event bus that sends a payload with search data that feeds into the filtered computed property. 在我的实际应用程序中,我为事件和过滤器使用不同的组件,并通过事件总线发送事件负载,该负载将搜索数据发送到过滤后的计算属性中。

I hope someone's able to point me in the right direction with an approach that hopefully doesn't require more complexity as additional (similar array based) filter options are added. 我希望有人能够用一种方法向我指出正确的方向,因为添加了其他(基于相似数组的)过滤器选项,希望该方法不需要更多的复杂性。

Still trying to get a grasp on Javascript so apologies for the newb question! 仍在尝试掌握Javascript,因此对newb问题深表歉意!

Edit: Baboo_'s answer is really close to what I want but it seems I may have overlooked two things now that I've tried his fiddle. 编辑:Baboo_的答案确实接近我想要的答案,但是现在我尝试了他的小提琴后,似乎我可能已经忽略了两件事。 Firstly, the filters should be able to accept an array of options. 首先,过滤器应该能够接受一系列选项。 I've updated my Fiddle to show this. 我已经更新了小提琴以显示此内容。

The intended effect is that the filters are constantly updating the results like a sidebar filter. 预期的效果是,过滤器像侧边栏过滤器一样不断更新结果。 It's purely optional. 它是完全可选的。 I understand that my Fiddle doesn't assume so because everything's hidden from the get-go but my intention is to include a hidden filter that has everything checked in. Every other input is to simply just refine the results in real time. 我知道我的Fiddle并不这么认为,因为一切都从一开始就被隐藏了,但我的目的是包括一个已检查所有内容的隐藏过滤器。其他所有输入只是简单地实时优化结果。

You can filter the countries successively by budget and continent like this: 您可以按预算和大陆依次过滤国家,如下所示:

computed: {
  filteredCountries: function(){
    return this.countries
     .filter(country => this.search.budget.includes(country.budget))
     .filter(country => this.search.continent.includes(country.continent));
  },
}

Here is the fiddle . 这是小提琴

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

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