当前位置: 首页 > news >正文

VUE篇之日历组件

1.简单日历组件展示

思路:根据当前月的第一天是星期几,来显示日期

<template><div class="wrap"><el-button @click="preMonth">上个月</el-button><el-tag>当前年份{{ curYear }}</el-tag><el-tag>当前月份{{ curMonth }}</el-tag><el-button @click="nextMonth">下个月</el-button><div class="weeks"><div v-for="item in week" :key="item" class="week">{{ item }}</div></div><div class="days"><!-- 当月 --><div v-for="item in curDays" :key="item + 'cur'" class="curday">{{ item }}</div></div></div>
</template>
<script>
import moment from 'moment';
moment.suppressDeprecationWarnings = true;
export default {data() {return {curYear: moment().year(), //当前年curMonth: moment().month() + 1, //当前月week: ['一', '二', '三', '四', '五', '六', '七'],firstDay: moment(`${moment().year()}-${moment().month() + 1}`).startOf('month').day(), //获取当月第一天是星期几;星期日为 0,星期六为 6curDays: moment().daysInMonth() //获取当月一共有多少天};},methods: {preMonth() {this.curMonth--;// 如果小于1表示上一年;重置日期if (this.curMonth < 1) {this.curYear--;this.curMonth = 12;}this.curDays = moment(`${this.curYear}-${this.curMonth}`).daysInMonth();this.firstDay = moment(`${this.curYear}-${this.curMonth}`).startOf('month').day();if (this.firstDay == 0) {this.firstDay = 7;}},nextMonth() {this.curMonth++;// 如果超过12表示下一年;重置日期if (this.curMonth > 12) {this.curYear++;this.curMonth = 1;}this.curDays = moment(`${this.curYear}-${this.curMonth}`).daysInMonth();this.firstDay = moment(`${this.curYear}-${this.curMonth}`).startOf('month').day();if (this.firstDay == 0) {this.firstDay = 7;}}}
};
</script><style lang="scss">
.wrap {width: 700px;height: 100%;.weeks {width: 100%;height: 50px;display: flex;.week {width: 100px;line-height: 50px;text-align: center;background: gainsboro;}}.days {display: flex;flex-wrap: wrap;.lastday,.curday {width: 100px;line-height: 50px;text-align: center;}.lastday {color: gold;}}
}
</style>

2.日历组件增强版------带有上个月或者下个月日期

较比上一版本,这个版本多了2个方法,主要用于更新上个月剩余日期,以及下个月最新日期

上个月日期:
// 获取上个月剩余天数getPreMonthDays() {if (this.firstDay == 1) return; //表示上个月无剩余天数let month = this.curMonth;let year = this.curYear;month--;if (month == 0) {year--;month = 12;}// 获取上个月的天数const days = moment(`${year}-${month}`).daysInMonth();this.preDays = days;},
 下个月日期:
// 获取下个月要显示的天数getNextMonthDays() {let month = this.curMonth;let year = this.curYear;// 获取当月最后一天是星期几const lastDay = moment(`${year}-${month}`).endOf('month').day();this.nextDays = lastDay == 0 ? 7 : lastDay;}

 整体代码:

<template><div class="wrap"><el-button @click="preMonth">上个月</el-button><el-tag>当前年份{{ curYear }}</el-tag><el-tag>当前月份{{ curMonth }}</el-tag><el-button @click="nextMonth">下个月</el-button><div class="weeks"><div v-for="item in week" :key="item" class="week">{{ item }}</div></div><div class="days"><!-- 上个月 --><div v-for="item in firstDay - 1" :key="item + 'pre'" class="lastday">{{ preDays - (firstDay - 1 - item) }}</div><!-- 当月 --><div v-for="item in curDays" :key="item + 'cur'" class="curday">{{ item }}</div><!-- 下个月 --><div v-for="item in 7 - nextDays" :key="item + 'next'" class="lastday">{{ item }}</div></div></div>
</template>
<script>
import moment from 'moment';
moment.suppressDeprecationWarnings = true;
export default {data() {return {preDays: 30,nextDays: 7,curYear: moment().year(), //当前年curMonth: moment().month() + 1, //当前月week: ['一', '二', '三', '四', '五', '六', '七'],firstDay: moment(`${moment().year()}-${moment().month() + 1}`).startOf('month').day(), //获取当月第一天是星期几;星期日为 0,星期六为 6curDays: moment().daysInMonth() //获取当月一共有多少天};},mounted() {this.getPreMonthDays();this.getNextMonthDays();},methods: {preMonth() {this.curMonth--;// 如果小于1表示上一年;重置日期if (this.curMonth < 1) {this.curYear--;this.curMonth = 12;}this.curDays = moment(`${this.curYear}-${this.curMonth}`).daysInMonth();this.firstDay = moment(`${this.curYear}-${this.curMonth}`).startOf('month').day();if (this.firstDay == 0) {this.firstDay = 7;}// 显示上个月日期this.getPreMonthDays();this.getNextMonthDays();},nextMonth() {this.curMonth++;// 如果超过12表示下一年;重置日期if (this.curMonth > 12) {this.curYear++;this.curMonth = 1;}this.curDays = moment(`${this.curYear}-${this.curMonth}`).daysInMonth();this.firstDay = moment(`${this.curYear}-${this.curMonth}`).startOf('month').day();if (this.firstDay == 0) {this.firstDay = 7;}// 显示上个月日期this.getPreMonthDays();this.getNextMonthDays();},// 获取上个月剩余天数getPreMonthDays() {if (this.firstDay == 1) return; //表示上个月无剩余天数let month = this.curMonth;let year = this.curYear;month--;if (month == 0) {year--;month = 12;}// 获取上个月的天数const days = moment(`${year}-${month}`).daysInMonth();this.preDays = days;},// 获取下个月要显示的天数getNextMonthDays() {let month = this.curMonth;let year = this.curYear;// 获取当月最后一天是星期几const lastDay = moment(`${year}-${month}`).endOf('month').day();this.nextDays = lastDay == 0 ? 7 : lastDay;}}
};
</script><style lang="scss">
.wrap {width: 700px;height: 100%;.weeks {width: 100%;height: 50px;display: flex;.week {width: 100px;line-height: 50px;text-align: center;background: gainsboro;}}.days {display: flex;flex-wrap: wrap;.lastday,.curday {width: 100px;line-height: 50px;text-align: center;}.lastday {color: gold;}}
}
</style>

3.日历组件增强版------可选择区间日期

思路:通过clickCount记录点击区间次数,默认是0,点击第一次是1,第二次是2;如果clickCount==2重置clickCount=0

页面渲染:通过判断日期是否在选择区间的最大和最小之间,来更改背景色

相比较之前,更新的代码:

方法新增一个点击事件

selectDate(year, day) {this.clickCount++;const date = new Date(`${year}-${this.curMonth}-${day}`);if (this.clickCount == 1) {this.startTime = date;} else if (this.clickCount == 2) {this.endTime = date;this.clickCount = 0;}if (this.endTime && +this.startTime > +this.endTime) {[this.startTime, this.endTime] = [this.endTime, this.startTime];}// console.log(//   this.clickCount,//   moment(this.startTime).format('YYYY-MM-DD'),//   moment(this.endTime).format('YYYY-MM-DD')// );}
  computed: {isSelected() {return (year, day) => {const date = new Date(`${year}-${this.curMonth}-${day}`);return ((+this.startTime <= +date && +this.endTime >= +date) || +date == +this.startTime || +date == +this.endTime);};}},

整体代码:

<template><div class="wrap"><el-button @click="preMonth">上个月</el-button><el-tag>当前年份{{ curYear }}</el-tag><el-tag>当前月份{{ curMonth }}</el-tag><el-button @click="nextMonth">下个月</el-button><div class="weeks"><div v-for="item in week" :key="item" class="week">{{ item }}</div></div><div class="days"><!-- 上个月 --><divv-for="item in firstDay - 1":key="item + 'pre'":class="['lastday', { select: isSelected(curYear - 1, preDays - (firstDay - 1 - item)) }]"@click="selectDate(curYear - 1, preDays - (firstDay - 1 - item))">{{ preDays - (firstDay - 1 - item) }}</div><!-- 当月 --><divv-for="item in curDays":key="item + 'cur'":class="['curday', { select: isSelected(curYear, item) }]"@click="selectDate(curYear, item)">{{ item }}</div><!-- 下个月 --><divv-for="item in 7 - nextDays":key="item + 'next'":class="['lastday', { select: isSelected(curYear + 1, item) }]"@click="selectDate(curYear + 1, item)">{{ item }}</div></div></div>
</template>
<script>
import moment from 'moment';
moment.suppressDeprecationWarnings = true;
export default {data() {return {startTime: null, //记录区间开始时间endTime: null, //记录区间结束时间clickCount: 0, //用于记录点击次数preDays: 30, //上个月天数nextDays: 7, //下个月天数curYear: moment().year(), //当前年curMonth: moment().month() + 1, //当前月week: ['一', '二', '三', '四', '五', '六', '七'],firstDay: moment(`${moment().year()}-${moment().month() + 1}`).startOf('month').day(), //获取当月第一天是星期几;星期日为 0,星期六为 6curDays: moment().daysInMonth() //获取当月一共有多少天};},computed: {isSelected() {return (year, day) => {const date = new Date(`${year}-${this.curMonth}-${day}`);return ((+this.startTime <= +date && +this.endTime >= +date) || +date == +this.startTime || +date == +this.endTime);};}},mounted() {this.getPreMonthDays();this.getNextMonthDays();},methods: {preMonth() {this.curMonth--;// 如果小于1表示上一年;重置日期if (this.curMonth < 1) {this.curYear--;this.curMonth = 12;}this.curDays = moment(`${this.curYear}-${this.curMonth}`).daysInMonth();this.firstDay = moment(`${this.curYear}-${this.curMonth}`).startOf('month').day();if (this.firstDay == 0) {this.firstDay = 7;}// 显示上个月日期this.getPreMonthDays();this.getNextMonthDays();},nextMonth() {this.curMonth++;// 如果超过12表示下一年;重置日期if (this.curMonth > 12) {this.curYear++;this.curMonth = 1;}this.curDays = moment(`${this.curYear}-${this.curMonth}`).daysInMonth();this.firstDay = moment(`${this.curYear}-${this.curMonth}`).startOf('month').day();if (this.firstDay == 0) {this.firstDay = 7;}// 显示上个月日期this.getPreMonthDays();this.getNextMonthDays();},// 获取上个月剩余天数getPreMonthDays() {if (this.firstDay == 1) return; //表示上个月无剩余天数let month = this.curMonth;let year = this.curYear;month--;if (month == 0) {year--;month = 12;}// 获取上个月的天数const days = moment(`${year}-${month}`).daysInMonth();this.preDays = days;},// 获取下个月要显示的天数getNextMonthDays() {let month = this.curMonth;let year = this.curYear;// 获取当月最后一天是星期几const lastDay = moment(`${year}-${month}`).endOf('month').day();this.nextDays = lastDay == 0 ? 7 : lastDay;},selectDate(year, day) {this.clickCount++;const date = new Date(`${year}-${this.curMonth}-${day}`);if (this.clickCount == 1) {this.startTime = date;} else if (this.clickCount == 2) {this.endTime = date;this.clickCount = 0;}if (this.endTime && +this.startTime > +this.endTime) {[this.startTime, this.endTime] = [this.endTime, this.startTime];}// console.log(//   this.clickCount,//   moment(this.startTime).format('YYYY-MM-DD'),//   moment(this.endTime).format('YYYY-MM-DD')// );}}
};
</script><style lang="scss">
.wrap {width: 700px;height: 100%;.weeks {width: 100%;height: 50px;display: flex;.week {width: 100px;line-height: 50px;text-align: center;background: gainsboro;}}.days {display: flex;flex-wrap: wrap;.lastday,.curday {width: 100px;line-height: 50px;text-align: center;cursor: pointer;}.lastday {color: gold;}.select {background: pink;color: #fff;}}
}
</style>

4.日历组件增强版------可自定义日期内容

未完待续

相关文章:

  • 基于Springboot的教学信息反馈系统的设计与实现(源码+调试)
  • java实现局域网内视频投屏播放(一)背景/需求
  • MATLAB Sub2ind下标值转化
  • Linux---获取管理员权限的相关命令
  • Day20【time模块】
  • 在Flutter中使用PhotoViewGallery指南
  • Docker与K8s的区别
  • 数字化转型导师坚鹏:中国工商银行人工智能与金融数字化转型培训
  • vscode 文件目录栏缩进
  • 基于Springboot的高校教学评价系统的设计与实现(源码+调试)
  • FreeRDP WebConnect Url 任意文件读取漏洞复现
  • acwing算法提高之动态规划--状态压缩DP
  • 网络安全—学习溯源和日志分析
  • Axure元件的介绍使用以及登录界面
  • HarmonyOS给应用添加消息通知
  • canvas 绘制双线技巧
  • CSS魔法堂:Absolute Positioning就这个样
  • GDB 调试 Mysql 实战(三)优先队列排序算法中的行记录长度统计是怎么来的(上)...
  • Git初体验
  • javascript 哈希表
  • Java读取Properties文件的六种方法
  • JS+CSS实现数字滚动
  • JS笔记四:作用域、变量(函数)提升
  • JS函数式编程 数组部分风格 ES6版
  • JS基础之数据类型、对象、原型、原型链、继承
  • leetcode388. Longest Absolute File Path
  • MyEclipse 8.0 GA 搭建 Struts2 + Spring2 + Hibernate3 (测试)
  • Mysql优化
  • node-glob通配符
  • Promise面试题,控制异步流程
  • Vue学习第二天
  • WebSocket使用
  • 包装类对象
  • 短视频宝贝=慢?阿里巴巴工程师这样秒开短视频
  • 二维平面内的碰撞检测【一】
  • 给自己的博客网站加上酷炫的初音未来音乐游戏?
  • 记一次和乔布斯合作最难忘的经历
  • 聚簇索引和非聚簇索引
  • 你不可错过的前端面试题(一)
  • 前端性能优化--懒加载和预加载
  • 思维导图—你不知道的JavaScript中卷
  • 延迟脚本的方式
  • media数据库操作,可以进行增删改查,实现回收站,隐私照片功能 SharedPreferences存储地址:
  • HanLP分词命名实体提取详解
  • Java总结 - String - 这篇请使劲喷我
  • #git 撤消对文件的更改
  • (17)Hive ——MR任务的map与reduce个数由什么决定?
  • (5)STL算法之复制
  • (bean配置类的注解开发)学习Spring的第十三天
  • (delphi11最新学习资料) Object Pascal 学习笔记---第2章第五节(日期和时间)
  • (Forward) Music Player: From UI Proposal to Code
  • (JSP)EL——优化登录界面,获取对象,获取数据
  • (二)c52学习之旅-简单了解单片机
  • (汇总)os模块以及shutil模块对文件的操作
  • (免费分享)基于springboot,vue疗养中心管理系统