vue 小案例
品牌列表案例
bootstrap渲染
<div id="app">
<div class="card">
<div class="card-header">
添加品牌
</div>
<div class="card-body">
<form @submit.prevent="add">
<div class="form-row align-items-center">
<div class="col-auto">
<div class="input-group mb-2">
<div class="input-group-prepend">
<div class="input-group-text">品牌名称</div>
</div>
<input type="text" class="form-control" placeholder="请输入品牌名称" v-model.trim="brand">
</div>
</div>
<div class="col-auto">
<button type="submit" class="btn btn-primary mb-2">添加</button>
</div>
</div>
</form>
</div>
</div>
<table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">品牌名称</th>
<th scope="col">状态</th>
<th scope="col">创建时间</th>
<th scope="col">操作</th>
</tr>
</thead>
<tbody>
<tr v-for="item in list" :key="item.id">
<td>{{item.id}}</td>
<td>{{item.uname}}</td>
<td>
<div class="custom-control custom-switch">
<input type="checkbox" class="custom-control-input" :id="'ch'+item.id" v-model="item.status">
<label class="custom-control-label" :for="'ch'+item.id" v-if="item.status==true">已启用</label>
<label class="custom-control-label" :for="'ch'+item.id" v-else>已禁用</label>
</div>
</td>
<td>{{item.time}}</td>
<td>
<a href="javascript:;" @click="remove(item.id)">删除</a>
</td>
</tr>
</tbody>
</table>
</div>
vue部分
<script src="./lib/vue-2.6.12.js"></script>
<script>
const vm = new Vue({
el: '#app',
data: {
brand: "",
nextid: 4,
list: [{
id: 1,
uname: "宝马",
status: true,
time: new Date()
}, {
id: 2,
uname: "奔驰",
status: false,
time: new Date()
}, {
id: 3,
uname: "奥迪",
status: true,
time: new Date()
}]
},
methods: {
remove(id) {
this.list = this.list.filter(item => item.id !== id)
},
add() {
if (this.brand == '') {
alert("请输入内容")
return
}
const obj = {
id: this.nextid,
uname: this.brand,
status: true,
time: new Date()
}
this.list.push(obj)
this.brand = ""
this.nextid++
}
}
})
</script>
css部分
body {
padding: 15px;
user-select: none;
}
|