zoukankan      html  css  js  c++  java
  • Vue实现添加、删除、关键字查询

    从今天开始,将不定期更新关于 Vue 的学习以及各种方法的使用,好了,下面就开始吧

    Vue的实例创建首先需要我们引入一个vue.js(也可以在本地npm安装vue,我为了省事就...),然后在HTML中定义一个 id 为 app 的 div ,这里定义的 id 是看你的个人喜好了,只要和后面我们在 script 标签内一直即可

    下面是一个简单的小例子,实现 列表的添加、删除、关键字查询

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>首页</title>
        <script src="./lib/vue.js"></script>
        <link rel="stylesheet" href="./css/home.css">
        <link rel="stylesheet" href="./css/bootstrap.css">
    </head>
    
    <body>
        <div id="app">
            <!-- 实现添加 删除 功能 -->
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title">添加人物信息</h3>
                </div>
                <div class="panel-body form-inline">
                    <label>
                        Id:
                        <input type="text" v-model="id" class="form-control">
                    </label>
                    <label>
                        Name:
                        <input type="text" v-model="name" class="form-control">
                    </label>
                    <label>
                        Age:
                        <input type="text" v-model="age" class="form-control">
                    </label>
                    <label>
                        Gender:
                        <input type="text" v-model="gender" class="form-control">
                    </label>
                    <label>
                        <input type="button" @click="add" value="添加" class="btn btn-primary">
                    </label>
                    <label>
                        关键字搜索:
                        <input type="text" v-model="keywords" placeholder="输入关键字搜索" class="form-control">
                    </label>
                </div>
            </div>
    
    
            <table class="table table-bordered table-hover">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>Name</th>
                        <th>Age</th>
                        <th>Gender</th>
                        <th>登记时间</th>
                        <th>编辑</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="item in search(keywords)" :key="item.id">
                        <td>{{ item.id }}</td>
                        <td>{{ item.name }}</td>
                        <td>{{ item.age }}</td>
                        <td>{{ item.gender }}</td>
                        <td>{{ item.ctime }}</td>
                        <td>
                            <a href="#" @click.prevent="del(item.id)">删除</a>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    
        <script src="./js/home.js"></script>
    
    </body>
    
    </html>
    var vm = new Vue({//js
      el: "#app",
      data: {
        id: "",
        name: "",
        age: "",
        gender: "",
        ctime: "",
        keywords: "",
        list: [
          { id: 1, name: "tom", age: 20, gender: "男", ctime: new Date() },
          { id: 2, name: "jam", age: 30, gender: "男", ctime: new Date() },
          { id: 3, name: "mark", age: 18, gender: "女", ctime: new Date() },
        ]
      },
      methods: {
    
        add() {//添加列表信息
          var addList = { id: this.id, name: this.name, age: this.age, gender: this.gender, ctime: this.ctime };
          this.list.push(addList);
          this.id = this.name = this.age = this.gender = this.ctime = "";
        },
        del(id) {//删除列表信息
          //第一种方法
          // some 方法返回 boolean 值,不是筛选一个新的数组,而是筛选有没有符合条件的值,只要有一个值符合条件则立即返回 true,不再执行后续操作(循环),否则返回 false
          // this.list.some((item, i) => {
          //   if (item.id == id) {
          //     this.list.splice(i, 1);
          //     return true;
          //   }
          // });
    
          //第二种方法
          var index = this.list.findIndex(item => {
            if (item.id == id) {
              return true
            }
          })
          this.list.splice(index, 1)
        },
        search(keywords) {//通过关键字搜索
          //第一种方法
          // var newList=[];
          // this.list.forEach(item =>{
          //   if(item.name.indexOf(keywords) != -1){
          //     newList.push(item)
          //   }
          // });
          // return newList;
    
    
          //第二种方法
          //filter方法是数组的过滤方法,返回一个新的数组,不会对原数组修改,return true 为想要的值,return false 则为去掉的值
          return this.list.filter(item => {
            if (item.name.includes(keywords)) {
              return item
            }
          })
        }
      }
    })
  • 相关阅读:
    内网穿透(Frp)-拯救没有公网IP的你
    用Windows远程桌面连接树莓派的方法
    TensorFlow 1.9开始支持树莓派
    树莓派制作遥控小车-新手教程
    Layui 一个页面包含多个table时不展示分页条
    MVC 通过@符号把数据赋值给jQuery对象
    jQuery 批量为表单元素赋值
    layui分页组件,一直在调用方法的解决办法
    layui 表格复选框不居中解决办法
    JQuery 解决遮罩层下内容可以滚动问题
  • 原文地址:https://www.cnblogs.com/caoxen/p/10021139.html
Copyright © 2011-2022 走看看