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/dayjs.min.js"></script>
<script src="./lib/vue-2.6.12.js"></script>
<script>
// 使用库文件 day.js
Vue.filter("dateFormat", function(time) {
return dayjs(time).format("YYYY-MM-DD HH:mm:ss")
})
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) {
// 循环项的id不等于要删的id时,会过滤出来 放到新数组里,若相等 就被删掉了
this.list = this.list.filter(item => item.id !== id)
},
// 添加
add() {
// 如果判断brand 的值为空字符串时,则return 出去
// if(this.brand=="") return alert(""); 只有一行代码时可省略{}
if (this.brand == '') {
alert("请输入内容")
return
}
// 若没有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;
}