简体   繁体   English

Vue.js - 重置 filterBy 结果

[英]Vue.js - Resetting the filterBy results

https://jsfiddle.net/72tnpa0o/ https://jsfiddle.net/72tnpa0o/

<div id="filter-by-example">
  <label>Cool</label><input type="checkbox" v-model="cool">
  <label>notCool</label><input type="checkbox" v-model="notCool">
  <ul>
    <li v-for="user in users | filterBy cool in 'cool' | filterBy notCool in 'notCool'">
      {{ user.name }}
    </li>
  </ul>
</div>`



new Vue({
  el: '#filter-by-example',
  data: {
    name: '',
    users: [
      { 
        name: 'Bruce',
        cool: true,
        notCool: false
      },
      { 
        name: 'Chuck',
        cool: false,
                notCool: true
      },
      { 
        name: 'Jackie',
        cool: true,
        notCool: false
      }
    ]
  }
})

I have an example up at the fiddle link above.我在上面的小提琴链接上有一个例子。 I'm able to sort via the input checkboxes, but unchecking the filters doesn't reset to the original results.我可以通过输入复选框进行排序,但取消选中过滤器不会重置为原始结果。

Does anyone know how after unchecking a filter button to get the full array to render?有谁知道取消选中过滤器按钮后如何渲染整个数组?

The main issue here is that the logic of the selector is wrong: as it stands now, if you deselect both checkboxes, you will get the items that are both cool and notCool .这里的主要问题是选择器的逻辑是错误的:就目前而言,如果您取消选中两个复选框,您将获得既cool notCool酷的项目。 The reason it's working in the beginning is because there's an error in your code (open the console, you will see errors there): both cool and notCool are undefined at the beginning.它在开始工作的原因是因为你的代码中有一个错误(打开控制台,你会在那里看到错误): coolnotCool在开始时都是undefined的。

So first you need to outline what do you want.所以首先你需要概述你想要什么。 Once you've done this, you can use a function to filter instead of using the out-of-the-box one, something like this:完成此操作后,您可以使用函数进行过滤,而不是使用开箱即用的函数,如下所示:

<li v-for="user in users | filterBy filterCool">
  {{ user.name }}
</li>

And filterCool should be like: filterCool应该是这样的:

filterCool: function (element, i, list) {
  if (this.showCool && element.cool) return true;
  if (this.showNotCool && element.notCool) return true;
  if (!this.showNotCool && !this.showCool) return true;
  return false;
}

Not a perfect solution right now, but a good way to start.现在不是一个完美的解决方案,但一个很好的开始方式。 Check this: https://jsfiddle.net/72tnpa0o/5/检查这个: https ://jsfiddle.net/72tnpa0o/5/

The filter does not apply if the :false-value checkbox is empty如果 :false-value 复选框为空,则过滤器不适用

Try change this:尝试改变这个:

<input type="checkbox" v-model="cool">
<input type="checkbox" v-model="notCool">

by经过

<input type="checkbox" :false-value="" v-model="cool">
<input type="checkbox" :false-value="" v-model="notCool">

Also make sure to add an key for your list items so that vue can track each item and show relevant data ( https://v2.vuejs.org/v2/guide/list.html#key ).还要确保为您的列表项添加一个键,以便 vue 可以跟踪每个项目并显示相关数据( https://v2.vuejs.org/v2/guide/list.html#key )。

Vue 2:视图 2:

<li 
  v-for="(user, key) in users | filterBy filterCool" 
  :key="key"
>
 {{ user.name }}
</li>

Vue 1:视图 1:

<li 
  v-for="user in users | filterBy filterCool" 
  track-by="$index"
>
 {{ user.name }}
</li>

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

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