0

Im trying to pass an id with the v-model so i can pass that to my computed function. The purpose of my function is to dynamically filter a table without hardcoding the names in the function. The function has to be able to filter both select boxes.

my data is called guides, it has an title/active/language_id/group.

this is my html dropdown:

    <select v-model="filterResult" :id="language.id" class="select2-selection custom-select">
          <option value="">All</option>
          <option v-for="language in languages" v-bind:key="language.id" v-bind:value="language.id">{{ language.name }}</option>
    </select>

    <select v-model="filterResult" :id="active" class="select2-selection custom-select">
       <option value="">All</option>
       <option :value=1>Active</option>
       <option :value=2>Archived</option>
    </select>

Here is my computed function now, but as u can see the "language.id" is hard coded in but i want the function to read it out of <select>. what i tried is putting it in the :id="" but i have no idea to forward it to the function besides the v-model. Purpose of all this is so i can reuse the component without changing anything. So it has to be applied to both select boxes or even more in the future.

    filterCertainValue: function (evt) {
        if (!this.filterResult) return this.guides;
        return this.guides.filter(guide => {
            if (guide.language.id == this.filterResult) {
                return guide.language.id;
            }
        })
    },
2
  • I dont have any errors or problems. The thing is that im inexperienced and have no clue how to get what i want. As ur demo shows u got one selectbox right now for languages, let's say that i got another selectbox below that one for "active" instead "language_id". What i want is that i can use the vue function to apply to both of them, but thats not possible right now because as u can see the function is saying return language.id. but i want the return to change upon what selectbox has been touched. Commented May 28, 2021 at 8:25
  • @tony19 codesandbox.io/s/… Commented May 28, 2021 at 8:27

1 Answer 1

1

You could create a dynamic filter key that changes based on the last selected <select>:

  1. Create a data property (named "filterKey"):

    export default {
      data() {
        return {
          filterKey: null
        }
      }
    }
    
  2. Add a change-event handler to each <select> to set filterKey to the desired key to use in the computed prop's filtering (e.g., language_id or active):

    <select @change="filterKey = 'language_id'">
      <!-- language_id options -->
    </select>
    
    <select @change="filterKey = 'active'">
      <!-- active options -->
    </select>
    
  3. Update the computed prop's filter to use the filterKey:

    computed: {
      filterCertainValue() {
        if (!this.filterResult) return this.guides
                                                    👇
        return this.guides.filter(guide => guide[this.filterKey] === this.filterResult)
      }
    },
    

demo

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.