[Vuejs]-Javascript โ€“ How to filter data with multiple arrays in Vue.js?

0๐Ÿ‘

โœ…

I found a solution, with other dummy data but works.

Here is my code

<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>

Leave a comment