案例:过滤器中使用dayjs.js格式化时间

需要先引入 dayjs.js,并且在其官方文档查看使用方法

代码

<!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>品牌列表案例</title>
		<link rel="stylesheet" href="./lib/bootstrap.css">
		<link rel="stylesheet" href="./css/brandlist.css">
	</head>

	<body>

		<div id="app">
			<!-- 卡片区域 -->
			<div class="card">
				<div class="card-header">
					添加品牌
				</div>
				<div class="card-body">
					<!-- 添加品牌的表单区域 -->
					<!-- form 表单元素有 submit 事件 -->
					<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.name }}</td>
						<td>
							<div class="custom-control custom-switch">
								<!-- 使用 v-model 实现双向数据绑定 -->
								<input type="checkbox" class="custom-control-input" :id="'cb' + item.id"
									v-model="item.status">
								<!-- 使用 v-if 结合 v-else 实现按需渲染 -->
								<label class="custom-control-label" :for="'cb' + item.id" v-if="item.status">已启用</label>
								<label class="custom-control-label" :for="'cb' + item.id" v-else>已禁用</label>
							</div>
						</td>
						<td>{{ item.time | dateFormat }}</td>
						<td>
							<a href="javascript:;" @click="remove(item.id)">删除</a>
						</td>
					</tr>
				</tbody>
			</table>
		</div>

		<script src="./lib/dayjs.min.js"></script>
		<script src="./lib/vue-2.6.12.js"></script>
		<script>
			//声明格式化时间的全局过滤器
			Vue.filter('dateFormat', (time) => {
				//对 time 进行格式化处理 得到(YYYY-MM-DD HH:mm:ss)
				//return 格式化的结果
				//直接调用 dayjs() 得到的是当前时间
				//dayjs(给定的日期时间) 得到的是指定时间
				return dayjs(time).format("YYYY-MM-DD HH:mm:ss")
			})

			const vm = new Vue({
				el: '#app',
				data: {
					// 用户输入的品牌名称
					brand: '',
					// nextId 是下一个,可用的 id
					nextId: 4,
					// 品牌的列表数据
					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()
						},
					],
				},
				methods: {
					//点击链接删除对应的品牌
					remove(id) {
						// console.log(id);
						this.list = this.list.filter(item => item.id !== id)
					},
					//阻止表单的默认提交行为之后,触发add方法
					add() {
						if (this.brand === '') return alert('必须填写品牌名称!')
						//先把要添加的品牌对象 整理出来
						const obj = {
							id: this.nextId,
							name: this.brand,
							status: true,
							time: new Date()
						}
						//往 this.list 数组中 push 步骤1得到的对象
						this.list.push(obj)
						//清空 this.brand 让 this.nextId 自增 +1
						this.brand = ''
						this.nextId += 1
					}
				},
			})
		</script>
	</body>

</html>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值