微信小程序自定义带价格显示日历效果微信小程序自定义带价格显示日历效果
主要为大家详细介绍了微信小程序自定义带价格显示日历效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了微信小程序自定义日历效果的具体代码,供大家参考,具体内容如下
JS代码:
var Moment = require("../../utils/moment.js");
var DATE_YEAR = new Date().getFullYear();
var DATE_MONTH = new Date().getMonth() + 1;
var DATE_DAY = new Date().getDate();
Page({
data: {
year: '',
month: '',
day: '',
days: {},
systemInfo: {},
weekStr: ['日', '一', '二', '三', '四', '五', '六'],
checkDate:[]
},
onLoad: function(options) {
var _this = this;
let now = new Date();
let year = now.getFullYear();
let month = now.getMonth() + 1;
// 页面初始化 options为页面跳转所带来的参数
this.createDateListData();
this.setData({
year: year,
month: month
})
wx.getSystemInfo({
success: function(res) {
_this.setData({
systemInfo: res,
});
}
})
},
onReady: function() {
// 页面渲染完成
},
onShow: function() {
},
/**创建日历数据 */
createDateListData: function(setYear, setMonth) {
//全部时间的月份都是按0~11基准,显示月份才+1
let dateArr = []; //需要遍历的日历数组数据
let arrLen = 0; //dateArr的数组长度
let now = setYear ? new Date(setYear, setMonth) : new Date();
let year = setYear || now.getFullYear();
let nextYear = 0;
let month = setMonth || now.getMonth();
//没有+1方便后面计算当月总天数
let nextMonth = (month + 1) > 11 ? 1 : (month + 1);
console.log("当前选中月nextMonth:" + nextMonth);
//目标月1号对应的星期
let startWeek = this.getWeek(year, nextMonth, 1); //new Date(year + ',' + (month + 1) + ',' + 1).getDay();
console.log("目标月1号对应的星期startWeek:" + startWeek);
//获取目标月有多少天
let dayNums = this.getTotalDayByMonth(year, nextMonth); //new Date(year, nextMonth, 0).getDate();
console.log("获取目标月有多少天dayNums:" + dayNums);
let obj = {};
let num = 0;
if (month + 1 > 11) {
nextYear = year + 1;
dayNums = new Date(nextYear, nextMonth, 0).getDate();
}
for (var j = -startWeek + 1; j <= dayNums; j++) {
var tempWeek = -1;
if (j > 0) {
tempWeek = this.getWeek(year, nextMonth, j);
//console.log(year + "年" + month + "月" + j + "日" + "星期" + tempWeek);
}
var clazz = '';
if (tempWeek == 0 || tempWeek == 6)
clazz = 'week'
if (j < DATE_DAY && year == DATE_YEAR && nextMonth == DATE_MONTH)
//当天之前的日期不可用
clazz = 'unavailable ' + clazz;
else
clazz = '' + clazz
/**如果当前日期已经选中,则变色 */
var date = year + "-" + nextMonth + "-" + j;
var index = this.checkItemExist(this.data.checkDate, date);
if (index != -1) {
clazz = clazz + ' active';
}
dateArr.push({
day: j,
class: clazz,
amount:'¥99.8'
})
}
this.setData({
days: dateArr
})
},
/**
* 上个月
*/
lastMonthEvent:function(){
//全部时间的月份都是按0~11基准,显示月份才+1
let year = this.data.month - 2 < 0 ? this.data.year - 1 : this.data.year;
let month = this.data.month - 2 < 0 ? 11 : this.data.month - 2;
this.setData({
year: year,
month: (month + 1)
})
this.createDateListData(year, month);
},
/**
* 下个月
*/
nextMonthEvent:function(){
//全部时间的月份都是按0~11基准,显示月份才+1
let year = this.data.month > 11 ? this.data.year + 1 : this.data.year;
let month = this.data.month > 11 ? 0 : this.data.month;
this.setData({
year: year,
month: (month + 1)
})
this.createDateListData(year, month);