基于vue的简易留言板 发表于 2017-05-25 基于vue的简易留言板一个基于vue的简易留言板,添加删除功能. 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <link rel="stylesheet" href="lib/bootstrap.min.css"> <script src="lib/jquery-1.7.2.js"></script> <script src="lib/bootstrap.js"></script> <script src="vue.js"></script> <script> window.onload = function() { new Vue({ el: '#box', data: { myData: [], username: '', age: '', nowIndex: -100, msg: '' }, methods: { add: function() { this.myData.push({ name: this.username, age: this.age }); this.username = ''; this.age = ''; }, deleteMsg: function(n) { if (n == -2) { this.myData = []; } else { this.myData.splice(n, 1); } }, alert1: function(msg) { var d = $("#layer"); //获取dom元素 $('#layer').modal('toggle'); this.msg = msg; } } }); }; </script></head><body> <div class="container" id="box"> <form role="form"> <div class="form-group"> <label for="username">用户名:</label> <input type="text" id="username" class="form-control" placeholder="输入用户名" v-model="username"> </div> <div class="form-group"> <label for="age">年 龄:</label> <input type="text" id="age" class="form-control" placeholder="输入年龄" v-model="age"> </div> <div class="form-group"> <input type="button" value="添加" class="btn btn-primary" v-on:click="add()"> <input type="reset" value="重置" class="btn btn-danger"> </div> </form> <hr> <table class="table table-bordered table-hover"> <caption class="h3 text-info">用户信息表</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 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" class="text-right"> <button class="btn btn-danger btn-sm" v-on:click="nowIndex=-2" data-toggle="modal" data-target="#layer">删除全部</button> </td> </tr> <tr v-show="myData.length==0"> <td colspan="4" class="text-center text-muted"> <p>暂无数据....</p> </td> </tr> </table> <button class="btn btn-primary btn-sm" v-on:click="alert1('helloWorld')">弹出-模态框</button> <!--模态框 弹出框--> <!-- 用法: 您可以切换模态框(Modal)插件的隐藏内容: 1.通过 data 属性:在控制器元素(比如按钮或者链接)上设置属性 data-toggle="modal",同时设置 data-target="#identifier" 或 href="#identifier" 来指定要切换的特定的模态框(带有 id="identifier")。 2.通过 JavaScript:使用这种技术,您可以通过简单的一行 JavaScript 来调用带有 id="identifier" 的模态框: $('#identifier').modal(options) --> <div role="dialog" class="modal fade bs-example-modal-sm my-class" 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">确认删除么?---{{msg}}</h4> </div> <div class="modal-body text-right"> <button data-dismiss="modal" class="btn btn-primary btn-sm">取消</button> <button data-dismiss="modal" class="btn btn-danger btn-sm" v-on:click="deleteMsg(nowIndex)">确认</button> </div> </div> </div> </div> </div></body></html> 坚持原创技术分享,您的支持将鼓励我继续创作! 赏 微信打赏 支付宝打赏