日历的功能,我们会经常用到,且逻辑比较复杂,小算法较多,花了半天时间写了个,特此详记。

先贴图

功能阐述:返回本月不多说,设置工作日和节假日是为了公司制度需要,后台会有假日表来记录。

为了适应于vue框架,很多jquery的方法用不上,例如addClass及removeClass,所以可能某些地方做的比较繁琐。

<template>
<div>
<div class="date"> <!-- 年份 月份 -->
<div class="month">
<i class="el-icon-arrow-left" @click="pickPre(currentYear,currentMonth)"></i>
<i>{{ currentYear }} 年 {{ currentMonth }} 月</i>
<i class="el-icon-arrow-right" @click="pickNext(currentYear,currentMonth)"></i>
</div>
<!-- 星期 -->
<ul class="weekdays">
<li>一</li>
<li>二</li>
<li>三</li>
<li>四</li>
<li>五</li>
<li style="color:#0A0A0A">六</li>
<li style="color:#0A0A0A">日</li>
</ul>
<!-- 日期 -->
<div class="bodyDiv">
<ul class="days" v-for="(value,index1) in daysUL">
<li @click="pick(day,index+index1*7)" v-for="(day, index) in value" :class="[{'ban':isBan[index+index1*7]},{'xiu':isXiu[index+index1*7]}]" >
<!--本月-->
<span v-if="day.getMonth()+1 != currentMonth" class="other-month" :class="{'selected':isSelected[index+index1*7]}">{{ day.getDate() }}</span>
<span v-else :class="{'selected':isSelected[index+index1*7]}">
<!--今天-->
<span v-if="day.getFullYear() == new Date().getFullYear() && day.getMonth() == new Date().getMonth() && day.getDate() == new Date().getDate()" class="active">{{ day.getDate() }}</span>
<span v-else>{{ day.getDate() }}</span>
</span>
</li> </ul>
</div>
<hr style="height:2px;border:none;border-top:2px dotted #185598;" />
</div>
<div class="button">
<div><el-button type="primary" size="large" @click="returnNow()">返回本月</el-button></div>
<div><el-button type="primary" size="large" @click="setRestOrWork('R')">设置为节假日</el-button></div>
<div><el-button type="primary" size="large" @click="setRestOrWork('W')">设置为工作日</el-button></div>
<div><el-button type="primary" size="large" @click="cancel()">取消</el-button></div>
</div>
</div>
</template> <script>
import { calendarMsgService } from './CalendarMsgService'
export default {
name: 'date', data () {
return {
currentYear: 1970, // 年份
currentMonth: 1, // 月份
currentDay: 1, // 日期
currentWeek: 1, // 星期
firstWeek:1,
days: [],
daysUL:[],
params:{
selectDay:'',
type:''
},
isSelected:[],
isBan:[],
isXiu:[],
restDays:{
year:'',
month:'',
day:'',
resttype:'',
restdate:''
},
restDaysList:[],
banList:[],
xiuList:[],
selectIndex:''
}
}, created () {
this.initData(null)
}, methods: {
//格式化日期
formatDate (year, month, day) {
const y = year
let m = month
if (m < 10) m = `0${m}`
let d = day
if (d < 10) d = `0${d}`
return `${y}-${m}-${d}`
}, initData (cur) {
debugger;
let date = ''
if (cur) {
date = new Date(cur)
} else {
date = new Date()
}
this.currentDay = date.getDate() // 今日日期 几号
this.currentYear = date.getFullYear() // 当前年份
this.currentMonth = date.getMonth() + 1 // 当前月份
this.currentWeek = date.getDay() // 1...6,0 // 今天是星期几 //当前月的第一天是星期几
date.setDate(1);
this.firstWeek = date.getDay(); if (this.firstWeek === 0) {
this.firstWeek = 7;
}
const str = this.formatDate(this.currentYear, this.currentMonth, 1)// 今日日期 年-月-日
this.days.length = 0 // 今天是周日,放在第一行第7个位置,前面6个 这里默认显示一周,如果需要显示一个月,则第二个循环为 i<= 42- this.firstWeek
for (let i = this.firstWeek - 1; i >= 0; i -= 1) {
const d = new Date(str)
d.setDate(d.getDate() - i)
this.days.push(d)
}
//处理1号是星期天为 7 的情况, 为7天就直接放在daysUL里
if (this.days.length % 7 === 0){
this.daysUL.push(this.days);
this.days = [];
} for (let i = 1; i <= 42 - this.firstWeek; i += 1) {
const d = new Date(str);
d.setDate(d.getDate() + i);
this.days.push(d); //一个 days 就是一行7天 daysUL 就是个数组,里面有六个days 就是六行42天
if (this.days.length % 7 === 0){
this.daysUL.push(this.days);
this.days = []; //清空重新存放天数
}
}
//调后台接口,获取当前年,当前月的 班休时间
calendarMsgService.getRestDays({currentYear:this.currentYear,currentMonth:this.currentMonth}).then(res => {
if (res.code === 0){
debugger;
this.restDaysList = res.content;
this.dealResult(this.currentYear,this.currentMonth);
}
})
},
setRestOrWork(type) {
if (this.onlySelect()) {
this.params.type = type;
debugger;
calendarMsgService.addRestDays(this.params).then(res => {
if (res.code === 0) {
this.$message({
message: '设置成功!',
type: 'success'
})
if (type == 'R'){
this.isXiu[this.selectIndex] = true;
}
if (type == 'W'){
this.isBan[this.selectIndex] = true;
}
} else {
this.$message({
message: res.msg,
type: 'error'
})
}
this.params.selectDay = '';
this.params.type = '';
}) }
},
cancel() {
if (this.onlySelect()) {
calendarMsgService.cancelRestDays(this.params).then(res => {
if (res.code === 0) {
this.$message({
message: '取消成功!',
type: 'success'
})
this.isXiu[this.selectIndex] = false;
this.isBan[this.selectIndex] = false;
} else {
this.$message({
message: res.msg,
type: 'error'
})
}
this.params.selectDay = '';
this.params.type = '';
})
}
}, // 上一個月 传入当前年份和月份
pickPre (year, month) {
this.daysUL = [];
this.isSelected = [];
const d = new Date(this.formatDate(year, month, 1))
d.setDate(0)
this.initData(this.formatDate(d.getFullYear(), d.getMonth() + 1, 1))
calendarMsgService.getRestDays({currentYear:this.currentYear,currentMonth:this.currentMonth}).then(res => {
if (res.code === 0){
debugger;
this.restDaysList = res.content;
this.dealResult(this.currentYear,this.currentMonth);
}
})
}, // 下一個月 传入当前年份和月份
pickNext (year, month) {
this.daysUL = [];
this.isSelected = [];
const d = new Date(this.formatDate(year, month, 1))
d.setDate(42)
this.initData(this.formatDate(d.getFullYear(), d.getMonth() + 1, 1));
//当点击下个月的时候,才会去拿该月的休息或者工作日的日期,而不是一下子都拿出来
calendarMsgService.getRestDays({currentYear:this.currentYear,currentMonth:this.currentMonth}).then(res => {
if (res.code === 0){
debugger;
this.restDaysList = res.content;
this.dealResult(this.currentYear,this.currentMonth);
}
})
},
//算法
dealResult(currentYear,currentMonth){
debugger;
this.banList = []; //把当前月的 工作日 放在一起
this.xiuList = []; //把当前月的 休息日 放在一起
this.isBan = []; //设置标识,来确定用什么样的背景图
this.isXiu = [];
let zhouji = new Date(this.formatDate(currentYear, currentMonth, 1)).getDay(); //被查找的月份 1 号是星期几
if (zhouji === 0){ // 0 就是星期天
zhouji = 7;
}
for (let i = 0; i<this.restDaysList.length;i++){
this.restDays = this.restDaysList[i];
if (this.restDays.resttype === 'W') {
let ban = this.restDays.day - 1 + (zhouji - 1);//重要算法,算出班日,在几号位
this.banList.push(ban);
}
if (this.restDays.resttype === 'R'){
let xiu = this.restDays.day - 1 + (zhouji - 1);//重要算法,算出休息日,在几号位
this.xiuList.push(xiu);
}
}
for (let m = 0; m < 42; m++) { // banlist 里面放置的都是在日历上处于几号位,而不是工作日的日期,
let nothave = true; // 所以得把这些位置号拎出来,给它们于不同的样式
for (let k = 0; k < this.banList.length; k++) {
if (m == this.banList[k]) {
this.isBan.push(true);
nothave = false;
break;
}
}
if (nothave) {
this.isBan.push(false);
} }
for (let n = 0; n < 42; n++) { // 同上,来处理休息日
let nothave = true;
for (let k = 0; k < this.xiuList.length; k++) {
if (n == this.xiuList[k]) {
this.isXiu.push(true);
nothave = false;
break;
}
}
if (nothave) {
this.isXiu.push(false);
} } },
returnNow(){
this.daysUL = [];
this.initData(null);
},
// 当前选择日期
pick (date,index) {
debugger;
this.selectIndex = index;
this.isSelected = [];
this.params.selectDay = this.formatDate(date.getFullYear(), date.getMonth() + 1, date.getDate());
for (let i = 0; i < 42; i++) {
if (index == i) {
this.isSelected.push(true);
continue;
}
this.isSelected.push(false);
}
},
onlySelect(){
debugger;
if(this.params.selectDay === ''){
this.$message({
message: '请选择日期',
type: 'warning'
})
return false;
}
return true;
}
},
} </script> <style scoped>
.date {
height: 150px;
width:1000px;
color: #333;
float: left;
}
.button{
float: left;
margin-left:110px;
margin-top:120px;
}
.button>div{
margin-top:70px; }
.month {
font-size: 24px;
text-align: center;
margin-top: 20px;
} .weekdays {
background-color: #20A0FF;
opacity: 0.6;
display: flex;
font-size: 28px;
margin-top: 20px;
} .days {
display: flex;
} li {
flex: 1;
font-size: 35px;
width:50px;
list-style-type:none;
text-align: center;
margin-top: 5px;
line-height: 60px;
cursor:pointer;
}
.selected{
display: inline-block;
width: 60px;
height: 60px;
color: #fff;
border-radius: 70%;
background-color: #1E90FF;
}
.ban{
background-image: url(image/ban.jpg);
}
.xiu{
background-image: url(./image/xiu.jpg);
background-repeat: no-repeat;
}
.active {
display: inline-block;
width: 60px;
height: 60px;
color: #fff;
border-radius: 50%;
background-color: #324057; }
i{
margin-right:30px;
cursor:pointer
} .other-month {
color: #EEC591;
} </style>

重要的点,都已经注释好。

vue 设计日历表的更多相关文章

  1. vue 设计一个倒计时秒杀的组件

    简介: 倒计时秒杀组件在电商网站中层出不穷  不过思路万变不离其踪,我自己根据其他资料设计了一个vue版的 核心思路:1.时间不能是本地客户端的时间  必须是服务器的时间这里用一个settimeout ...

  2. Vue.js 系列教程 4:Vuex

    这是关于 JavaScript 框架 Vue.js 五个教程的第四部分.在这一部分,我们会学习使用 Vuex 进行状态管理. 这不是一个完整的指南,而是基础知识的概述,所以你可以了解 Vue.js 以 ...

  3. 通俗理解vuex原理---通过vue例子类比

    本文主要通过简单的理解来解释下vuex的基本流程,而这也是vuex难点之一. 首先我们先了解下vuex的作用 vuex其实是集中的数据管理仓库,相当于数据库mongoDB,MySQL等,任何组件都可以 ...

  4. 从后端到前端之Vue(五)小试路由

    一开始我还以为vue的路由只能用在工程化的项目里面呢,然后研究了一下才发现,在脚本化里面也是可以用的.其实呢不管在哪里用,把原理研究明白就对了. 一. 官网demo 这里不得不吐槽一下官网,写的不清不 ...

  5. Vue响应式原理以及注意事项

    响应基于 set 和 get(Object.defineProperty) 类型: 单向绑定 双向绑定 简单例子(基于Object.defineProperty) <!DOCTYPE html& ...

  6. Vue.js 十五分钟入门

    本文经授权转载,仅用于学习,版权归原作者所有. TypeScript 为 JavaScript 带来静态类型检查,让 JavaScript 编写中大型应用的时候可以应用工具来避免部分错误. Vue 很 ...

  7. Vue+ElementUI学习总结(转载)

    Vue框架简介 Vue是一套构建用户界面的框架, 开发只需要关注视图层, 它不仅易于上手,还便于与第三方库或既有项目的整合.是基于MVVM(Model-View-ViewModel)设计思想.提供MV ...

  8. 举个例子去理解vuex(状态管理),通俗理解vuex原理,通过vue例子类比

    通俗理解vuex原理---通过vue例子类比   本文主要通过简单的理解来解释下vuex的基本流程,而这也是vuex难点之一. 首先我们先了解下vuex的作用vuex其实是集中的数据管理仓库,相当于数 ...

  9. [Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being

    [Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent c ...

随机推荐

  1. 一、Spring Cloud介绍

    springcloud 介绍 研究了一段时间spring boot了准备向spirng cloud进发,公司架构和项目也全面拥抱了Spring Cloud.在使用了一段时间后发现Spring Clou ...

  2. (转)java内部类详解

    本文转自http://www.cnblogs.com/dolphin0520/p/3811445.html,谢谢作者 说起内部类这个词,想必很多人都不陌生,但是又会觉得不熟悉.原因是平时编写代码时可能 ...

  3. 【JavaScript】 JS面向对象的模式与实践 (重点整治原型这个熊孩子 (/= _ =)/~┴┴ )

    参考书籍 <JavaScript高级语言程序设计>—— Nicholas C.Zakas <你不知道的JavaScript>  —— KYLE SIMPSON   在JS的面向 ...

  4. 「mysql优化专题」详解引擎(InnoDB,MyISAM)的内存优化攻略?(9)

    注意:以下都是在MySQL目录下的my.ini文件中改写(技术文). 一.InnoDB内存优化 InnoDB用一块内存区域做I/O缓存池,该缓存池不仅用来缓存InnoDB的索引块,而且也用来缓存Inn ...

  5. Sphinx学习笔记2

    因为网站搜索的需要,启动了一个搜索引擎项目,其实也算不上完整的搜索引擎,需求很简单,如下:     1)搜索产品名.类别名.品牌名.副标题.关键字等字段     2)数据量目前为13000左右,未来可 ...

  6. python scipy计算机数值库

    scripy有聚类的python 代码,如: http://blog.csdn.net/elaine_bao/article/details/50242867

  7. JPA的基本使用

    前提: 创建一个springboot项目 创建一个名为springboottest的MySQL数据库 1 jar包准备 jpa的jar包 mysql驱动的jar包 druid数据库连接池的jar包 l ...

  8. JAVA NIO学习二:通道(Channel)与缓冲区(Buffer)

    今天是2018年的第三天,真是时光飞逝,2017年的学习计划还没有学习完成,因此继续开始研究学习,那么上一节我们了解了NIO,那么这一节我们进一步来学习NIO相关的知识.那就是通道和缓冲区.Java ...

  9. tideways+xhgui搭建php 7的性能测试环境

    前言 我之前使用的是xhprof+xhgui分析线上环境的性能,然而PHP版本升级到PHP 7之后,xhprof已经不可用,于是改用tideways+xhgui,这实际上也是PHP7下开源方案的唯一选 ...

  10. Git_学习_03_ ignore文件配置

    一.示例 # 1.Compiled class file *.class bin target # 2.Log file *.log logs # 3. Package Files # *.war * ...