首頁 > web前端 > js教程 > 主體

Bootstrap + Vue.js實作新增刪除數據

高洛峰
發布: 2017-02-28 14:30:36
原創
1699 人瀏覽過

介面首先需要引入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>
登入後複製

Bootstrap + Vue.js实现添加删除数据

Bootstrap + Vue.js实现添加删除数据

# #效果如下圖所示,輸入使用者名稱和年齡,點擊添加,資料會自動添加到下面的使用者資訊表內。當沒有數據時,用戶資訊表顯示:暫無數據…,當有數據時,顯示刪除全部按鈕,這裡為了方便快捷,我沒有做刪除按鈕的彈出框,所以點擊刪除按鈕會直接刪除掉當前這條數據。



#########
 <p class="container" id="box">
    <form role="form">
      <p class="form-group">
        <label for="username">用户名:</label>
        <input type="text" id="username" class="form-control" placeholder="请输入用户名" v-model="username" />
      </p>
      <p class="form-group">
        <label for="age">年龄:</label>
        <input type="text" id="age" class="form-control" placeholder="请输入年龄" v-model="age" />
      </p>
      <p class="form-group">
        <input type="button" value="添加" class="btn btn-primary" v-on:click="add()" />
        <input type="reset" value="重置" class="btn btn-danger" />
      </p>
    </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>
  </p>
登入後複製
############
window.onload = function(){
    new Vue({
      el:"#box",
      data:{
        myData:[],
        username:&#39;&#39;,
        age:&#39;&#39;,
        nowIndex:-100
      },
      methods:{
        add:function(){
          this.myData.push({
            name:this.username,
            age:this.age
          });
          this.username=&#39;&#39;;
          this.age=&#39;&#39;;
        },
        deleteMsg:function(){
          this.myData.splice(0,1)
        },
        all:function(){
          this.myData = [];
        }
      }
    })
  }
登入後複製
#########以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持PHP中文網。 ######更多Bootstrap + Vue.js實作新增刪除資料相關文章請關注PHP中文網! ################
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!