我有一个数组用户列表.我想根据顶部的搜索框(名称)过滤它们.我看到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>
Run Code Online (Sandbox Code Playgroud)
Nel*_*uez 25
我通过将我的属性"array"与数据元素和带有过滤元素的计算属性(数组)一起完成此操作.HTML呈现已过滤的元素.我有一个绑定到文本框的属性.为简单起见,这样的事情:
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;});
}
}
Run Code Online (Sandbox Code Playgroud)
将HTML绑定到filteredCustomers.您现在应该根据您在搜索上键入的内容来设置反应式HTML.这个"过滤器"方法是数组的原生JavaScript,我将两个值都设置为低位,使其不区分大小写.
这是一个小提琴的工作示例:https: //jsfiddle.net/dkmmhf5y/1/
编辑:在计算属性中添加小提琴和代码更正
过滤器已在vuejs 2中删除.正如@ azamat-sharapov所建议的那样,您可以使用以下3种方法之一来获得可重用的过滤器:
我怎么能在2.0中做到这一点?
- 混入
- 用方法分开模块
- 具有计算螺旋功能的独立模块
vuejs 2中过滤器的简单实现可以使用计算属性,该属性可以调用方法来获取过滤列表.在该方法中,您可以传递列表,查询,它可以返回已过滤的列表.看看下面的代码和工作演示在这里.以下是通用功能,可以移动到mixin或模块,并可以在多个组件中重复使用.
var vm = new Vue({
el: '#demo',
data: {
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' }
],
columns: {
id : {
displayname : "id",
sortorder : 1
},
name : {
displayname : "name",
sortorder : 1
},
email : {
displayname : "email",
sortorder : 1
}
},
query: '',
},
computed: {
tableFilter: function () {
return this.findBy(this.customers, this.query, 'name')
}
},
methods: {
findBy: function (list, value, column) {
return list.filter(function (item) {
return item[column].includes(value)
})
}
}
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
30977 次 |
| 最近记录: |