Commit e2b958b4 authored by 潘永坪's avatar 潘永坪

日历禁用修改

parent 79627ed6
...@@ -3,10 +3,9 @@ ...@@ -3,10 +3,9 @@
:show="showPop" :show="showPop"
@confirm="onConfirm" @confirm="onConfirm"
@close="showPop=false" @close="showPop=false"
:maxDate="maxDate"
:minDate='minDate'
:defaultDate="defaultDate" :defaultDate="defaultDate"
:formatter='formatter' :formatter='formatter'
:customList='customList'
ref="calendar2" ref="calendar2"
rowHeight='112' rowHeight='112'
round='20' round='20'
...@@ -21,10 +20,14 @@ export default { ...@@ -21,10 +20,14 @@ export default {
dateList:{ dateList:{
handler(newValue, oldValue){ handler(newValue, oldValue){
if(newValue&&newValue.length>0){ if(newValue&&newValue.length>0){
this.maxDate=newValue[newValue.length-1].startTime.substr(0,10) this.customList=newValue.map(item=>{
//默认和最小日期都为第一天 return item.startTime.substr(0,10)
this.minDate=newValue[0].startTime.substr(0,10) })
this.defaultDate=newValue[0].startTime.substr(0,10) this.defaultDate=newValue[0].startTime.substr(0,10)
//由于传入了customList,最大日期和最小日期可以取消
// this.maxDate=newValue[newValue.length-1].startTime.substr(0,10)
// //默认和最小日期都为第一天
// this.minDate=newValue[0].startTime.substr(0,10)
} }
}, },
deep:true, deep:true,
...@@ -37,6 +40,7 @@ export default { ...@@ -37,6 +40,7 @@ export default {
defaultDate:'', //默认日期 defaultDate:'', //默认日期
maxDate:'',//最大日期 maxDate:'',//最大日期
minDate:'',//最小日期 minDate:'',//最小日期
customList:[],//转化之后的日期列表
} }
}, },
mounted() { mounted() {
...@@ -67,9 +71,6 @@ export default { ...@@ -67,9 +71,6 @@ export default {
// day.dot = true // day.dot = true
// } // }
// }) // })
// if (!day.bottomInfo) {
// day.type = 'disabled'
// }
return day return day
} }
......
...@@ -109,267 +109,269 @@ import Calendar from '../../libs/util/calendar.js' ...@@ -109,267 +109,269 @@ import Calendar from '../../libs/util/calendar.js'
</u-calendar> </u-calendar>
* */ * */
export default { export default {
name: 'u-calendar', name: 'u-calendar',
mixins: [uni.$u.mpMixin, uni.$u.mixin, props], mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
components: { components: {
uHeader, uHeader,
uMonth uMonth
}, },
data() { data() {
return { return {
// 需要显示的月份的数组 // 需要显示的月份的数组
months: [], months: [],
// 在月份滚动区域中,当前视图中月份的index索引 // 在月份滚动区域中,当前视图中月份的index索引
monthIndex: 0, monthIndex: 0,
// 月份滚动区域的高度 // 月份滚动区域的高度
listHeight: 0, listHeight: 0,
// month组件中选择的日期数组 // month组件中选择的日期数组
selected: [], selected: [],
scrollIntoView: '', scrollIntoView: '',
scrollTop:0, scrollTop:0,
// 过滤处理方法 // 过滤处理方法
innerFormatter: (value) => value innerFormatter: (value) => value
} }
}, },
watch: { watch: {
selectedChange: { selectedChange: {
immediate: true, immediate: true,
handler(n) { handler(n) {
this.setMonth() this.setMonth()
} }
}, },
// 打开弹窗时,设置月份数据 // 打开弹窗时,设置月份数据
show: { show: {
immediate: true, immediate: true,
handler(n) { handler(n) {
this.setMonth() this.setMonth()
} }
} }
}, },
computed: { computed: {
// 由于maxDate和minDate可以为字符串(2021-10-10),或者数值(时间戳),但是dayjs如果接受字符串形式的时间戳会有问题,这里进行处理 // 由于maxDate和minDate可以为字符串(2021-10-10),或者数值(时间戳),但是dayjs如果接受字符串形式的时间戳会有问题,这里进行处理
innerMaxDate() { innerMaxDate() {
return uni.$u.test.number(this.maxDate) return uni.$u.test.number(this.maxDate)
? Number(this.maxDate) ? Number(this.maxDate)
: this.maxDate : this.maxDate
}, },
innerMinDate() { innerMinDate() {
return uni.$u.test.number(this.minDate) return uni.$u.test.number(this.minDate)
? Number(this.minDate) ? Number(this.minDate)
: this.minDate : this.minDate
}, },
// 多个条件的变化,会引起选中日期的变化,这里统一管理监听 // 多个条件的变化,会引起选中日期的变化,这里统一管理监听
selectedChange() { selectedChange() {
return [this.innerMinDate, this.innerMaxDate, this.defaultDate] return [this.innerMinDate, this.innerMaxDate, this.defaultDate]
}, },
subtitle() { subtitle() {
// 初始化时,this.months为空数组,所以需要特别判断处理 // 初始化时,this.months为空数组,所以需要特别判断处理
if (this.months.length) { if (this.months.length) {
return `${this.months[this.monthIndex].year}${ return `${this.months[this.monthIndex].year}${
this.months[this.monthIndex].month this.months[this.monthIndex].month
}月` }月`
} else { } else {
return '' return ''
} }
}, },
buttonDisabled() { buttonDisabled() {
// 如果为range类型,且选择的日期个数不足1个时,让底部的按钮出于disabled状态 // 如果为range类型,且选择的日期个数不足1个时,让底部的按钮出于disabled状态
if (this.mode === 'range') { if (this.mode === 'range') {
if (this.selected.length <= 1) { if (this.selected.length <= 1) {
return true return true
} else { } else {
return false return false
} }
} else { } else {
return false return false
} }
} }
}, },
mounted() { mounted() {
this.start = Date.now() this.start = Date.now()
this.init() this.init()
}, },
methods: { methods: {
// 在微信小程序中,不支持将函数当做props参数,故只能通过ref形式调用 // 在微信小程序中,不支持将函数当做props参数,故只能通过ref形式调用
setFormatter(e) { setFormatter(e) {
this.innerFormatter = e this.innerFormatter = e
}, },
// month组件内部选择日期后,通过事件通知给父组件 // month组件内部选择日期后,通过事件通知给父组件
monthSelected(e) { monthSelected(e) {
this.selected = e this.selected = e
if (!this.showConfirm) { if (!this.showConfirm) {
// 在不需要确认按钮的情况下,如果为单选,或者范围多选且已选长度大于2,则直接进行返还 // 在不需要确认按钮的情况下,如果为单选,或者范围多选且已选长度大于2,则直接进行返还
if ( if (
this.mode === 'multiple' || this.mode === 'multiple' ||
this.mode === 'single' || this.mode === 'single' ||
(this.mode === 'range' && this.selected.length >= 2) (this.mode === 'range' && this.selected.length >= 2)
) { ) {
this.$emit('confirm', this.selected) this.$emit('confirm', this.selected)
} }
} }
}, },
init() { init() {
// 校验maxDate,不能小于minDate // 校验maxDate,不能小于minDate
if ( if (
this.innerMaxDate && this.innerMaxDate &&
this.innerMinDate && this.innerMinDate &&
new Date(this.innerMaxDate).getTime() < new Date(this.innerMinDate).getTime() new Date(this.innerMaxDate).getTime() < new Date(this.innerMinDate).getTime()
) { ) {
return uni.$u.error('maxDate不能小于minDate') return uni.$u.error('maxDate不能小于minDate')
} }
// 滚动区域的高度 // 滚动区域的高度
this.listHeight = this.rowHeight * 5 + 30 this.listHeight = this.rowHeight * 5 + 30
this.setMonth() this.setMonth()
}, },
close() { close() {
this.$emit('close') this.$emit('close')
}, },
// 点击确定按钮 // 点击确定按钮
confirm() { confirm() {
if (!this.buttonDisabled) { if (!this.buttonDisabled) {
this.$emit('confirm', this.selected) this.$emit('confirm', this.selected)
} }
}, },
// 获得两个日期之间的月份数 // 获得两个日期之间的月份数
getMonths(minDate, maxDate) { getMonths(minDate, maxDate) {
const minYear = dayjs(minDate).year() const minYear = dayjs(minDate).year()
const minMonth = dayjs(minDate).month() + 1 const minMonth = dayjs(minDate).month() + 1
const maxYear = dayjs(maxDate).year() const maxYear = dayjs(maxDate).year()
const maxMonth = dayjs(maxDate).month() + 1 const maxMonth = dayjs(maxDate).month() + 1
return (maxYear - minYear) * 12 + (maxMonth - minMonth) + 1 return (maxYear - minYear) * 12 + (maxMonth - minMonth) + 1
}, },
// 设置月份数据 // 设置月份数据
setMonth() { setMonth() {
// 最小日期的毫秒数 // 最小日期的毫秒数
const minDate = this.innerMinDate || dayjs().valueOf() const minDate = this.innerMinDate || dayjs().valueOf()
// 如果没有指定最大日期,则往后推3个月 // 如果没有指定最大日期,则往后推3个月
const maxDate = const maxDate =
this.innerMaxDate || this.innerMaxDate ||
dayjs(minDate) dayjs(minDate)
.add(this.monthNum - 1, 'month') .add(this.monthNum - 1, 'month')
.valueOf() .valueOf()
// 最大最小月份之间的共有多少个月份, // 最大最小月份之间的共有多少个月份,
const months = uni.$u.range( const months = uni.$u.range(
1, 1,
this.monthNum, this.monthNum,
this.getMonths(minDate, maxDate) this.getMonths(minDate, maxDate)
) )
// 先清空数组 // 先清空数组
this.months = [] this.months = []
for (let i = 0; i < months; i++) { for (let i = 0; i < months; i++) {
this.months.push({ this.months.push({
date: new Array( date: new Array(
dayjs(minDate).add(i, 'month').daysInMonth() dayjs(minDate).add(i, 'month').daysInMonth()
) )
.fill(1) .fill(1)
.map((item, index) => { .map((item, index) => {
// 日期,取值1-31 // 日期,取值1-31
let day = index + 1 let day = index + 1
// 星期,0-6,0为周日 // 星期,0-6,0为周日
const week = dayjs(minDate) const week = dayjs(minDate)
.add(i, 'month') .add(i, 'month')
.date(day) .date(day)
.day() .day()
const date = dayjs(minDate) const date = dayjs(minDate)
.add(i, 'month') .add(i, 'month')
.date(day) .date(day)
.format('YYYY-MM-DD') .format('YYYY-MM-DD')
let bottomInfo = '' let bottomInfo = ''
if (this.showLunar) { if (this.showLunar) {
// 将日期转为农历格式 // 将日期转为农历格式
const lunar = Calendar.solar2lunar( const lunar = Calendar.solar2lunar(
dayjs(date).year(), dayjs(date).year(),
dayjs(date).month() + 1, dayjs(date).month() + 1,
dayjs(date).date() dayjs(date).date()
) )
bottomInfo = lunar.IDayCn bottomInfo = lunar.IDayCn
} }
let config = { let config = {
day, day,
week, week,
// 小于最小允许的日期,或者大于最大的日期,则设置为disabled状态 // 小于最小允许的日期,或者大于最大的日期,则设置为disabled状态
disabled: disabled:
dayjs(date).isBefore( dayjs(date).isBefore(
dayjs(minDate).format('YYYY-MM-DD') dayjs(minDate).format('YYYY-MM-DD')
) || ) ||
dayjs(date).isAfter( dayjs(date).isAfter(
dayjs(maxDate).format('YYYY-MM-DD') dayjs(maxDate).format('YYYY-MM-DD')
), )||
// 返回一个日期对象,供外部的formatter获取当前日期的年月日等信息,进行加工处理 //自己修改的--用于禁用最大和最小日期之间的日期
date: new Date(date), this.customList.indexOf(date) === -1,
bottomInfo, // 返回一个日期对象,供外部的formatter获取当前日期的年月日等信息,进行加工处理
dot: false, date: new Date(date),
month: bottomInfo,
dot: false,
month:
dayjs(minDate).add(i, 'month').month() + 1 dayjs(minDate).add(i, 'month').month() + 1
} }
const formatter = const formatter =
this.formatter || this.innerFormatter this.formatter || this.innerFormatter
return formatter(config) return formatter(config)
}), }),
// 当前所属的月份 // 当前所属的月份
month: dayjs(minDate).add(i, 'month').month() + 1, month: dayjs(minDate).add(i, 'month').month() + 1,
// 当前年份 // 当前年份
year: dayjs(minDate).add(i, 'month').year() year: dayjs(minDate).add(i, 'month').year()
}) })
} }
}, },
// 滚动到默认设置的月份 // 滚动到默认设置的月份
scrollIntoDefaultMonth(selected) { scrollIntoDefaultMonth(selected) {
// 查询默认日期在可选列表的下标 // 查询默认日期在可选列表的下标
const _index = this.months.findIndex(({ const _index = this.months.findIndex(({
year, year,
month month
}) => { }) => {
month = uni.$u.padZero(month) month = uni.$u.padZero(month)
return `${year}-${month}` === selected return `${year}-${month}` === selected
}) })
if (_index !== -1) { if (_index !== -1) {
// #ifndef MP-WEIXIN // #ifndef MP-WEIXIN
this.$nextTick(() => { this.$nextTick(() => {
this.scrollIntoView = `month-${_index}` this.scrollIntoView = `month-${_index}`
}) })
// #endif // #endif
// #ifdef MP-WEIXIN // #ifdef MP-WEIXIN
this.scrollTop = this.months[_index].top || 0; this.scrollTop = this.months[_index].top || 0
// #endif // #endif
} }
}, },
// scroll-view滚动监听 // scroll-view滚动监听
onScroll(event) { onScroll(event) {
// 不允许小于0的滚动值,如果scroll-view到顶了,继续下拉,会出现负数值 // 不允许小于0的滚动值,如果scroll-view到顶了,继续下拉,会出现负数值
const scrollTop = Math.max(0, event.detail.scrollTop) const scrollTop = Math.max(0, event.detail.scrollTop)
// 将当前滚动条数值,除以滚动区域的高度,可以得出当前滚动到了哪一个月份的索引 // 将当前滚动条数值,除以滚动区域的高度,可以得出当前滚动到了哪一个月份的索引
for (let i = 0; i < this.months.length; i++) { for (let i = 0; i < this.months.length; i++) {
if (scrollTop >= (this.months[i].top || this.listHeight)) { if (scrollTop >= (this.months[i].top || this.listHeight)) {
this.monthIndex = i this.monthIndex = i
} }
} }
}, },
// 更新月份的top值 // 更新月份的top值
updateMonthTop(topArr = []) { updateMonthTop(topArr = []) {
// 设置对应月份的top值,用于onScroll方法更新月份 // 设置对应月份的top值,用于onScroll方法更新月份
topArr.map((item, index) => { topArr.map((item, index) => {
this.months[index].top = item this.months[index].top = item
}) })
// 获取默认日期的下标 // 获取默认日期的下标
if (!this.defaultDate) { if (!this.defaultDate) {
// 如果没有设置默认日期,则将当天日期设置为默认选中的日期 // 如果没有设置默认日期,则将当天日期设置为默认选中的日期
const selected = dayjs().format("YYYY-MM") const selected = dayjs().format('YYYY-MM')
this.scrollIntoDefaultMonth(selected) this.scrollIntoDefaultMonth(selected)
return return
} }
let selected = dayjs().format("YYYY-MM"); let selected = dayjs().format('YYYY-MM')
// 单选模式,可以是字符串或数组,Date对象等 // 单选模式,可以是字符串或数组,Date对象等
if (!uni.$u.test.array(this.defaultDate)) { if (!uni.$u.test.array(this.defaultDate)) {
selected = dayjs(this.defaultDate).format("YYYY-MM") selected = dayjs(this.defaultDate).format('YYYY-MM')
} else { } else {
selected = dayjs(this.defaultDate[0]).format("YYYY-MM"); selected = dayjs(this.defaultDate[0]).format('YYYY-MM')
} }
this.scrollIntoDefaultMonth(selected) this.scrollIntoDefaultMonth(selected)
} }
} }
} }
</script> </script>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment