使⽤vue实现的品牌列表简单⼩例⼦直接贴代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>品牌列表添加功能</title>
<link rel="stylesheet" href="./lib/bootstrap.min.css">
<script src="./lib/vue.min.js"></script>
</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 for="">
Name:
<!-- 按键修饰符.后⾯可以跟码值 -->
<input type="text" class="form-control" v-model="name" @="add">
</label>
<input type="button" value="添加" class="btn btn-primary" @click="add">
<label for="">
搜索名称关键字:
<input type="text" class="form-control" v-model="keywords">
</label>
</div>
</div>
<table class="table table-bordered table-hover table-strip">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Ctime</th>
<th>Operation</th>
</tr>
</thead>
<tbody>
<tr v-for="item in search(keywords)" :key="item.id">
<td>{{item.id}}</td>
<td>{{item.name}}</td>
<td>{{ime | dateFormat(pattern="")}}</td>
<td><a href="#" @click="del(item.id)">删除</a></td>
</tr>
</tbody>
</table>
</div>
<div id="app2">
{{dt|dateFormat(pattern='')}}
<!-- 优先调⽤私有过滤器 -->
</div>
<script>
// ⾃定义全局按键修饰符
// 全局过滤器应该放在相关的vue实例前⾯
Vue.filter('dateFormat',function(dateStr,pattern=''){
var dt=new Date(dateStr)
var FullYear()
// padStart()是es6⽤来填充字符串的,前⼀个参数⽤来定义有字符创的长度,后⼀个⽤来定义⽤什么填充
var m=(dt.getMonth()+1).toString().padStart(2,'0');
var Date()
LowerCase0==='yyy-mm-dd'){
return `${y}-${m}-${d}`
}else{
var Hours()
var Minutes()
var Seconds()
return `${y}-${m}-${d} ${h}:${min}:${s}`
}
//return y+'-'+m+'-'+d
})
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(){
var obj={};
obj.id=this.id;
obj.name=this.name;
this.list.push(obj);
},
del(id){
if(item.id==id){
this.list.splice(i,1)
return true;
}
})
//var index=this.list.findIndex(item=>{//遍历数组,并返回满⾜条件返回true的数组项的索引
// 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;
// var newList=this.list.filter(item=>{
// if(item.name.includes(keywords)){//es6新⽅法
// return item;
// }
// })
// return newList;
}
}
})
var vm2=new Vue({
el:"#app2",
data:{
dt:new Date()
},
methods:{},
/
/ 定义私有过滤器
filters:{
dateFormat:function(dateStr,pattern=''){
var dt=new Date(dateStr)
var FullYear()
var Month()+1
var Date()
LowerCase0==='yyy-mm-dd'){
return `${y}-${m}-${d}`
}else{
var Hours()
var Minutes()
var Seconds()
return `${y}-${m}-${d} ${h}:${min}:${s}~~~~`
}
}
}
})
</script>
</body>
</html>
效果图:
发布评论