使用Bootstrap + Vue.js实现添加删除数据示例
2017-02-27 09:00
1276 查看
界面首先需要引入bootstrap的css和bootstrap的js文件,还有vue.js和jQuery.js才可以看见效果。
这里提供bootstrap的在线文件给大家引用:
<!-- 最新版本的 Bootstrap 核心 CSS 文件 --> <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="external nofollow" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- 最新的 Bootstrap 核心 JavaScript 文件 --> <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
效果如下图所示,输入用户名和年龄,点击添加,数据会自动添加到下面的用户信息表内。当没有数据时,用户信息表显示:暂无数据……,当有数据时,显示 删除全部 按钮,这里为了方便快捷,我没有做删除按钮的弹出框,所以 点击删除按钮 会直接删除掉当前这条数据。
<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="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+1}}</td> <td>{{item.name}}</td> <td>{{item.age}}</td> <td> <button class="btn btn-primary btn-sm" v-on:click="deleteMsg()">删除</button> </td> </tr> <tr v-show="myData.length!==0"> <td colspan="4" class="text-right"> <button class="btn btn-danger" v-on:click="all()">删除全部</button> </td> </tr> <tr v-show="myData.length==0"> <td colspan="4" class="text-center text-muted"> <p>暂无数据……</p> </td> </tr> </table> </div>
window.onload = function(){ new Vue({ el:"#box", data:{ myData:[], username:'', age:'', nowIndex:-100 }, methods:{ add:function(){ this.myData.push({ name:this.username, age:this.age }); this.username=''; this.age=''; }, deleteMsg:function(){ this.myData.splice(0,1) }, all:function(){ this.myData = []; } } }) }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
您可能感兴趣的文章:
相关文章推荐
- 使用Bootstrap + Vue.js实现 添加删除数据
- vue.js使用v-model指令实现的数据双向绑定功能示例
- 使用Bootstrap + Vue.js实现表格的动态展示、新增和删除功能
- Vue.js使用-组件示例(实现数据的CRUD)
- 使用js对select动态添加和删除OPTION示例代码
- 基于Bootstrap使用jQuery实现输入框组input-group的添加与删除
- 基于Bootstrap使用jQuery实现输入框组input-group的添加与删除
- 在vue.js中如何将后台管理系统中的"修改","删除"添加到每条数据的后面
- vue实现动态添加数据滚动条自动滚动到底部的示例代码
- js动态添加表格数据使用insertRow和insertCell实现
- 使用js实现对table的动态添加、删除和更新
- vue.js 利用SocketCluster实现动态添加数据及排序
- 在Vue中使用sortable.js实现拖动效果并保存拖动数据
- vue.js 使用axios实现下载功能的示例
- Js实现动态添加删除Table行示例
- vue.js使用v-if实现显示与隐藏功能示例
- yii2使用GridView实现数据全选及批量删除按钮示例
- Vue 中使用vue2-highcharts实现实时曲线数据展示示例
- 使用js对select动态添加和删除OPTION示例代码
- vue2.0使用Sortable.js实现的拖拽功能示例