<!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 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()">
</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>
<tr v-for="item in list" :key="item.id">
<td>
{{item.id}}
</td>
<td v-text="item.name"></td>
<td>
{{item.Ctime}}
</td>
<td>
<!--prevent防止点击删除的时候刷新页面-->
<a href="" @click.prevent="del(item.id)"> 删除 </a>
</td>
</tr>
</tbody>
</table>
</div>
<body>
<script>
var vm=new Vue({
el:'#app',
data:{
id:'',
name:'',
list:[
{id:1,name:'张三',Ctime:new Date()},
{id:2,name:'李四',Ctime:new Date()}
]
},
methods:{
add(){
//1、获取id和name,直接从上面data中获取
//2、组织出一个对象
var people= {id:this.id,name:this.name,Ctime:new Date()}
//3、把这个对象添加到list中
this.list.push(people)
//4、v-model已经双向绑定了
},
del(id){
// //1、如何根据id找到要删除这一项的索引
// this.list.some((item,i)=>{
// if(item.id==id){
// //在数组的some方法中,如果return true,就会立即终止这个数组的后续循环
// this.list.splice(i,1)
// return true;
// }
// })
// //2、找到索引后,直接调用splice方法
var index = this.list.findIndex(item=>{
if(item.id==id){
return true;
}
})
this.list.splice(index,1)
}
}
});
</script>
</html>