我有一个数组中的用户列表.我想根据顶部的搜索框(名称)对它们进行过滤.我看到VueJS 1中有过滤器.但在VuesJS 2中不可用.如何在VueJS 2中实现这一点

<input type="text" v-model="search"/>   
<div class="row profile" v-for="customer in customers">
 <div class="col-md-offset-1 col-md-3"><img :src="customer.profile_pic" class="profile-pic" /></div>
 <div class="col-md-8"><h4 class="name">{{customer.name}}</h4></div>
</div>
<script>
    data: function () {
      return{
        search: '',
        customers: [
          { id: '1', name: 'user 1', profile_pic: 'https://i.stack.imgur.com/CE5lz.png', email:'ab@gmail.com', phone:'+91959657248', unread:'0' },
          { id: '2', name: 'user 2', profile_pic: 'https://i.stack.imgur.com/CE5lz.png', email:'abcd@gmail.com', phone:'+919456664023', unread:'0' },
          { id: '3', name: 'user 3', profile_pic: 'https://i.stack.imgur.com/CE5lz.png', email:'test@gmail.com', phone:'+919566565065', unread:'0' },
          { id: '4', name: 'user 4', profile_pic: 'https://i.stack.imgur.com/CE5lz.png', email:'sample@gmail.com', phone:'+916466004566', unread:'0' }
        ]
      }
    }
</script>

推荐答案

我通过让我的属性"array"和数据元素以及计算(computed)属性(array)和过滤元素实现了这一点.

data: function () {
      return{
        search: '',
        customers: [
          { id: '1', name: 'user 1', profile_pic: 'https://i.stack.imgur.com/CE5lz.png', email:'ab@gmail.com', phone:'+91959657248', unread:'0' },
          { id: '2', name: 'user 2', profile_pic: 'https://i.stack.imgur.com/CE5lz.png', email:'abcd@gmail.com', phone:'+919456664023', unread:'0' },
          { id: '3', name: 'user 3', profile_pic: 'https://i.stack.imgur.com/CE5lz.png', email:'test@gmail.com', phone:'+919566565065', unread:'0' },
          { id: '4', name: 'user 4', profile_pic: 'https://i.stack.imgur.com/CE5lz.png', email:'sample@gmail.com', phone:'+916466004566', unread:'0' }
        ]
},
computed:
{
    filteredCustomers:function()
    {
        var self=this;
        return this.customers.filter(function(cust){return cust.name.toLowerCase().indexOf(self.search.toLowerCase())>=0;});
    }
}

将HTML绑定到filteredCustomers.你现在应该有一个基于你在搜索中输入内容的react 式HTML."filter"方法是数组的原生JavaScript,我降低了这两个值的大小写,使其不区分大小写.

下面是fiddle中的一个工作示例:

编辑:在计算(computed)属性中添加了小提琴和代码更正

Vue.js相关问答推荐

Vue路由路径匹配行为

Vuetify/Vue3 插槽模板之间的阴影

Vue Router中.getRoutes()获取的路由顺序优化建议

Vuejs - 更新数组中对象的数组

props至少应该定义它们的类型

在 dom 内移动 Vue 组件?

在 Vue 中启用(控制台)记录路由事件

Vuex 和 Event Bus 有什么区别?

aspnet core如何在deploy上构建webpack

如何将 vuejs 项目部署到 heroku

Vue:需要禁用页面上的所有输入

未捕获的错误:[vue-composition-api] 必须在使用任何函数之前调用 Vue.use(plugin)

Polyfill for ie

在 v-for 的情况下如何从 v-model 输入更新 Vuex 存储

为什么我在 vue.js 中得到避免使用 JavaScript 一元运算符作为属性名称?

Vue.js 无法切换字体真棒图标

Vue Cli 3 不允许我在 Webpack 中处理 SVG

所有 vue props和数据都给出错误 Property属性在 type 上不存在,带有 typescript

Vuex 存储数据总是驻留在内存中?

如何在 vuejs 中的基于类的组件中编写计算设置器