Kauthon
Kauthon

Reputation: 181

How do I return only those searched records whose isActive property is true?

In an ember app and I'm trying to return records that match the search term and isActive equals true. The search part works, I just can't get the isActive true to work with the rest of it.

filterDecks(search) {
  if (search.length < 2) {
    return;
  }
  // keep it local™
  search = search.toLowerCase();

  let decks = this.store.peekAll('deck').filter(function(item) {
    let title = (item.get('title') || '').toLowerCase();
    let description = (item.get('description') || '').toLowerCase();
    let tags = (item.get('tags') || []).toString().toLowerCase();
    let isActive  = (item.get('isActive'));

    return title.includes(search) ||
        description.includes(search) ||
        tags.includes(search);
  }, this);

  this.set('filteredDecks', decks);
},

Upvotes: 1

Views: 48

Answers (2)

Ember Freak
Ember Freak

Reputation: 12872

you can include isActive condition along with searchterm.

let decks = this.store.peekAll('deck').filter((item) => {
    let title = (item.get('title') || '').toLowerCase();
    let description = (item.get('description') || '').toLowerCase();
    let tags = (item.get('tags') || []).toString().toLowerCase();
    let isActive = (item.get('isActive'));

    return ((title.includes(search) ||
        description.includes(search) ||
        tags.includes(search)) && isActive);
});

Upvotes: 2

Lux
Lux

Reputation: 18240

You can chain filters, and notice embers filterBy:

let decks = this.store.peekAll('deck').filter((item) => {
    let title = (item.get('title') || '').toLowerCase();
    let description = (item.get('description') || '').toLowerCase();
    let tags = (item.get('tags') || []).toString().toLowerCase();

    return title.includes(search) ||
        description.includes(search) ||
        tags.includes(search);
}).filterBy('isActive', true);

Upvotes: 2

Related Questions