需求:
小程序需要实现日历的需求,可以单选、可以选择区间、节假日和周六颜色高亮、返回本月。具体如图,类似猫眼app的日历选择。
思路:
步骤:
修改样式及组件的属性配置
下图是下载好的组件库,删掉了没有用的组件
将日历中的日期进行三种展示,分别是,普通日期、节假日、周六日
添加返回本月按钮,以及返回本月对应的方法,在开发者工具上好用,但体验版上不好使
后续删掉了整个按钮,具体问题没有分析
修改样式及组件的属性配置
<view style="{{!showCalendar ? 'badisplay:block' : 'background: #4c4c4c;display:none'}}" >
<view>页面内容view>
<view class="dayDate">
<view class="pickerBox {{asyncShow ? 'open' : 'off'}}" bindtap="goCalendar" data-dateBox="{{dateBox}}" data-platformIdIndex="{{platformIdIndex}}">
<view class="picker">{{dateName}}view>
<image class="drop-down" src="../../../images/drop-down.png">image>
view>
view>
view>
<view>
<van-calendar wx:if="{{asyncLoad}}" style="{{showCalendar ? 'display:block' : 'display:none'}}" show-mark="{{ false }}" formatter="{{ formatter }}" default-date="{{ defaultVal }}" min-date="{{ minDate }}" max-date="{{ maxDate }}" show="{{ showCal }}" show-confirm="{{ false }}" bind:close="onClose" bind:confirm="onConfirm" round="false" position="bottom" />
view>
// pages/boxOfficeModule/boxOfficeList/boxOfficeList.js
var app = getApp()
var util = require('../../../utils/util.js')
import * as echarts from '../../../ec-canvas/echarts';
Page({
data: {
date: '',
showCal: false,
minDate: new Date(2019, 0, 1).getTime(),
maxDate: new Date().getTime(),
defaultVal: new Date('2021-04-19').getTime(),
formatter(day) {
// console.log('day', day)
const year = day.date.getFullYear()
const month = day.date.getMonth() + 1
const date = day.date.getDate()
let week = day.date.getDay()
if (week === 0) week = 7
const holiday = [
{
year: 2020,
monthDate: [
{
month: 1,
date: [
{ day: 1, text: '元旦', holidayColor: true },
{ day: 24, text: '除夕', holidayColor: true },
{ day: 25, text: '春节', holidayColor: true },
]
},
{
month: 2,
date: [
{ day: 14, text: '情人节', holidayColor: true },
{ day: 8, text: '元宵节', holidayColor: true }
]
},
{
month: 4,
date: [
{ day: 1, text: '愚人节', holidayColor: true },
{ day: 4, text: '清明节', holidayColor: true }
]
},
{
month: 5,
date: [
{ day: 1, text: '劳动节', holidayColor: true }
]
},
{
month: 6,
date: [
{ day: 1, text: '儿童节', holidayColor: true },
{ day: 25, text: '端午节', holidayColor: true }
]
},
{
month: 8,
date: [
{ day: 25, text: '七夕节', holidayColor: true }
]
},
{
month: 9,
date: [
{ day: 10, text: '教师节', holidayColor: true }
]
},
{
month: 10,
date: [
{ day: 1, text: '国庆节', holidayColor: true },
{ day: 1, text: '中秋节', holidayColor: true }
]
},
{
month: 11,
date: [
{ day: 11, text: '光棍节', holidayColor: true }
]
},
{
month: 12,
date: [
{ day: 24, text: '平安夜', holidayColor: true },
{ day: 25, text: '圣诞节', holidayColor: true }
]
},
]
},
{
year: 2021,
monthDate: [
{
month: 1,
date: [
{ day: 1, text: '元旦', holidayColor: true }
]
},
{
month: 2,
date: [
{ day: 14, text: '情人节', holidayColor: true },
{ day: 11, text: '除夕', holidayColor: true },
{ day: 12, text: '春节', holidayColor: true },
{ day: 26, text: '元宵节', holidayColor: true }
]
},
{
month: 4,
date: [
{ day: 1, text: '愚人节', holidayColor: true },
{ day: 4, text: '清明节', holidayColor: true }
]
},
{
month: 5,
date: [
{ day: 1, text: '劳动节', holidayColor: true }
]
},
{
month: 6,
date: [
{ day: 1, text: '儿童节', holidayColor: true },
{ day: 14, text: '端午节', holidayColor: true }
]
},
{
month: 8,
date: [
{ day: 14, text: '七夕节', holidayColor: true }
]
},
{
month: 9,
date: [
{ day: 10, text: '教师节', holidayColor: true },
{ day: 21, text: '中秋节', holidayColor: true }
]
},
{
month: 10,
date: [
{ day: 1, text: '国庆节', holidayColor: true }
]
},
{
month: 11,
date: [
{ day: 11, text: '光棍节', holidayColor: true }
]
},
{
month: 12,
date: [
{ day: 24, text: '平安夜', holidayColor: true },
{ day: 25, text: '圣诞节', holidayColor: true }
]
},
]
},
{
year: 2022,
monthDate: [
{
month: 1,
date: [
{ day: 1, text: '元旦', holidayColor: true },
{ day: 31, text: '除夕', holidayColor: true },
]
},
{
month: 2,
date: [
{ day: 14, text: '情人节', holidayColor: true },
{ day: 1, text: '春节', holidayColor: true },
{ day: 25, text: '元宵节', holidayColor: true }
]
},
{
month: 4,
date: [
{ day: 1, text: '愚人节', holidayColor: true },
{ day: 5, text: '清明节', holidayColor: true }
]
},
{
month: 5,
date: [
{ day: 1, text: '劳动节', holidayColor: true }
]
},
{
month: 6,
date: [
{ day: 1, text: '儿童节', holidayColor: true },
{ day: 3, text: '端午节', holidayColor: true }
]
},
{
month: 8,
date: [
{ day: 4, text: '七夕节', holidayColor: true }
]
},
{
month: 9,
date: [
{ day: 10, text: '教师节', holidayColor: true },
{ day: 10, text: '中秋节', holidayColor: true }
]
},
{
month: 10,
date: [
{ day: 1, text: '国庆节', holidayColor: true }
]
},
{
month: 11,
date: [
{ day: 11, text: '光棍节', holidayColor: true }
]
},
{
month: 12,
date: [
{ day: 24, text: '平安夜', holidayColor: true },
{ day: 25, text: '圣诞节', holidayColor: true }
]
},
]
},
]
if (week === 6 || week === 7) day.weekendColor = true
const yearObj = holiday.filter(item => item.year === year)[0]
if (!yearObj) return day
const monthObj = yearObj.monthDate.filter(item => item.month === month)[0]
if (!monthObj) return day
monthObj.date.forEach(item => {
if (item.day === date) {
day.text = item.text
day.holidayColor = item.holidayColor
}
})
return day;
},
},
onClose() {
this.setData({ showCal: false, showCalendar: false });
},
formatDate(date) {
date = new Date(date);
return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
},
onConfirm(event) {
let date = this.formatDate(event.detail)
let dateTime = new Date(date.replace(/-/g, '/')).getTime()
this.setData({
showCalendar: false,
showCal: false,
date: date,
})
const prevDate = util.formatDate(dateTime, 'yyyy-mm-dd')
const prevDateName = util.formatDate(dateTime, 'yyyy年mm月dd日')
this.setData({
"dateName": prevDateName,
"dateBox": prevDate,
"dateTime": dateTime,
"queryParam.dateStart": prevDate,
})
this.getList(this.data.queryParam)
},
onLoad: function (options) {
if (options && options.dateTime) {
var time = options.dateTime - 0
} else {
var time = new Date().getTime() - 24*60*60*1000
}
const prevDate = util.formatDate(time, 'yyyy-mm-dd')
this.setData({
"dateBox": prevDate,
"dateTime": time,
"queryParam.dateStart": prevDate,
})
this.getList(this.data.queryParam)
},
onReady: function () {
setTimeout(() => {
this.setData({
// 控制日历组件显示
asyncLoad: true,
// 控制禁用
asyncShow: true,
})
}, 50)
}
})
/* components/watermark/watermark.json */
{
"navigationBarTitleText": "榜单",
"usingComponents": {
"ec-canvas": "../../../ec-canvas/ec-canvas",
"van-calendar": "@vant/weapp/calendar/index",
"van-cell": "@vant/weapp/cell/index",
"van-button": "@vant/weapp/button/index"
},
"navigationBarBackgroundColor": "#E8380D",
"navigationBarTextStyle": "white"
}
参考文章:
Vant Weapp官网
微信小程序:自定义组件的数据传递
微信小程序中自定义组件间通信传值
【微信小程序】自定义组件的使用+传值
日历以子组件的形式在具体页面使用(第二种方案)
<van-calendar
default-date="{{ defaultVal }}"
poppable="{{ false }}"
show-confirm="{{ false }}"
style="--calendar-height: {{screenHeight}}"
type="single"
bind:confirm="onConfirm"
min-date="{{ minDate }}"
max-date="{{ maxDate }}"
show-mark="{{ false }}"
show-title="{{ false }}"
formatter="{{ formatter }}"
/>
// pages/boxOfficeModule/calendar/calendar.js
Component({
/**
* 页面的初始数据
*/
properties: {
date: {
type: String,
value: ''
},
platformIdIndex: {
type: String,
value: ''
}
},
data: {
date: '',
show: false,
minDate: new Date(2018, 0, 1).getTime(),
maxDate: new Date().getTime(),
defaultVal: new Date('2021-04-19').getTime(),
formatter(day) {
const year = day.date.getFullYear()
const month = day.date.getMonth() + 1
const date = day.date.getDate()
let week = day.date.getDay()
if (week === 0) week = 7
const holiday = [
{
year: 2020,
monthDate: [
{
month: 1,
date: [
{ day: 1, text: '元旦', holidayColor: true },
{ day: 24, text: '除夕', holidayColor: true },
{ day: 25, text: '春节', holidayColor: true },
]
},
{
month: 2,
date: [
{ day: 14, text: '情人节', holidayColor: true },
{ day: 8, text: '元宵节', holidayColor: true }
]
},
{
month: 4,
date: [
{ day: 1, text: '愚人节', holidayColor: true },
{ day: 4, text: '清明节', holidayColor: true }
]
},
{
month: 5,
date: [
{ day: 1, text: '劳动节', holidayColor: true }
]
},
{
month: 6,
date: [
{ day: 1, text: '儿童节', holidayColor: true },
{ day: 25, text: '端午节', holidayColor: true }
]
},
{
month: 8,
date: [
{ day: 25, text: '七夕节', holidayColor: true }
]
},
{
month: 9,
date: [
{ day: 10, text: '教师节', holidayColor: true }
]
},
{
month: 10,
date: [
{ day: 1, text: '国庆节', holidayColor: true },
{ day: 1, text: '中秋节', holidayColor: true }
]
},
{
month: 11,
date: [
{ day: 11, text: '光棍节', holidayColor: true }
]
},
{
month: 12,
date: [
{ day: 24, text: '平安夜', holidayColor: true },
{ day: 25, text: '圣诞节', holidayColor: true }
]
},
]
},
{
year: 2021,
monthDate: [
{
month: 1,
date: [
{ day: 1, text: '元旦', holidayColor: true }
]
},
{
month: 2,
date: [
{ day: 14, text: '情人节', holidayColor: true },
{ day: 11, text: '除夕', holidayColor: true },
{ day: 12, text: '春节', holidayColor: true },
{ day: 26, text: '元宵节', holidayColor: true }
]
},
{
month: 4,
date: [
{ day: 1, text: '愚人节', holidayColor: true },
{ day: 4, text: '清明节', holidayColor: true }
]
},
{
month: 5,
date: [
{ day: 1, text: '劳动节', holidayColor: true }
]
},
{
month: 6,
date: [
{ day: 1, text: '儿童节', holidayColor: true },
{ day: 14, text: '端午节', holidayColor: true }
]
},
{
month: 8,
date: [
{ day: 14, text: '七夕节', holidayColor: true }
]
},
{
month: 9,
date: [
{ day: 10, text: '教师节', holidayColor: true },
{ day: 21, text: '中秋节', holidayColor: true }
]
},
{
month: 10,
date: [
{ day: 1, text: '国庆节', holidayColor: true }
]
},
{
month: 11,
date: [
{ day: 11, text: '光棍节', holidayColor: true }
]
},
{
month: 12,
date: [
{ day: 24, text: '平安夜', holidayColor: true },
{ day: 25, text: '圣诞节', holidayColor: true }
]
},
]
},
]
if (week === 6 || week === 7) day.weekendColor = true
const yearObj = holiday.filter(item => item.year === year)[0]
if (!yearObj) return day
const monthObj = yearObj.monthDate.filter(item => item.month === month)[0]
if (!monthObj) return day
monthObj.date.forEach(item => {
if (item.day === date) {
day.text = item.text
day.holidayColor = item.holidayColor
}
})
return day;
},
screenHeight: '600px',
platformIdIndex: ''
},
methods: {
onDisplay() {
this.setData({ show: true });
},
onClose() {
this.setData({ show: false });
},
formatDate(date) {
date = new Date(date);
return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
},
onConfirm(event) {
let date = this.formatDate(event.detail)
let dateTime = new Date(date.replace(/-/g, '/')).getTime()
let platformIdIndex = this.data.platformIdIndex
this.setData({
show: false,
date: date
})
console.log('event.detail', event)
this.triggerEvent('dayChange', {
dateTime: dateTime,
platformIdIndex: platformIdIndex,
showCalendar: false
})
// wx.reLaunch({
// url: '/pages/boxOfficeModule/boxOfficeList/boxOfficeList?dateTime=' + dateTime + '&platformIdIndex=' + platformIdIndex
// })
},
computeScrollViewHeight() {
let screenHeight = wx.getSystemInfoSync().windowHeight - 40
this.setData({
"screenHeight": screenHeight + 'px'
})
},
},
/**
* 生命周期函数--监听页面加载
*/
attached: function (options) {
this.computeScrollViewHeight()
console.log('this.properties', this.properties.date)
console.log('new Date(this.properties.date).getTime()', new Date(this.properties.date).getTime())
// if (options && options.date) {
// this.setData({
// "date": this.properties.date,
// "defaultVal": new Date(this.properties.date).getTime(),
// "platformIdIndex": this.properties.platformIdIndex
// })
console.log('this.data.date', this.data.date, this.data.defaultVal)
// }
},
})
/* pages/boxOfficeModule/calendar/calendar.wxss */
.calendar {
--calendar-height: 600px;
}
/* pages/boxOfficeModule/calendar/calendar.json */
{
"navigationBarTitleText": "榜单",
"navigationBarBackgroundColor": "#E8380D",
"navigationBarTextStyle": "white",
"usingComponents": {
"van-calendar": "@vant/weapp/calendar/index",
"van-cell": "@vant/weapp/cell/index",
"van-button": "@vant/weapp/button/index"
},
"component": true
}