uniapp 日历组件

我们的需求是显示当前月和下个月的排班表

引入 uniapp 日历组件 uni-calendar

做法有两种,一种是直接去修改组件,还有就是文档中提供的 selected 方法

修改组件的就不写了

<uni-calendar :lunar="true" :selected="selected" :insert="true" :start-date="'2015-1-1'" :end-date="'2090-12-31'" @change="change" @monthSwitch="monthSwitch"></uni-calendar>

onLoad() {
        //  进入页面获取年月
		this.year = Moment().year()
		this.month = Moment().format('MM')
        // 获取这个月和下个月的天数
		this.daysInatMonth = this.getMonthDay(0); // 获取这个月的天数
		this.daysInNextMonth = this.getMonthDay(1); // 获取下个月的天数
		this.dataInit(this.year, this.month, this.daysInatMonth, this.daysInNextMonth)
},
methods: {
		dataInit(year, month, at, next) {
			var randomType = [
				{ type: 'night', info: '夜班' },
				{ type: 'rest', info: '休息' },
				{ type: 'daytime', info: '白班' },
			  ]
			var arr1 = this.getNewArray(year, month, at, randomType)
			var arr2 = this.getNewArray(year, (Number(month) + 1), next, randomType)
			this.selected = [...arr1, ...arr2];
		},
        // 目前排班表是写死的
		getNewArray(year, month, day, randomType) {
            // 我们根据这个月多少天 生成多少条数据然后填充
			var list = new Array(day).fill('').map((item, index) => {
                // 让 randomType 这个数组中数据随机到这个月每天中
				const randomNum = Math.floor(Math.random() * randomType.length);
				const obj = { date: year + '-' + month + '-' + Number(index + 1).toString().padStart(2, '0'), info: randomType[randomNum].info, data: { type: randomType[randomNum].type } }

				return obj
			})
			return list
		},
		getMonthDay(type) {
			// 获取下个月的日期 0 是当月  1 下个月 
			return Moment().add(type, 'month').daysInMonth();
		},

		change(res) {},
		
		monthSwitch(res) {
			this.dataInit(this.year, this.month, this.daysInatMonth, this.daysInNextMonth)
		},
		
	}

如果想给每月都添加下面的东西,在monthSwitch 方法下传的月和年修改即可,同时还需要去修改组件

  • 10
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值