Home Web Front-end JS Tutorial VueJS method to implement user management system

VueJS method to implement user management system

Jul 17, 2020 pm 05:35 PM
vue User Management

VueJS method to implement user management system

The example in this article shares the specific code of VueJS to implement the user management system for your reference. The specific content is as follows

Source code

<!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>用户管理系统</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: &#39;#app&#39;,
  data: {
   user: {},
   users: [
   {name: &#39;Switch&#39;, age: 25, email: &#39;switchvov@163.com&#39;},
   {name: &#39;Kitty&#39;, age: 25, email: &#39;kitty@163.com&#39;},
   ],
   nowIndex: -1, // 当前要删除项的索引
   delIndexes: [], // 删除项索引列表
   selectAll: false, // 删除所有
   disableDelSelect: true, // 关闭删除选项
   modalTarget: &#39;&#39;,
   modalToggle: &#39;&#39;
  },
  methods: {
   addUser: function () {
   this.users.push(this.user);
   this.user = {};
   },
   deleteUser: function () {
   if (this.delIndexes.length > 0) {
    // 从大到小排序,不排序则会出现删除错乱
    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 = &#39;#del&#39;;
    this.modalToggle = &#39;modal&#39;;
    return;
   }
   this.disableDelSelect = true;
   }
  }
  });
 });
 </script>
</head>
<body>
<p id="app" class="container">
 <h2 class="text-center">添加用户</h2>
 <form class="form-horizontal">
 <p class="form-group">
  <label for="name" class="control-label col-sm-2 col-sm-offset-2">姓 名:</label>
  <p class="col-sm-6">
  <input type="text" class="form-control" id="name" v-model="user.name" placeholder="请输入姓名">
  </p>
 </p>
 <p class="form-group">
  <label for="age" class="control-label col-sm-2 col-sm-offset-2">年 龄:</label>
  <p class="col-sm-6">
  <input type="text" class="form-control" id="age" v-model="user.age" placeholder="请输入年龄">
  </p>
 </p>
 <p class="form-group">
  <label for="email" class="control-label col-sm-2 col-sm-offset-2">邮 箱:</label>
  <p class="col-sm-6">
  <input type="text" class="form-control" id="email" v-model="user.email" placeholder="请输入邮箱">
  </p>
 </p>
 <p class="form-group text-center">
  <input type="button" value="添 加" class="btn btn-primary" @click="addUser">
  <input type="reset" value="重 置" class="btn btn-primary">
 </p>
 </form>
 <br/>
 <table class="table table-bordered table-hover">
 <caption class="h3 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>

 <!-- 弹出框 -->
 <p class="modal" id="del">
 <p class="modal-dialog">
  <p class="modal-content">
  <p class="modal-header">
   <button class="close" data-dismiss="modal">
   <span>&times;</span>
   </button>
   <h4 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>
   吗?
   </h4>
   <h4 class="modal-title" v-show="delIndexes.length === 0 && nowIndex !== -1">
   确认要删除用户{{ users[nowIndex] ? users[nowIndex].name : &#39;&#39; }}吗?
   </h4>
   <h4 class="modal-title" v-show="delIndexes.length === 0 && nowIndex === -1">
   确认要删除所有用户吗?
   </h4>
  </p>
  <p 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>
  </p>
  </p>
 </p>
 </p>
</p>
</body>
</html>
Copy after login

GitHub :vue-user-manager

Related learning recommendations: javascript video tutorial

The above is the detailed content of VueJS method to implement user management system. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to add functions to buttons for vue How to add functions to buttons for vue Apr 08, 2025 am 08:51 AM

You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

How to reference js file with vue.js How to reference js file with vue.js Apr 07, 2025 pm 11:27 PM

There are three ways to refer to JS files in Vue.js: directly specify the path using the &lt;script&gt; tag;; dynamic import using the mounted() lifecycle hook; and importing through the Vuex state management library.

How to use watch in vue How to use watch in vue Apr 07, 2025 pm 11:36 PM

The watch option in Vue.js allows developers to listen for changes in specific data. When the data changes, watch triggers a callback function to perform update views or other tasks. Its configuration options include immediate, which specifies whether to execute a callback immediately, and deep, which specifies whether to recursively listen to changes to objects or arrays.

How to return to previous page by vue How to return to previous page by vue Apr 07, 2025 pm 11:30 PM

Vue.js has four methods to return to the previous page: $router.go(-1)$router.back() uses &lt;router-link to=&quot;/&quot; component window.history.back(), and the method selection depends on the scene.

What does vue multi-page development mean? What does vue multi-page development mean? Apr 07, 2025 pm 11:57 PM

Vue multi-page development is a way to build applications using the Vue.js framework, where the application is divided into separate pages: Code Maintenance: Splitting the application into multiple pages can make the code easier to manage and maintain. Modularity: Each page can be used as a separate module for easy reuse and replacement. Simple routing: Navigation between pages can be managed through simple routing configuration. SEO Optimization: Each page has its own URL, which helps SEO.

How to use function intercept vue How to use function intercept vue Apr 08, 2025 am 06:51 AM

Function interception in Vue is a technique used to limit the number of times a function is called within a specified time period and prevent performance problems. The implementation method is: import the lodash library: import { debounce } from 'lodash'; Use the debounce function to create an intercept function: const debouncedFunction = debounce(() =&gt; { / Logical / }, 500); Call the intercept function, and the control function is called at most once in 500 milliseconds.

How to jump to the div of vue How to jump to the div of vue Apr 08, 2025 am 09:18 AM

There are two ways to jump div elements in Vue: use Vue Router and add router-link component. Add the @click event listener and call this.$router.push() method to jump.

See all articles