简体   繁体   English

对数据库中的数据放置多个过滤器

[英]Put multiple filters on data from database

I am fetching data from database. 我正在从数据库中获取数据。

Data is structured like this: 数据的结构如下:

serie
   --- title (string)
   --- category (array)

I made search filter with computed property. 我用计算属性制作了搜索过滤器。 It looks like this: 看起来像这样:

filteredSeries () {
  return this.series.filter(serie => {
    return serie.title.toLowerCase().match(this.search.toLowerCase())
  })
}

I am looping through series like this: 我正在遍历这样的系列:

<v-flex xs12 sm4 md3 lg2 v-for="serie in filteredSeries" :key="serie.title" pa-3>

.....

</v-flex>

Search word is got from here: 搜索词是从这里得到的:

<v-text-field label="Search" height="35" v-model="search" prepend-inner-icon="search"></v-text-field>

It all works just fine, but now we are getting to my problem. 一切正常,但是现在我们解决了我的问题。 I want to filter series not just by title, but by category too. 我不仅要按标题过滤系列,而且要按类别过滤。

I get all categories from data method in array like this: 我从数组中的数据方法获取所有类别,如下所示:

data () {
  return {
    series: [],
    search: '',
    categories: [
      'Crime', 'Drama', 'Mistery', 'Comedy', 'Horror', 'Sci-Fi'
    ],
    filterCategory: []
  }
},

Filter select is filled with data like this: 过滤器选择填充有如下数据:

<v-select  prepend-inner-icon="category" height="35" v-model="filterCategory" :items="categories" chips label="Category" multiple></v-select>

This select is returning filterCategory array. 该选择返回filterCategory数组。 Now what I want is that I get series with specific categories selected in filterCategory. 现在我想要的是获得带有filterCategory中选定的特定类别的系列。 I don't know how to do that. 我不知道该怎么做。 I think that it would be perfect if filtering by category could be included in computed property where is filtering by search word done. 我认为,如果按类别过滤可以包含在按搜索字词进行过滤的计算属性中,那将是完美的。

Any help how that could be done is welcome. 欢迎提供任何帮助。

Your filter method can be modified to also filter by filteredCategories . 您的filter方法可以修改为也可以使用filteredCategories

We can use JavaScript's Array.every() and Array.includes() method to do that. 我们可以使用JavaScript的Array.every()Array.includes()方法来实现。

Also, I would use String.includes() instead of String.match() , as match() is mostly used when searching against a regular expression. 另外,我将使用String.includes()而不是String.match() ,因为match()在针对正则表达式进行搜索时最常使用。

filteredSeries () {

  // Filtering by selected categories.
  const filteredSeries = this.series.filter(serie => {
    return this.filteredCategories.every(category => {
      return serie.categories.includes(category);
    });
  });

  // Further filtering by search word.
  return filteredSeries.filter(serie => {
      return serie.title.toLowerCase().includes(this.search.toLowerCase()));
  });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM