WebStorm来实现增删

这段代码我的方法是静态引入BootStrap
增加 采用的方法是push()
删除的方法采用的是splice()通过索引来获取到数据进行删除

<script>
export default {
  name: "zengShan",
  data() {
    return {
      carList: [
        {
          id: 1,
          carName: '奔驰',
          cTime: new Date()
        }, {
          id: 2,
          carName: '奥迪',
          cTime: new Date()
        },
      ],
      carId: '',
      carName: '',
    }
  },
  methods: {
    add() {
      // console.log(this.carId)
      // console.log(this.carName)
      let obj = {
        id: this.carId,
        carName: this.carName,
        cTime: new Date()
      }
      if(this.carList.find((item)=>{
        return item.id===this.carId
      })){
        alert('id重复了')
      }else{
        this.carList.push(obj)
      }
    },
    //删除
    del(index){
      console.log(index)
      this.carList.splice(index,1)
    }
  }
}
</script>

<template>
  <div class="container">
    <div class="card">
      <div class="card-header">头部</div>
      <div class="card-body">
        <form class="form-inline">
          <label for="text">id</label>
          <input type="text" class="form-control" id="email" v-model="carId">
          <label for="pwd">name:</label>
          <input type="text" class="form-control" id="pwd" v-model="carName">
          <button type="button" class="btn btn-primary" @click="add">提交</button>
          <label for="scrch">搜索关键字:</label>
          <input type="text" class="form-control" id="serch">
        </form>
        <table class="table">
          <thead>
          <tr>
            <th>id</th>
            <th>name</th>
            <th>Ctime</th>
            <th>operation</th>
          </tr>
          </thead>
          <tbody>
          <tr v-for="(item,index) in carList" :key="item.id">
            <td>{{ item.id }}</td>
            <td>{{ item.carName }}</td>
            <td>{{ item.cTime }}</td>
            <td>
              <button class="btn btn-primary" @click="del(index)">删除</button>
            </td>
          </tr>

          </tbody>
        </table>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">

</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值