如何只返回isActive属性为true的搜索记录?

时间:2016-09-13 15:28:34

标签: ember.js

在一个ember应用程序中,我正在尝试返回与搜索词匹配且isActive等于true的记录。搜索部分有效,我无法让isActive真正与其余部分一起工作。

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);
},

2 个答案:

答案 0 :(得分:2)

您可以将isActive条件与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);
});

答案 1 :(得分:2)

您可以链接过滤器,并注意余烬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);