先给大家看一下效果,这个效果是网上有很多的,但是只是复制下来光看,最后是会不了的,所以还是建议大家,亲自动手打一下,不明白的地方再百度查。
下面是代码部分:
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://cdn.bootcss.com/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<title>案例1</title>
</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" @keyup.enter="add">
</label>
<input type="button" value="添加" class="btn btn-primary" @click="add">
<label>
输入搜索内容:
<input type="text" class="form-control" v-model="keywords" v-focus v-color="'#bcdff7'">
</label>
</div>
</div>
<table class="table table-bordered table-hover table-striped">s
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Ctime</th>
<th>Operation</th>
</tr>
</thead>
<tbody>
<!--在search方法中通过执行for循环,把所有符合关键字的数据,保存到一个新数组中,返回-->
<tr v-for="item in search(keywords)" :key="item.id">
<td>{{item.id}}</td>
<td v-text="item.name"></td>
<td><a>{{item.ctime | dateFormat}}</a></td>
<td><a href="#" @click.prevent="del(item.id)">删除</a></td>
</tr>
</tbody>
</table>
</div>
</body>
<script>
Vue.filter('dateFormat',function (dateStr) {
var dt = new Date(dateStr)
var y = dt.getFullYear()
var m = (dt.getMonth()+1).toString().padStart(2,'0')
var d = dt.getDate().toString().padStart(2,'0')
var hh = dt.getHours().toString().padStart(2,'0')
var mm = dt.getMinutes().toString().padStart(2,'0')
var ss = dt.getSeconds().toString().padStart(2,'0')
return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
})
Vue.directive('focus',{
inserted:function (el) {
el.focus()
}
})
Vue.directive('color',{
bind:function (el,binding) {
el.style.color = binding.value
}
})
var vm = new Vue({
el:'#app',
data:{
id:'',
name:'',
keywords:'',
list:[
{id:1,name:'奔驰',ctime:new Date()},
{id:2,name:'宝马',ctime:new Date()},
{id:3,name:'兰博基尼',ctime:new Date()},
]
},
methods:{
add(){
var car = {id:this.id,name:this.name,ctime:new Date()}
this.list.push(car)
this.id = this.name =''
},
del(id){
this.list.some((item,i)=>{
if(item.id == id){
this.list.splice(i,1)
return true;
}
})},
// 根据id得到下标
// 默认去遍历list集合,将集合中的每个元素传入到function的item里,
// var index = this.list.findIndex(item=>{
// if(item.id == id){
// return true;
// }
// })
// this.list.splice(index,1)
// forEach some filter findIndex 这些方法都会对数组中的每一项,进行遍历,执行相关操作;
search(keywords){
// var newList = []
// this.list.forEach(item=>{
// if(item.name.indexOf(keywords)!=-1){
// newList.push(item)
// }
// })
// return newList
return this.list.filter(item=> {
if(item.name.includes(keywords)){
return item
}
})
}
}
});
Vue.config.devtools = true;
</script>
</html>