1、当前需求,要求不超过当前日期
2、找到picker的源码位置==>uview-ui==>components==>u-picker==>u-picker.vue
在props里新增,
timeEnd:{
type:Object,
}
在watch监听里 添加调用函数 this.timeHandle()
watch: {
propsChange() {
this.reset = true;
setTimeout(() => this.init(), 10);
this.timeHandle()
},
// 如果地区发生变化,为了让picker联动起来,必须重置this.citys和this.areas
regionChange(val) {
this.citys = citys[this.province];
this.areas = areas[this.province][this.city];
},
// watch监听月份的变化,实时变更日的天数,因为不同月份,天数不一样
// 一个月可能有30,31天,甚至闰年2月的29天,平年2月28天
yearAndMonth(val) {
if (this.params.year) this.setDays();
this.timeHandle()
},
// 微信和QQ小程序由于一些奇怪的原因(故同时对所有平台均初始化一遍),需要重新初始化才能显示正确的值
value(n) {
if (n) {
this.reset = true;
setTimeout(() => this.init(), 10);
}
}
},
timeHandle(){
if(this.mode !== 'time') return;
if(this.timeEnd.year) this.endYear = this.timeEnd.year;
if(this.timeEnd.month) {
if(this.endYear === this.year){
this.setMonths(this.timeEnd.month)
}else{
this.setMonths()
}
}
if(this.timeEnd.day) {
if(this.endYear === this.year){
this.setDays(this.timeEnd.day)
}else{
this.setDays()
}
}
},
//下面两个函数复制替换
setMonths(num=12) {
// this.months = this.generateArray(1, 12);
this.months = this.generateArray(1, num);
this.valueArr.splice(this.valueArr.length - 1, 1, this.getIndex(this.months, this.month));
},
setDays(num) {
let totalDays = new Date(this.year, this.month, 0).getDate();
if(this.month != (new Date().getMonth() +1)){
this.days = this.generateArray(1,totalDays)
}else{
this.days = this.generateArray(1, num?num:totalDays);
}
let index = 0;
// 这里不能使用类似setMonths()中的this.valueArr.splice(this.valueArr.length - 1, xxx)做法
// 因为this.month和this.year变化时,会触发watch中的this.setDays(),导致this.valueArr.length计算有误
if (this.params.year && this.params.month) index = 2;
else if (this.params.month) index = 1;
else if (this.params.year) index = 1;
else index = 0;
// 当月份变化时,会导致日期的天数也会变化,如果原来选的天数大于变化后的天数,则重置为变化后的最大值
// 比如原来选中3月31日,调整为2月后,日期变为最大29,这时如果day值继续为31显然不合理,于是将其置为29(picker-column从1开始)
if(this.day > this.days.length) this.day = this.days.length;
this.valueArr.splice(index, 1, this.getIndex(this.days, this.day));
},
在init()方法中任意处添加一行
this.timeHandle()
使用方法
<view class="date" @click="show = true">
当前日期:{{date}}
<u-picker mode="time" v-model="show" :params="params" :timeEnd="endTime" @confirm="confirm"></u-picker>
</view>
export default {
data() {
return {
detailList: [],
dataObj: {},
time: '',
id: '',
show: false,
params: {
year: true,
month: true,
day: true,
hour: false,
minute: false,
second: false
},
endTime: {},
onLoad(e) {
let time = new Date()
let year = time.getFullYear()
let month = (time.getMonth() + 1) < 10 ? "0" + (time.getMonth() + 1) : (time.getMonth() + 1)
let month1 = (time.getMonth()) < 10 ? "0" + (time.getMonth()) : (time.getMonth())
let day = (time.getDate()) < 10 ? "0" + (time.getDate()) : (time.getDate())
this.endTime.year = new Date().getFullYear()
this.endTime.month = new Date().getMonth() + 1
this.endTime.day = new Date().getDate()
this.date = `${year}-${month}-${day}`
if (e) {
this.id = e.id
}
this.getList()
this.systemPhone = uni.getSystemInfoSync().platform;
console.log(this.systemPhone)
if (this.systemPhone == 'ios') {
this.date = `${year}/${month}/${day}`
} else {
this.date = `${year}-${month}-${day}`
}
},
至此,成功解决。