Javascript - 如何在 Vue.js 中使用多个复选框进行过滤?

Vesko_dev

这是我用于过滤、排序和搜索的代码,一切正常

<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> 
        <div>
        <input type="checkbox" name="test" id="test" v-model="city" value='Roma' @change="uniqueCheck">
        <label for="test"> Roma</label>
        <input type="checkbox" name="test2" id="test2" v-model.trim="city" value='Barselona' @change="uniqueCheck">
        <label for="test2"> Barselona</label>
        <input type="checkbox" name="test3" id="test3" v-model.trim="city" value='Milano' @change="uniqueCheck">
        <label for="test3"> Milano</label>
        </div>
         <!-- <select v-model="city">
            <option value="Barselona">Barselona</option>
            <option value="Roma"> Roma </option>
            <option value="Milano">Milano</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.city}}
            </li>
        </ul>
</template>

<script>
export default {
 data: ()=> ( {

           
            city:[ ],
            category: '',
            name: '',
            range: '10000',
            products: [
                { name: "Keyboard", price: 44, category: 'Accessories', city:'Roma'},
                { name: "Mouse", price: 20, category: 'Accessories', city:'Barselona'},
                { name: "Monitor", price: 399, category: 'Accessories', city:'Roma'},
                { name: "Dell XPS", price: 599, category: 'Laptop', city:'Roma'},
                { name: "MacBook Pro", price: 899, category: 'Laptop', city:'Roma'},
                { name: "Pencil Box", price: 6, category: 'Stationary', city:'Barselona'},
                { name: "Pen", price: 2, category: 'Stationary', city:'Milano'},
                { name: "USB Cable", price: 7, category: 'Accessories', city:'Milano'},
                { name: "Eraser", price: 2, category: 'Stationary', city:'Roma'},
                { name: "Highlighter", price: 5, category: 'Stationary', city:'Roma'}
            ]

        }),

              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.city.indexOf(this.city))
            },

            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)
            },

            uniqueCheck(e){
            this.city = [];
            if (e.target.checked) {
                 this.city.push(e.target.value);
               }
         },

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

}
</script>

<style>

</style>

但我还想有多个复选框过滤器,例如,如果我选中了城市“巴塞罗那”和“罗马”,我想向我展示城市的巴塞罗那和罗马的产品。这段代码不起作用,因为@change 函数,我没有尝试过,但结果是当我点击第二个复选框时我什么也没得到

在此处输入图片说明

我怎么能在这里也有那个过滤器?

太阳能太阳能

您的 filterProductsByCity 方法不正确。

它应该检查产品的城市是否列在您的this.city财产中。

当您选中复选框时,它会将它们的值添加到city属性中:

例如: ['barcelona', 'montreal']

因此,您可以执行以下操作:

filterProductsByCity: function(products) {
    return products.filter(product => this.city.indexOf(product.city) !== -1)
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用Vue.js确保至少选中一个复选框

如何在PHP发出的表格行中使用JavaScript检查所有复选框

Vue.js复选框组件多个实例

Vue.JS多个复选框系列可过滤应用

单击复选框后,如何在Vue.js中使用v-on传递复选框ID?

Vue JS:如何在简单的Vue示例中将复选框与相关选择绑定?

如何在Vue js中将复选框与芯片绑定(双向绑定)

Bootstrap-Vue:如何使用带有Table组件的复选框来过滤数据?

如何在不使用vue.js禁用的情况下使始终未选中的复选框?

如何在vue.js组件中使用导入的JavaScript库构造函数?

如何在vue-good-table中的复选框表中使用按钮

如何在Vue.js中将活动类添加到多个复选框?

Vue JS-如何在JavaScript的过滤器函数中调用数据

如何使用Angular.js / Javascript在点击事件中动态选中复选框

如何根据具体值使用 Vue.js 2 选中复选框?

如何在 Vue.js cli 项目中使用外部 Javascript 库?

如何在 Vue Js 中的检查父复选框上切换嵌套复选框(选项)

如何在 JavaScript 地图函数中使用 React Native 中的复选框?

Vue.js 多复选框过滤器

如何在 JavaScript 过滤器函数中访问 Vue.js 数据变量

如何在reactjs中使用antd多个复选框?

在Vue js中如何使用通过插槽渲染的复选框来调用子组件方法

修复 vue js 复选框过滤器

如何在javascript中显示多个复选框值

Vue - 如何使用 Javascript 过滤多个值

如何防止使用 Vue.js 检查相同值的复选框?

如何在Vue中控制多个复选框

如何在反应中使用多个复选框并收集选中的复选框

如何在 onClick 按钮中使用 javascript 检查所有复选框?