vue与bootstrap实现简单用户信息添加删除功能
(编辑:jimmy 日期: 2024/11/29 浏览:3 次 )
本文实例为大家分享了vue与bootstrap实现用户信息添加删除操作的具体代码,供大家参考,具体内容如下
小记:
1.v-model="" 用于input表单双向数据绑定 逻辑层跟渲染层双向绑定
2.v-on:click='add()' click方法绑定
3.v-for='(item,index) in myData' 遍历数组 {{index}} {{item.name}} {{item.age}} 适用于vue版本2.0
4.v-for='(item,index,key) in myData' 遍历json {{index}} {{item}} {{key}} 适用于vue版本2.0
5.v-on:click="currentUser=index" 直接绑定点击事件改变逻辑层的数据 currentUser这里是逻辑层的数据
6.v-show="myData.length!=0" v-show根据后面的布尔值觉得显示还是隐藏 可直接用逻辑层的数据进行判断
7.<div class="modal" role='dialog' id="layer"> modal dialog为遮罩框 id用来联系触发元素
8. data-toggle='modal' 交替显示隐藏遮罩框 data-target='#layer' 确定目标模态框
9. data-dismiss='modal' 点击后消失目标元素
效果图:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=1.0" > <title>Document</title> <!-- 最新版本的 Bootstrap 核心 CSS 文件 --> <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="external nofollow" > <script src='../jquery-3.2.1.min.js'></script> <script src='bootstrap.js'></script> <script src='vue.js'></script> <style> table td {vertical-align: middle !important;} </style> </head> <body> <div class="container"> <form action="" role='form' class=""> <div class="form-group"> <label for="username" class="">用户名:</label> <input type="text" id="username" class="form-control" v-model="username" placeholder="请输入用户名"> </div> <div class="form-group"> <label for="age">年 龄:</label> <input type="text" id="age" class="form-control" v-model="age" placeholder="请输入年龄"> </div> <div class="form-group"> <input type="button" value="添加" class="btn btn-primary" v-on:click='add()'> <input type="reset" value="重置" class="btn btn-warning"> </div> </form> <table class="table table-bordered table-hover"> <caption class="h4 text-info text-center">用户信息表</caption> <tr class="text-danger"> <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}}</td> <td>{{item.name}}</td> <td>{{item.age}}</td> <td> <button class="btn btn-danger btn-xs" data-toggle='modal' data-target='#layer' v-on:click="currentUser=index">删除</button> </td> </tr> <tr v-show="myData.length!=0"> <td colspan="4" class="text-right"> <button class="btn btn-danger btn-xs" v-on:click='currentUser="all"' data-toggle='modal' data-target="#layer">全部删除</button> </td> </tr> <tr v-show="myData.length==0"> <td colspan="4" class="text-center"> <p class="text-muted">暂无数据...</p> </td> </tr> </table> <div class="modal fade bs-example-modal-sm" role='dialog' id="layer"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button class="close" data-dismiss='modal'> <span>×</span> </button> <h4 class="modal-title">确认删除吗?</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="deleteuser()">确认</button> </div> </div> </div> </div> </div> </body> </html>
<script> var c = new Vue({ el:'.container', data:{ myData:[ {name:"张三",age:20}, {name:"李四",age:20}, {name:"王五",age:20}, ], username:"", age:"", currentUser :-100, }, methods : { deleteuser :function(){ if (this.currentUser == 'all') { this.myData = []; }else{ this.myData.splice(this.currentUser,1); } }, add : function(){ if (this.username!=""&&this.age!=0) { this.myData.push({ name:this.username, age:this.age }) } }, } }) </script>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇:微信小程序实现工作时间段选择