溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

VueJS實現(xiàn)用戶管理系統(tǒng)的代碼詳解

發(fā)布時間:2020-07-20 16:08:22 來源:億速云 閱讀:179 作者:小豬 欄目:web開發(fā)

這篇文章主要講解了VueJS實現(xiàn)用戶管理系統(tǒng)的代碼詳解,內(nèi)容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。

源代碼

<!doctype html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta name="viewport"
  content="width=device-width, user-scalable=no,
  initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
 <meta http-equiv="X-UA-Compatible" content="ie=edge">
 <title>用戶管理系統(tǒng)</title>
 <script src="js/jquery.js"></script>
 <script src="js/bootstrap.js"></script>
 <script src="js/vue.js"></script>
 <link rel="stylesheet" href="css/bootstrap.css" type="text/css">
 <script>
 $(function () {
  let vm = new Vue({
  el: '#app',
  data: {
   user: {},
   users: [
   {name: 'Switch', age: 25, email: 'switchvov@163.com'},
   {name: 'Kitty', age: 25, email: 'kitty@163.com'},
   ],
   nowIndex: -1, // 當前要刪除項的索引
   delIndexes: [], // 刪除項索引列表
   selectAll: false, // 刪除所有
   disableDelSelect: true, // 關閉刪除選項
   modalTarget: '',
   modalToggle: ''
  },
  methods: {
   addUser: function () {
   this.users.push(this.user);
   this.user = {};
   },
   deleteUser: function () {
   if (this.delIndexes.length > 0) {
    // 從大到小排序,不排序則會出現(xiàn)刪除錯亂
    this.delIndexes.sort(function (a, b) {
    return b - a;
    });
    for (let i = 0; i < this.delIndexes.length; i++) {
    this.users.splice(this.delIndexes[i], 1);
    }
    this.delIndexes = [];
    this.selectAll = false;
    return;
   }
   if (this.nowIndex === -1) {
    this.users = [];
    return;
   }
   this.users.splice(this.nowIndex, 1);
   },
   toggleAll: function () {
   if (this.selectAll) {
    let length = this.users.length;
    this.delIndexes = [];
    for (let i = 0; i < length; i++) {
    this.delIndexes.push(i);
    }
    return;
   }
   this.delIndexes = [];
   }
  },
  watch: {
   delIndexes: function () {
   if (this.delIndexes.length > 0) {
    this.disableDelSelect = false;
    this.modalTarget = '#del';
    this.modalToggle = 'modal';
    return;
   }
   this.disableDelSelect = true;
   }
  }
  });
 });
 </script>
</head>
<body>
<div id="app" class="container">
 <h3 class="text-center">添加用戶</h3>
 <form class="form-horizontal">
 <div class="form-group">
  <label for="name" class="control-label col-sm-2 col-sm-offset-2">姓 名:</label>
  <div class="col-sm-6">
  <input type="text" class="form-control" id="name" v-model="user.name" placeholder="請輸入姓名">
  </div>
 </div>
 <div class="form-group">
  <label for="age" class="control-label col-sm-2 col-sm-offset-2">年 齡:</label>
  <div class="col-sm-6">
  <input type="text" class="form-control" id="age" v-model="user.age" placeholder="請輸入年齡">
  </div>
 </div>
 <div class="form-group">
  <label for="email" class="control-label col-sm-2 col-sm-offset-2">郵 箱:</label>
  <div class="col-sm-6">
  <input type="text" class="form-control" id="email" v-model="user.email" placeholder="請輸入郵箱">
  </div>
 </div>
 <div class="form-group text-center">
  <input type="button" value="添 加" class="btn btn-primary" @click="addUser">
  <input type="reset" value="重 置" class="btn btn-primary">
 </div>
 </form>
 <br/>
 <table class="table table-bordered table-hover">
 <caption class="h4 text-center text-info">用戶列表</caption>
 <thead>
 <tr>
  <th class="text-center">
  <input type="checkbox" @click="toggleAll" v-model="selectAll">
  </th>
  <th class="text-center">序號</th>
  <th class="text-center">姓名</th>
  <th class="text-center">年齡</th>
  <th class="text-center">郵箱</th>
  <th class="text-center">操作</th>
 </tr>
 </thead>
 <tbody>
 <tr v-for="(user, index) in users" class="text-center">
  <td>
  <input type="checkbox" :value="index" :id="index" v-model="delIndexes" @click="selectAll = false">
  </td>
  <td>{{ index+1 }}</td>
  <td>{{ user.name }}</td>
  <td>{{ user.age }}</td>
  <td>{{ user.email }}</td>
  <td>
  <button class="btn btn-danger" data-toggle="modal" data-target="#del" @click="nowIndex = index;delIndexes=[]">
   刪除
  </button>
  </td>
 </tr>
 <tr>
  <td colspan="6" class="text-right">
  <button class="btn btn-danger" data-toggle="modal" data-target="#del" @click="nowIndex = -1;delIndexes=[]">
   刪除所有
  </button>
  <button class="btn btn-danger" :data-toggle="modalToggle" :data-target="modalTarget"
   :class="{disabled:disableDelSelect}">
   刪除選中
  </button>
  </td>
 </tr>
 </tbody>
 </table>

 <!-- 彈出框 -->
 <div class="modal" id="del">
 <div class="modal-dialog">
  <div class="modal-content">
  <div class="modal-header">
   <button class="close" data-dismiss="modal">
   <span>&times;</span>
   </button>
   <h5 class="modal-title" v-show="delIndexes.length > 0">
   確認要刪除用戶
   <span v-for="(value, index) in delIndexes">
    {{ users[value].name }}
    <span v-if="index < delIndexes.length - 1">、</span>
   </span>
   嗎?
   </h5>
   <h5 class="modal-title" v-show="delIndexes.length === 0 && nowIndex !== -1">
   確認要刪除用戶{{ users[nowIndex] &#63; users[nowIndex].name : '' }}嗎?
   </h5>
   <h5 class="modal-title" v-show="delIndexes.length === 0 && nowIndex === -1">
   確認要刪除所有用戶嗎?
   </h5>
  </div>
  <div class="modal-body text-center">
   <button class="btn btn-primary" data-dismiss="modal">取消</button>
   <button class="btn btn-primary" data-dismiss="modal" @click="deleteUser">確認</button>
  </div>
  </div>
 </div>
 </div>
</div>
</body>
</html>

看完上述內(nèi)容,是不是對VueJS實現(xiàn)用戶管理系統(tǒng)的代碼詳解有進一步的了解,如果還想學習更多內(nèi)容,歡迎關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI