1. 全局过滤器的基本使用
过滤器定义语法
Vue.filter(‘过滤器的名称’,function(){})
过滤器中的function,第一个参数已经规定死了,永远都是过滤器管道符前面传递过来的数据
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>过滤器的基本使用</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
<body>
<div id="app">
<!--可以同时调用多个过滤器-->
<p>{{msg|msgFormat('疯狂')|test}}</p>
</div>
<script>
//定义一个vue全局的过滤器,名字叫msgFormat
Vue.filter('msgFormat',function(msg,str){
//字符串的replace方法,第一个参数,除了可以写一个字符串之外,还可以定义一个正则表达式
return msg.replace(/单纯/g,str)
})
Vue.filter('test',function(msg){
return msg+'==========='
})
var vm = new Vue({
el: '#app',
data: {
msg:'曾经,我也是一个单纯的少年,单纯的我,傻傻地问,谁是世界上最单纯的男人'
},
methods:{}
});
</script>
</body>
</html>
2. 案例
效果:
代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>品牌列表案例</title>
<script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div id="app">
<div class="panel panel-primary">
<div class="panel-heading">
<h3 class="panel-title">添加品牌</h3>
</div>
<div class="panel-body form-inline">
<label>
Id:<input type="text" class="form-control" v-model="id">
</label>
<label>
Name:<input type="text" class="form-control" v-model="name">
</label>
<!--在Vue中,使用事件绑定机制,为元素指定处理函数的以后,如果加了小括号,就可以给函数传参了-->
<input type="button" value="添加" class="btn btn-primary" @click="add">
<label>
搜索名称关键字:
<input type="text" class="form-control" v-model="keywords">
</label>
</div>
</div>
<table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Ctime</th>
<th>Operation</th>
</tr>
</thead>
<tbody>
<!--v-for中的数据之前都是从data上的list中直接渲染过来的-->
<!--现在,我们定义了一个search方法,同时,把搜索的关键字通过参数,传递给该方法-->
<!--在search方法内部,通过执行for循环,把所有符合搜索关键字的数据,保存到一个新数组中返回-->
<tr v-for="item in search(keywords)" :key="item.id">
<td>{{item.id}}</td>
<td>{{item.name}}</td>
<td>{{item.ctime|dateFormat}}</td>
<td>
<a href="" @click.prevent="del(item.id)">删除</a>
</td>
</tr>
</tbody>
</table>
</div>
<script>
//全局过滤器,进行时间的格式化
Vue.filter('dateFormat',function(dateStr,pattern){
//根据给定的时间字符串,得到待定的时间
var dt=new Date(dateStr)
//yyy-mm-dd
var y=dt.getFullYear()
var m=dt.getMonth()+1
var d=dt.getDate()
if(pattern && pattern.toLowerCase()=='yyyy-mm-dd'){
return y+'-'+m+'-'+d
}else{
var h=dt.getHours()
var m=dt.getMinutes()
var s=dt.getSeconds()
return y+'-'+m+'-'+d+' '+h+':'+m+':'+s
}
})
var vm = new Vue({
el: '#app',
data: {
id:'',
name:'',
keywords:'',
list:[
{id:1,name:'奔驰',ctime:new Date()},
{id:2,name:'宝马',ctime:new Date()}
]
},
methods:{
add(){
//分析:
//1.获取到id和name,直接从data上面获取
//2.组织一个对象
//3.把这个对象,调用相关的方法,添加到当前的data上的list中
//注意:在vue中已经实现了数据的双向绑定,当修改了data中的数据,vue会监听到数据的改动,自动吧最新数据应用到页面上。
var car={id:this.id,name:this.name,ctime:new Date()}
this.list.push(car)
this.id=this.name=''
},
del(id){
//分析:
//方法一:
//1.如何根据id找到要删除对象的索引
//2.找到索引后,直接调用数组的splice方法
// this.list.some((item,i)=>{
// if(item.id==id){
// this.list.splice(i,1) //从索引为i的位置删除一个元素
// //在数组的some方法中,如果return true,就会立即终止这个数组的后续循环
// return true;
// }
// })
//方法二:
var index=this.list.findIndex(item=>{
if(item.id==id){
return true;
}
})
this.list.splice(index,1)
},
search(keywords){
//方法一:
// var newList=[]
// this.list.forEach(item => {
// if(item.name.indexOf(keywords)!=-1){
// newList.push(item)
// }
// });
// return newList
//方法二:
//forEach,some,filter,findIndex这些都是属于数组的新方法,都可以进行遍历
var newList=this.list.filter(item=>{
//注意:在es6中,为字符串提供了一个新方法,叫做string.prototype.includes('要包含的字符串')
//如果包含,则返回true,否则返回false
if(item.name.includes(keywords)){
return item
}
})
return newList
}
}
});
</script>
</body>
</html>
3. 私有过滤器
私有过滤器定义到vue的filters中。
注:过滤器调用的时候,采用的是就近原则,如果私有过滤器和全局过滤器名称一致,这时候,优先调用私有过滤器。
<div id="app2">
<h3>{{dt|dateFormat}}</h3>
</div>
<script>
//自定义一个私有过滤器
var vm2=new Vue({
el:'#app2',
data:{
dt:new Date()
},
methods:{},
filters:{ //定义私有过滤器,过滤器有两个条件(过滤器名称,处理函数)
dateFormat:function(dateStr,pattern){
//根据给定的时间字符串,得到待定的时间
var dt=new Date(dateStr)
//yyy-mm-dd
var y=dt.getFullYear()
var m=dt.getMonth()+1
var d=dt.getDate()
if(pattern && pattern.toLowerCase()=='yyyy-mm-dd'){
return y+'-'+m+'-'+d
}else{
var h=dt.getHours()
var m=dt.getMinutes()
var s=dt.getSeconds()
return y+'-'+m+'-'+d+' '+h+':'+m+':'+s+'~~~'
}
}
}
})
</script>