vuejsdev
vuejsdev

Reputation: 25

search in data returned by computed property vue

I have computed property which returns the data from getters (vuex) which is again being used in template to show data.

computed: {
    returnlist() {
    
      return this.$store.getters[LIST]
    },
}

Now I have filters which has needs to do search within available result from computed property.

I am trying

method :{
  search() {
    this.returnlist.filter(el=>el.name==="test")

}

However this does not filter and if I console I am getting same array provided it matches with given name property.

Upvotes: 0

Views: 976

Answers (1)

geauser
geauser

Reputation: 1025

Your search method is not returning anything. Depending on how you want to use it, you could either have your search function return the filtered array:

methods: {
  search() {
    return this.returnlist.filter(el => el.name === "TEST");
  }
}

or you could have an object holding filter values in your component's data and change the filter values to adjust the returned filtered array:

data: {
  filters: {
    name: 'Jack',
    minAge: 32
    maxAge: 40,
  },
},

computed: {

  filteredArray() {
    return this.$store.getters[LIST].filter(el => {
      return el.name === this.filters.name &&
            (el.age >= this.filters.minAge &&
             el.age <= this.filters.maxAge);
    });
  }
},

Upvotes: 1

Related Questions