简体   繁体   English

在VueJS中使用计算属性过滤结果时,如何在数组中显示所有文章?

[英]How do I show all articles in an array when using a computed property to filter results in VueJS?

I am using VueJS to show a list of articles.我正在使用 VueJS 来显示文章列表。 Then I have some filters which use checkboxes and a computed property to filter the list of articles displayed, based on what tag is selected.然后我有一些过滤器,它们使用复选框和计算属性来过滤显示的文章列表,基于选择的标签。

However, I would like to add a 'All' tag which would clear any applied filter tags that were selected previously (Sports/Schools).但是,我想添加一个“全部”标签,该标签将清除之前选择的所有已应用过滤标签(体育/学校)。 Is this possible?这可能吗? If so, what do I need to do?如果是这样,我需要做什么? Any help welcome.欢迎任何帮助。

var app = new Vue({
  el: '#app',
  data: {
    tags: ['all', 'sports', 'schools'],
    articles: [
      {tags: ['sports'], name: 'Why sports is fun'},
      {tags: ['sports', 'schools'], name: 'How sports helps schools'},
      {tags: ['schools'], name: 'Why students attend school'}
    ],
    selectedTags: []
  },
  computed: {
    activeArticles: function() {
      if(this.selectedTags.length == 0) return this.articles;

      var activeArticles = [];
      var filters = this.selectedTags;

      this.articles.forEach(function(article) {

        function articleContainsFilter(filter) {
          return article.tags.indexOf(filter) != -1;
        }

        if(filters.every(articleContainsFilter)) {
          activeArticles.push(article);
        }
      });
      return activeArticles;
    }
  }
});

Then my HTML is as follows;那么我的HTML如下;

<div id="app">

  <label for="showAllArticles">All</label>
  <input type="checkbox" value="showAllArticles">

  <div v-for="tag in tags">
    <input type="checkbox" value="{{ tag }}" v-model="selectedTags" id="tag-{{ tag }}">
    <label for="tag-{{ tag }}">{{ tag }}</label>
  </div>

  <p v-show="selectedTags.length != 0">
    Filters: <span v-for="tag in selectedTags" class="label">{{ tag }}</span>
  </p>

  <ul>
    <li v-for="article in activeArticles">{{ article.name }}</li> 
  </ul>

</div>

Change activeArticles to return all articles if all tag selected如果选择all标签,则更改activeArticles以返回所有文章

    activeArticles: function() {
      // if statement edited
      if(this.selectedTags.length == 0 || this.selectedTags.includes('all') )return this.articles;

      var activeArticles = [];
      var filters = this.selectedTags;

      this.articles.forEach(function(article) {

        function articleContainsFilter(filter) {
          return article.tags.indexOf(filter) != -1;
        }

        if(filters.every(articleContainsFilter)) {
          activeArticles.push(article);
        }
      });
      return activeArticles;
    }

Just add只需添加

methods: {
    resetFilters(e) {
      if (e.target.checked) {
         this.selectedTags = []
      }
    }
  }

And apply onClick={resetFilters} listener to the 'All' checkbox并将 onClick={resetFilters} 侦听器应用于“全部”复选框

The computed activeArticles will update, because one of its dependents ( selectedTags ) updated.计算的activeArticles将更新,因为它的依赖项之一( selectedTags )已更新。

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

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