user9465677
user9465677

Reputation: 427

Composing computed properties with other computed properties

I need to write a custom filtered search, sort and paginate properties since my product cannot rely any of the out of box solutions mainly because I need to display images, icons, buttons and urls in my table. Think of it as a product listing page with images and buy links.

My question is how do I chain multiple computed properties?

Example

For Filtered Search:

computed: {
    filteredProds:function() {
        return this.prodlist.filter(prod => {
            return prod.name.toLowerCase().includes(this.search.toLowerCase())
        })
    }

and for sorting the table I have this computed property along with a method to do the sort.

myprods.sort((a,b) => {
  let modifier = 1;
  if(this.currentSortDir === 'desc') modifier = -1;
  if(a[this.currentSort] < b[this.currentSort]) return -1 * modifier;
  if(a[this.currentSort] > b[this.currentSort]) return 1 * modifier;
  return 0;
});

Upvotes: 1

Views: 988

Answers (1)

Frondor
Frondor

Reputation: 3466

As simple as referencing them

data: {
  numbers: [1,2,3]
},
computed: {
  oddNumbers () {
    return this.numbers.filter(n => n % 2)
  },
  firstOddNumber () {
    return this.oddNumbers[0]
  }
}

Upvotes: 3

Related Questions