这段代码我的方法是静态引入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>