直接贴代码了:
<!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>Document</title>
<script src="./lib/vue.js"></script>
<link rel="stylesheet" href="./lib/bootstrap.css">
</head>
<body>
<div id="app">
<!-- //用一个div来放 文本框及添加按钮 -->
<div>
<label>
Id:
<input type="text" class="form-control" v-model="id">
</label>
<label>
Name:
<input type="text" class="form-control" v-model="name">
</label>
<input type="button" value="添加" class="btn btn-primary"@click="add()">
<label>
搜索关键字:
<input type="text" class="form-control" v-model="keywords">
</label>
<!-- //v-model实现数据双向绑定 -->
</div>
<!-- 定义一个列表 -->
<table>
<thead>
<tr>
<th width='50px'>Id </th>
<th width='50px'>Name </th>
<th width='100px'>Ctime </th>
<th width='50px'>Operation </th>
</tr>
</thead>
<tbody>
<tr v-for="item in search(keywords)" :key="item.id">
<td>{{item.id}}</td>
<td>{{item.name}}</td>
<td width='100px'>{{item.ctime}}</td>
<td>
<a href=""@click.prevent="del(item.id)">删除</a>
</td>
</tr>
</tbody>
</table>
</div>
<script>
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 car={id:this.id,name:this.name,ctime:new Date()}
this.list.push(car)
},
del(id){
this.list.some((item,i)=>{
if(item.id==id)
{
this.list.splice(i,1);
return true;
}
})
},
search(keywords){
var newList=this.list.filter(item=>{
if(item.name.includes(keywords)){
return item
}
})
return newList
// this.list.forEach(element => {
// if(element.name.indexOf(keywords)!=-1){
// newList.push(element)
// }
// return newList
// })
// this.list.filter(item=>{
// if(item.name.includes(keywords)!=-1){
// newList.push(item)
// }
// return newList
// })
},
}
})
</script>
</body>
</html>