1. 程式人生 > >vue的 簡單表格例子 v-if v-show v-model

vue的 簡單表格例子 v-if v-show v-model

ger miss inpu maximum ole tex scala view 取消

技術分享圖片

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <link rel="stylesheet"
href="../bootstrap.css"> <title>Document</title> <script src="jquery.min.js"></script> <script src="../bootstrap.js"></script> <script src="../vue.min.js"></script> <script> window.onload=function(){ new Vue({ el:
"#box", data:{ mydata:[ // {name:"航三",age:"12"} ], username:"", userage:"", nowIndex:-100 }, methods:{ add:function(){
this.mydata.push({ name:this.username, age:this.userage }); this.username=""; this.userage=""; }, deleteMsg:function(n){ if(n==-2){ this.mydata=[] }else{ this.mydata.splice(n,1) } } } }); }; </script> </head> <body> <div class="container" id="box"> <form> <div class="form-group"> <label>用戶名:</label> <input type="text" class="form-control" placeholder="請輸入用戶名" v-model="username"> </div> <div class="form-group"> <label>年齡:</label> <input type="text" class="form-control" placeholder="請輸入年齡" v-model="userage"> </div> <button type="button" class="btn btn-info" v-on:click="add()">添加</button> <button type="reset" class="btn btn-danger">重置</button> <div class="table-responsive"> <table class="table-bordered table"> <tr> <th>序號</th> <th>名字</th> <th>年齡</th> <th>操作</th> </tr> <tr v-for="(x,index) in mydata"> <td>{{index+1}}</td> <td>{{x.name}}</td> <td>{{x.age}}</td> <td><button type="button" class="btn btn-info" data-toggle="modal" data-target="#myclick" v-on:click="nowIndex=index">刪除</button></td> </tr> <tr v-show="mydata.length!=0"> <td colspan="4" class="text-right"><button type="button" class="btn btn-danger btn-sm" v-on:click="nowIndex=-2" data-toggle="modal" data-target="#myclick">刪除全部</button></td> </tr> <tr v-show="mydata.length==0"> <td colspan="4" class="text-center"><p>暫無數據。。。</p></td> </tr> </table> </div> </form> <!-- 模態框 --> <div id="myclick" role="dialog" class="modal"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> <h4 class="modal-title" v-if="nowIndex!=-2">是否確定刪除</h4> <h4 class="modal-title" v-else>是否確定刪除全部</h4> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">取消</button> <button type="button" class="btn btn-primary" v-on:click="deleteMsg(nowIndex)" data-dismiss="modal">確認</button> </div> </div> </div> </div> </div> </div> </body> </html>

vue的 簡單表格例子 v-if v-show v-model