小程序自定义日历效果小程序自定义日历效果
主要为大家详细介绍了小程序自定义日历效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了小程序自定义日历展示的具体代码,供大家参考,具体内容如下
重点在于将数个月的日期拆分重组,然后再统一以月为输出。详细操作代码注释有说明。
<view class="flex box box-tb box-align-center">
<!--整体日期循环部分 -->
<block wx:for="{{allMan}}" wx:for-item="item" >
<view class="calendar pink-color box box-tb">
<view class="top-handle fs28 box box-lr box-align-center box-pack-center">
<view class="date-area box box-lr box-align-center box-pack-center years-month">{{item.year || "--"}} 年 {{item.Month || "--"}} 月</view>
</view>
<!--展示星期几的头部 -->
<view class="weeks box box-lr box-pack-center box-align-center day-week">
<view class="flex week fs28" wx:for="{{weeks_ch}}" wx:key="{{index}}" data-idx="{{index}}">{{item}}</view>
</view>
<view class="days box box-lr box-wrap">
<view class="grid white-color box box-align-center box-pack-center" wx:for="{{item.empyt}}" wx:key="{{index}}" data-idx="{{index}}">
</view>
<!--循环日期 -->
<block wx:for="{{item.day}}" wx:for-item="item" wx:key="{{index}}">
<view class="grid white-color box box-align-center box-pack-center" data-idx="{{index}}" data-dayz="{{item.dayZ}}" bindtap="{{item.canChoosed ? 'tapDayItemX' : ''}}">
<view class="day {{item.dependence ? 'border-instinct pink-bg' : ''}} {{item.startDay ? 'border-radius dependence-bg' : ''}} {{item.endDay ? 'border-right-radius dependence-bg' : ''}} {{item.canChoosed ? '' : 'grays'}} box box-align-center box-pack-center">
{{item.today}}
</view>
</view>
</block>
</view>
</view>
</block>
</view>
.js:
let choose_year = null,//选择的年
choose_month = null;//选择的月
let clickNum=new Array();//点击日期的次数,做显示判断处理
let date = new Date();
let cur_year = date.getFullYear();
let cur_month = date.getMonth() + 1;
let weeks_ch = ['日', '一', '二', '三', '四', '五', '六']; //日历的显示
let allMan = new Array();//循环总日历容器
let haveClickNum=false;//是否可点击选择日期
let havInitDate = false;//是否已经重置数据
let conf = {//拿去给Page
onLoad() {
if (allMan.length != 0) {//重新进入界面发现已经有数据,直接渲染
havInitDate = false;
clickNum = new Array();
haveClickNum = true;
this.setData({
allMan,
weeks_ch
})
}else{
allMan = new Array();//非常重要的主输出,计算好的每月数据都合拼在里面
clickNum = new Array();
let that = this;
let empytGrids = this.calculateEmptyGrids(cur_year, cur_month);//计算空格
let days = this.calculateDays(cur_year, cur_month);//计算日期总数
let DogDays = { year: cur_year, Month: cur_month, empyt: empytGrids, day: days };//组装年、月、星期一前的空格、日
allMan.push(DogDays);
let newMonth = cur_month + 1;
let newYear = cur_year;
if (newMonth > 12) {//新年重置1月份
newYear = cur_year + 1;
newMonth = 1;
}
let empytGridsTwos = this.calculateEmptyGridsTwo(newYear, newMonth);//计算新月份的空格
let dayTwos = this.calculateDaysTwo(newYear, newMonth);//计算日期总数
let catDays = { year: newYear, Month: newMonth, empyt: empytGridsTwos, day: dayTwos };
allMan.push(catDays);
let sakura = days.concat(dayTwos);//将存放日期的内容统一合并为数组,单独处理是否可选等展示状态,属于中间件数组
let isTrue = new Array();//存放可选
for (var i = 0; i < sakura.length; i++) {//循环处理
if (sakura[i].canChoosed == true) {
isTrue.push(sakura[i]);
} else {
continue
}
}
let addMoreMonth = function () {//添加更多的月份
let isTrue = new Array();//是否可选的日期数组,为N个月等可选日期总日输做判断
for (var i = 0; i < sakura.length; i++) {
if (sakura[i].canChoosed == true) {
isTrue.push(sakura[i]);
} else {
continue
}
}
let newMonthThree = newMonth + 1;
let newYearThree = newYear;
if (newMonthThree > 12) {
newYearThree = newYear + 1;
newMonthThree = 1;
}
let empytGridsThree = that.calculateEmptyGridsTwo(newYearThree, newMonthThree);//计算空格
let dayThree = that.calculateDaysTwo(newYearThree, newMonthThree, 91 - isTrue.length);//计算日期总数,61减,今天不算
let duckDays = { year: newYearThree, Month: newMonthThree, empyt: empytGridsThree, day: dayThree };
sakura = sakura.concat(dayThree);
allMan.push(duckDays);
if (parseInt(91 - isTrue.length) > parseInt(dayThree.length)) {//第n个月是否足够显示需要的可选日期长度
for (var i = 0; i < dayThree.length; i++) {
if (dayThree[i].canChoosed == true) {
isTrue.push(dayThree[i]);
} else {
continue
}
}
let newMonthFour = newMonthThree + 1;
let newYearFour = newYearThree;
if (newMonthFour > 12) {
newYearFour = newYearThree + 1;
newMonthFour = 1;
}
let empytGridsFour = that.calculateEmptyGridsTwo(newYearFour, newMonthFour);//计算空格
let dayFour = that.calculateDaysTwo(newYearFour, newMonthFour, 91 - isTrue.length);//计算日期总数,61减,今天不算
let wolfDays = { year: newYearFour, Month: newMonthFour, empyt: empytGridsFour, day: dayFour };
sakura = sakura.concat(dayFour);
allMan.push(wolfDays);
} else {//不足够继续增加第n+1个月
let newMonthFour = newMonthThree + 1;
let newYearFour = newYearThree;
if (newMonthFour > 12) {
newYearFour = newYearThree + 1;