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 role='dialog'> modal  dialog为遮罩框 id用来联系触发元素

8. data-toggle='modal'   交替显示隐藏遮罩框  data-target='#layer'    确定目标模态框

9. data-dismiss='modal'  点击后消失目标元素

效果图:

vue与bootstrap实现简单用户信息添加删除功能

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta content="width=device-width,user-scalable=no,initial-scale=1.0" > <title>Document</title> <!-- 最新版本的 Bootstrap 核心 CSS 文件 --> <link href="https://cdn.bootcss.com/bootstrap/3.1.0/css/bootstrap.min.css" > <script src='https://www.jb51.net/jquery-3.2.1.min.js'></script> <script src='https://www.jb51.net/bootstrap.js'></script> <script src='https://www.jb51.net/article/vue.js'></script> <style> table td {vertical-align: middle !important;} </style> </head> <body> <div> <form action="" role='form'> <div> <label for="username">用户名:</label> <input type="text" v-model="username" placeholder="请输入用户名"> </div> <div> <label for="age">年 龄:</label> <input type="text" v-model="age" placeholder="请输入年龄"> </div> <div> <input type="button" value="添加" v-on:click='add()'> <input type="reset" value="重置"> </div> </form> <table> <caption>用户信息表</caption> <tr> <th>序号</th> <th>姓名</th> <th>年龄</th> <th>操作</th> </tr> <tr v-for='(item,index) in myData'> <td>{{index}}</td> <td>{{item.name}}</td> <td>{{item.age}}</td> <td> <button data-toggle='modal' data-target='#layer' v-on:click="currentUser=index">删除</button> </td> </tr> <tr v-show="myData.length!=0"> <td colspan="4"> <button v-on:click='currentUser="all"' data-toggle='modal' data-target="#layer">全部删除</button> </td> </tr> <tr v-show="myData.length==0"> <td colspan="4"> <p>暂无数据...</p> </td> </tr> </table> <div role='dialog'> <div> <div> <div> <button data-dismiss='modal'> <span>&times;</span> </button> <h4>确认删除吗?</h4> </div> <div> <button data-dismiss='modal'>取消</button> <button 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>

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/3c639ada8729864aaad039e1873aa2ef.html