javascript - Vue.js - 重置filterBy结果

标签 javascript arrays vue.js

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
      }
    ]
  }
})

我在上面的 fiddle 链接中有一个示例。我可以通过输入复选框进行排序,但取消选中过滤器不会重置为原始结果。

有谁知道取消选中过滤器按钮后如何获取完整的数组进行渲染?

最佳答案

这里的主要问题是选择器的逻辑是错误的:就目前而言,如果取消选择两个复选框,您将获得同时 coolnotCool< 的项目。它一开始工作的原因是你的代码中有一个错误(打开控制台,你会看到错误):coolnotCool 都是 undefined 在开头。

所以首先你需要概述你想要什么。完成此操作后,您可以使用函数进行过滤,而不是使用开箱即用的函数,如下所示:

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

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

目前还不是一个完美的解决方案,但却是一个很好的开始方式。检查这个:https://jsfiddle.net/72tnpa0o/5/

关于javascript - Vue.js - 重置filterBy结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38600981/

相关文章:

java - 如何为抽象类创建对象数组?

更改位数组中的值

vue.js - 如何在Vue Router中模拟 "scroll to anchor"on page load?

javascript - 如何将提及标签文本转换为 <router-link> ? (VUEJS)

javascript - 让 Gulp 合并/解析需要的 js 文件

JavaScript:在嵌套生成器中使用 yield 设置值

c# - 从列表<>中动态删除和添加另一个字母,保持字母顺序

javascript - 在 AMCharts4 上增量调整列

javascript - 从 Javascript 调用 iOS Webview 委托(delegate)未触发

javascript - 捕获并处理 vue.js 应用程序中出站链接的点击