【问题标题】:Javascript - How to filter data with multiple arrays in Vue.js?Javascript - 如何在 Vue.js 中使用多个数组过滤数据?
【发布时间】:2021-07-05 10:28:12
【问题描述】:

我正在使用 Vue.js 3,并且在这种情况下,我有一个用于一个数组的过滤器类型,但是如何与另一个数组连接,例如这里的一个类别?我尝试了同样的方法,但我只得到了重复

https://stackoverflow.com/users/3330981/tauzn 你可以检查一下,如果你 有时间吗?

这是我的代码

<template>
<div>
   <form>
      <input type="checkbox" name="drama" v-model="filters.drama"/>
      <label for="drama">drama</label>
      <input type="checkbox" name="comedy" v-model="filters.comedy"/>
      <label for="comedy">comedy</label>
      <input type="checkbox" name="horror" v-model="filters.horror"/>
      <label for="horror">horror</label>
      <input type="checkbox" name="polovno" v-model="filters.new"/>
      <label for="new">new</label>
   </form>
   <div v-for="(movie) in filteredMovies" :key="movie.title">
      <div>
        {{ movie.title }}
      </div>
    </div>
</div>
</template>

<script>
  export default {
    data: ()=>({
      filters: {
        horror: true,
        comedy: true,
        drama: true,
        new:true,
        old:true,
      },
      movies: [
        { title: 'Movia A', genres: ['horror'], category:['new']},
        { title: 'Movia B', genres: ['comedy'], category:['old']},
        { title: 'Movia C', genres: ['drama'], category:['new']},
        { title: 'Movia D',genres: ['horror'], category:['new']},
      ]
    }),
    computed: {
      filteredMovies(){
        return this.movies.filter( movie => {
          let selectedGenres = Object.keys(this.filters).filter(item => this.filters[item] === true)
    
                    return movie.genres.some(item => selectedGenres.includes(item))
        })
      },
      filteredMoviesByCategory(){
        return this.movies.filter( movie => {

          let selectedCategory = Object.keys(this.filters).filter(item => this.filters[item] === true)
          
           return movie.category.some(item => selectedCategory.includes(item))
        })
      }
    },


  }
</script>

所以我有复选框,这对于流派过滤非常有效,但是在这种情况下如何将其连接到另一个数组?我是 Vue.js 的新手,这对我有很大帮助

这是现在一切运作方式的 Gif

【问题讨论】:

  • 是您选择以这种方式制作新/旧数据,还是来自该格式。如果一个类别只能有两个值的一个交替,它应该是布尔值,如 isNew true/false。将其设为布尔值将使事情变得更容易,尤其是当您的过滤器变大时。如果 category 可以有任何其他值,请添加更多。
  • 您想显示只匹配类型和类别的电影吗?那么,恐怖+旧会什么都不会给?恐怖+喜剧+老电影B?
  • @MattEllen 是的,这正是我想要的

标签: javascript vue.js filtering


【解决方案1】:

我找到了一个解决方案,使用其他虚拟数据但有效。

这是我的代码

<template>
  <h5>List of Products</h5>

        <h3>Filter</h3> 
        <button v-on:click="resetOptions">Reset</button>
        <button v-on:click="sorting">Sorting</button>
        <button v-on:click="sorting2">Sorting2</button>
        <select v-model="category">
            <option value="Accessories">Accessories</option>
            <option value="Laptop">Laptop</option>
            <option value="Stationary">Stationary</option>
        </select> 
         <select v-model="gradovi">
            <option value="Podgorica">Podgorica</option>
            <option value="Niksic">Niksic</option>
            <option value="Herceg Novi">Herceg Novi</option>
        </select> 

        <input type="text" v-model="name" placeholder="Filter By Name"/>

        <label for="vol">Price (between 0 and 1000):</label>

        <input type="range" v-model.trim="range" min="0" max="1000" step="10"/>  
        <ul>
            <li v-for="product in filterProducts" :key="product.name"> Product Name : {{product.name}} - Price : {{product.price}} ({{product.category}}) 
                {{product.gradovi}}
            </li>
        </ul>
</template>

<script>
export default {
 data: ()=> ( {
            gradovi:'',
            category: '',
            name: '',
            range: '0',
            products: [
                { name: "Keyboard", price: 44, category: 'Accessories', gradovi:'Podgorica'},
                { name: "Mouse", price: 20, category: 'Accessories', gradovi:'Niksic'},
                { name: "Monitor", price: 399, category: 'Accessories', gradovi:'Herceg Novi'},
                { name: "Dell XPS", price: 599, category: 'Laptop', gradovi:'Podgorica'},
                { name: "MacBook Pro", price: 899, category: 'Laptop', gradovi:'Podgorica'},
                { name: "Pencil Box", price: 6, category: 'Stationary', gradovi:'Niksic'},
                { name: "Pen", price: 2, category: 'Stationary', gradovi:'Niksic'},
                { name: "USB Cable", price: 7, category: 'Accessories', gradovi:'Herceg Novi'},
                { name: "Eraser", price: 2, category: 'Stationary', gradovi:'Podgorica'},
                { name: "Highlighter", price: 5, category: 'Stationary', gradovi:'Niksic'}
            ]
        }),

              computed: {
            filterProducts: function(){
                return this.filterProductsByName(this.filterProductsByRange(this.filterProductsByCity(this.filterProductsByCategory(this.products))))
            },
        },
        
        methods: {

            filterProductsByCategory: function(products){
                return products.filter(product => !product.category.indexOf(this.category))
            },

            filterProductsByName: function(products) {
                return products.filter(product => !product.name.toLowerCase().indexOf(this.name.toLowerCase()))
            },

            filterProductsByCity: function(products) {
                return products.filter(product => !product.gradovi.indexOf(this.gradovi))
            },

            filterProductsByRange: function(products){
                return products.filter(product => (product.price >= 0 && product.price <= this.range) ? product : '')
            },

            sorting:function(){
                this.products.sort((a,b)=>(a.price > b.price) ? 1 : -1)
            },
             sorting2:function(){
                this.products.sort((a,b)=>(a.price < b.price) ? 1 : -1)
            },

            resetOptions:function(){
                this.category='',
                this.gradovi='',
                this.name='',
                this.range='1000'
            },
        },

}
</script>

<style>

</style>

【讨论】:

    猜你喜欢
    • 2020-07-01
    • 2021-07-08
    • 1970-01-01
    • 2021-09-07
    • 1970-01-01
    • 2021-08-25
    • 1970-01-01
    • 2017-10-02
    • 1970-01-01
    相关资源
    最近更新 更多