Vue综合指令(汽车品牌管理)

在这里插入图片描述

index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <link rel="icon" href="<%= BASE_URL %>favicon.ico">
    <title><%= htmlWebpackPlugin.options.title %></title>
  </head>
  <body>
    <div id="app">
        <div class="form-group">
            <label for="id">id</label>
            <input type="text"  id="id" v-model="id" >

            <label for="name">name</label>
            <input type="text"  id="name" v-model="name">
            <button type="button" class="btn btn-primary" @click="add">添加</button>
            &nbsp; &nbsp;
            <label for="name">keywords</label>
            <input type="text"  id="keywords" v-model="keywords" >
        </div>
        <table class="table">
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Ctime</th>
                <th>Operation</th>
            </tr>
            <tr v-for=  "car in search(keywords)" :key = "car.id">
                <td>{{car.id}}</td>
                <td>{{car.name}}</td>
                <td>{{car.ctime}}</td>
                <td><a href="#" @click.prevent="del(car.id)">删除</a></td>

            </tr>
        </table>
    </div>

  </body>
</html>

main.js

import Vue from 'vue'
import Bootstrap from "bootstrap/dist/css/bootstrap.min.css"
import VueResource from 'vue-resource'
var vm =new Vue({
    el:"#app",
    data:{
        id:"",
        name:"",
        keywords:"",
        carList:[{id:"1",name:"宝马",ctime:new Date()},{id:"2",name:"奔驰",ctime: new Date()}]
    },
    methods:{

        add(){
            var car={id:this.id,name:this.name,ctime:new Date()};
            this.carList.push(car);
        },

        del(id){
            //some循环:some循环和forEach不同的是,some循环内可以return true 终止循环
            this.carList.some((element,index)=>{
                alert(element.id+"\n"+index);
                if(element.id == id){
                    //删除元素
                    this.carList.splice(index,1);
                    return true;
                }
            })
        },
        search(keywords){
            return this.carList.filter((element)=>{
                return element.name.includes(keywords);
            })
        }
    }
})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值