1.品牌列表案例
将产品列表渲染
实现添加删除
//因为篇幅原因个别没有显示但不影响复制 改排版可太费经了=.= 偷懒.jpg
//该案例还有bug存在
//将所有品牌删除后重新添加 序号不会从头开始计数
//学到后面再改现在不会哈哈哈
//用.vue文件时案例会逐个解析暂时不需要直接搬过去看就能看懂
//2021/10/27 修改以下内容
用过滤器及Day.js优化显示时间
修改<td>{{item.time}}</td>
添加全局过滤器
<!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">
<title>Document</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script src="./vue.js"></script>
<script src="https://unpkg.com/dayjs@1.8.21/dayjs.min.js"></script>
<link rel="stylesheet" href="./css/css12.css">
</head>
<body>
<div id="app">
<!-- 卡片区域 -->
<div id="card">
<div class="card-header">
添加品牌
</div>
<div class="card-body">
<!-- 添加品牌的表单区域 -->
<!-- form表单元素有submit事件 用.prevent取消其默认提交后刷新网页行为 但事件仍会触发 触发add方法-->
<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">
<!-- 与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>
<th scope="col">#</th>
<th scope="col">品牌名称</th>
<th scope="col">状态</th>
<th scope="col">创建时间</th>
<th scope="col">操作</th>
</thead>
<tbody>
<tr v-for="item in list" :key="item.id">
<td>{{ item.id }}</td>
<td>{{ item.name }}</td>
<td>
<div class="custom-control custom-switch">
<!-- 使用v-model实现双向数据绑定 -->
<input type="checkbox" class="custom-control-input" v-model="item.status" id="'a'+item.id">
<!-- 使用v-if结合v-else实现按需渲染 -->
<!-- 但此时出现一个问题 虽然每一条数据都被渲染出来 但checkbox却只能控制一个 -->
<!-- 所以需要给给生成的input也给予可自动生成的id来区分每一个元素 给生成id前加字母防止数字开头报错 -->
<label :for="'a'+item.id" v-if="item.status">已启用</label>
<label :for="'a'+item.id" v-else="item.status">已禁用</label>
</div>
</td>
<td>{{item.time | dateFormat}}</td>
<td><a href="javascript:;" @click="remove(item.id)">删除</a></td>
</tr>
</tbody>
</table>
</div>
<script>
//声明格式化时间的全局过滤器
Vue.filter('dateFormat', function (time) {
//1.对time进行格式化处理,得到 YYYY-MM-DD HH:mm:ss
//把格式化的结果,return出去
//直接调用dayjs()得到的是当前时间
//dayjs(给定的日期时间)得到指定的日期
const dtStr = dayjs(time).format('YYYY-MM-DD HH:mm:ss')
return dtStr
})
const vm = new Vue({
el: "#app",
data: {
list: [
{ id: 1, name: "奔驰", status: true, time: new Date() },
{ id: 2, name: "宝马", status: false, time: new Date() },
{ id: 3, name: "奥迪", status: true, time: new Date() },
],
//用户输入的品牌名称
brand: '',
//为了添加新的品牌给予一个可用id 也就是上一个品牌+1
nextId: 4,
},
methods: {
//实现删除的功能
remove(id) {
//用this.list拿到整个列表
//利用过滤函数filter 将传入id与列表id进行对比 不等于这个id的剩余值传给左面list成为新的列表 即可实现删除
//=>箭头函数详见自己去查
this.list = this.list.filter(item => item.id !== id)
},
//实现添加
add() {
//如果判断到brand的值为空字符串则return出去
if (this.brand === '') {
alert('不可为空');
return
} else {
//1.先把需要添加的品牌对象整理出来
const once = {
id: this.nextId,
name: this.brand,
status: true,
time: new Date
}
//2.往 this.list 数组中添加用户输入的值
this.list.push(once)
//3.清空 this.brand 让 this.nextId 自增1
this.brand = ''
this.nextId++
}
}
},
})
</script>
</body>
</html>