本文實例為大家分享了vue與bootstrap實現(xiàn)用戶信息添加刪除操作的具體代碼,供大家參考,具體內(nèi)容如下
小記:
1.v-model="" 用于input表單雙向數(shù)據(jù)綁定 邏輯層跟渲染層雙向綁定
2.v-on:click='add()' click方法綁定
3.v-for='(item,index) in myData' 遍歷數(shù)組 {{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" 直接綁定點擊事件改變邏輯層的數(shù)據(jù) currentUser這里是邏輯層的數(shù)據(jù)
6.v-show="myData.length!=0" v-show根據(jù)后面的布爾值覺得顯示還是隱藏 可直接用邏輯層的數(shù)據(jù)進行判斷
7.<div class="modal" role='dialog' id="layer"> modal dialog為遮罩框 id用來聯(lián)系觸發(fā)元素
8. data-toggle='modal' 交替顯示隱藏遮罩框 data-target='#layer' 確定目標(biāo)模態(tài)框
9. data-dismiss='modal' 點擊后消失目標(biāo)元素
效果圖:
<!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" 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">暫無數(shù)據(jù)...</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">確認(rèn)刪除嗎?</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()">確認(rèn)</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>
聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com