在做vue项目的时候觉得有些还是不都扎实,所以现在一边从基本的练习一边做项目,这个是一个课程里面的实现留言板的一部分代码,css部分主要用了bootstrap,在此写出来,供日后观看。
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" type="text/css" href="bootstrap.min.css">
<!-- <script src="vue_dev.js"></script> -->
<script src="jquery-3.2.1.js"></script>
<script src="bootstrap.js"></script>
<script src="vue1.js"></script>
</head>
<body>
<div class="container" id="box">
<form role="form">
<div class="form-group">
<label for="username">用户名:</label>
<input type="" class="form-control" name="" id="username" placeholder="请输入用户名" v-model="username">
</div>
<div class="form-group">
<label for="age">年龄:</label>
<input type="" class="form-control" name="" id="age" placeholder="请输入年龄" v-model="age">
</div>
<div class="form-group">
<input type="button" name="" value="添加" class="btn btn-primary" v-on:click="add()">
<input type="reset" name="" value="重置" class="btn btn-danger">
</div>
</form>
<hr>
<table class="table table-border table-hover">
<caption class='h2 text-info text-center'>用户信息表</caption>
<tr class="text-danger text-center">
<th class="text-center">序号</th>
<th class="text-center">名字</th>
<th class="text-center">年龄</th>
<th class="text-center">操作</th>
</tr>
<tr class="text-center" v-for="(item,index) in mydata">
<td>{{index+1}}</td>
<td>{{item.name}}</td>
<td>{{item.age}}</td>
<td><button class="btn btn-primary btn-sm" data-toggle="modal" data-target="#layer" v-on:click="nowIndex=index">删除</button></td>
</tr>
<tr v-show="mydata.length!=0">
<td colspan="4"><button class="btn btn-danger btn-sm text-right" data-toggle="modal" data-target="#layer" v-on:click="nowIndex=-2;changename()">删除全部</button></td>
</tr>
<tr v-show="mydata.length==0">
<td colspan="4" class="text-center text-mutwd">
<p> 暂无数据...</p>
</td>
</th>
</table>
<!-- 模态窗 -->
<div role="dialog" class="modal fade " id="layer">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">
<span>×</span>
</button>
<h4 class="modal-title" id="a">确认删除吗</h4>
</div>
<div class="modal-body text-right">
<button class="btn btn-primary btn-sm" data-dismiss="modal">取消</button>
<button class="btn btn-danger btn-sm" data-dismiss="modal" v-on:click="deleteMsg(nowIndex)">确认</button>
</div>
</div>
</div>
</div>
</div>
<script>
window.οnlοad=function(){
new Vue({
el:'#box',
data:{
mydata:[],
username:'',
age:'',
nowIndex:-100,
},
methods:{
add:function(){
this.mydata.push({
name:this.username,
age:this.age
})
this.username='',
this.age=''
},
deleteMsg:function(n){
if(n==-2){
this.mydata=[]
}
this.mydata.splice(n,1);
},
changename:function(){
document.getElementById('a').innerHTML="确认删除全部吗"
}
}
})
}
</script>
</body>
</html>