微信小程序的双时间选择器基于picker的mode = multiSelector
废了好大的劲写了这么一个组件,叉会腰~
其实这段代码并不精炼,不过本人也是个刚工作一年多的小菜鸟,就先放在这里,有想法再来改;
事实上我就在写这篇博文的时候,就修改了3个bug,就当给大家提供一个思路吧;
想到啥写啥,我觉得这么写比用picker-view的好处就是可以使用原生组件,只需要管自己想管的逻辑,不过写完发现逻辑部分好鸡儿多;
我认为这个组件主要的缺点在于每次滚动事件都重新计算年月日,如果能一次计算清楚年月日,只管显示,想必会简单不少,不过本人简单想了想没找到思路,就留待大神们解决啦;
由于我用的mpx框架,这段代码不能直接插入到原生小程序里,需要修改
html部分:
<picker
mode="multiSelector"
bindchange="bindMultiPickerChange"
bindcolumnchange="bindMultiPickerColumnChange"
value="{{multiIndex}}"
range="{{multiArray}}"
>
时间
</picker>
js部分:
<script>
const date = new Date();
const years = [];
const months = [];
const days = [];
const years2 = [];
const months2 = [];
const days2 = [];
const yearIndex = date.getFullYear()-2010
const monthIndex = date.getMonth()
const dayIndex = date.getDate() - 1
for (let i = 2010; i <= date.getFullYear() + 5 ; i++) {
years.push("" + i);
}
for (let i = 1; i <= 12; i++) {
if (i < 10) {
i = "0" + i;
}
months.push("" + i);
}
for (let i = 1; i <= 31; i++) {
if (i < 10) {
i = "0" + i;
}
days.push("" + i);
}
for (let i = date.getFullYear(); i <= date.getFullYear() + 5 ; i++) {
years2.push("" + i);
}
for (let i = date.getMonth()+1; i <= 12; i++) {
if (i < 10) {
i = "0" + i;
}
months2.push("" + i);
}
for (let i = date.getDate(); i <= 31; i++) {
if (i < 10) {
i = "0" + i;
}
days2.push("" + i);
}
createComponent({
data:{
time: '',
multiArray: [years, months, days,"-", years2, months2, days2],
multiIndex: [yearIndex,monthIndex , dayIndex,0, 0, 0,0],
},
methods: {
bindMultiPickerChange(e) {
this.multiIndex = e.detail.value
const index = this.multiIndex
const year = this.multiArray[0][index[0]]
const month = this.multiArray[1][index[1]]
const day = this.multiArray[2][index[2]]
const year2 = this.multiArray[4][index[4]]
const month2 = this.multiArray[5][index[5]]
const day2 = this.multiArray[6][index[6]]
this.time = year + '-' + month + '-' + day + ' 至 ' + year2 + '-' + month2 + '-' + day2
console.log(this.time)
this.triggerE()
},
bindMultiPickerColumnChange(e) {
const column = e.detail.column
const array = this.multiArray
const index = this.multiIndex
this.multiIndex[column] = e.detail.value
if(column == 0){
this.multiArray[2] = this.getday(1,array[1][index[1]],array[0][index[0]])
this.multiArray[4] = this.getyear(array[0][index[0]])
if(array[4].length<=index[4]){
this.multiIndex[4] = 0
}
if(array[1][index[1]] == array[5][index[5]]){
this.multiArray[6] = this.getday(index[2]+1,array[5][index[5]],array[4][index[4]])
}
}else if(column == 4){
if(array[0][index[0]] == array[4][index[4]] && array[1][index[1]] > array[5][index[5]]){
this.multiArray[5] = this.getmonth(index[1]+1)
if(array[1][index[1]] >= array[5][index[5]]){
this.multiArray[6] = this.getday(index[2]+1,array[5][index[5]],array[4][index[4]])
}else{
this.multiArray[6] = this.getday(1,array[5][index[5]],array[4][index[4]])
}
}else{
this.multiArray[5] = this.getmonth()
this.multiArray[6] = this.getday(1,array[5][index[5]],array[4][index[4]])
}
}else if(column == 1 || column == 5 ){
this.multiArray[column+1] = this.getday(1,array[column][index[column]],array[column-1][index[column-1]])
if(array[0][index[0]] == array[4][index[4]] ){
let monthi = parseInt(array[1][index[1]])
this.multiArray[5] = this.getmonth(monthi)
if(array[5].length<=index[5]){
this.multiIndex[5] = 0
}
let value = array[column][index[column]]
if((value == 4 || value == 6 || value == 9 || value == 11) && index[column+1] == 30 ){
this.multiIndex[2] = 29
this.multiIndex[6] = 29
}else if(value == 2 && index[column+1] > 27){
this.multiIndex[2] = 27
this.multiIndex[6] = 27
}
if(array[1][index[1]] == array[5][index[5]]){
this.multiArray[6] = this.getday(array[2][index[2]],array[5][index[5]],array[4][index[4]])
}
}
}else if(column == 2 && array[0][index[0]] == array[4][index[4]] && array[1][index[1]] == array[5][index[5]]){
this.multiArray[6] = this.getday(array[2][index[2]],array[1][index[1]],array[0][index[0]])
if(array[6].length<=index[6]){
this.multiIndex[6] = 0
}
}
},
getyear(e){
let year = e?e:2010
let years = []
for (let i = year; i <= new Date().getFullYear() + 5 ; i++) {
years.push("" + i);
}
return years
},
getmonth(e){
let month = e?e:1
let months = []
for (let i = month; i <= 12; i++) {
if (i < 10) {
i = "0" + i;
}
months.push("" + i);
}
return months
},
getday(d,m,y){
let dm = 31
if (m == 4 || m == 6 || m == 9 || m == 11){
dm = 30
if(d == 31){
d = 30
}
}else if( m == 2 ){
if(y&&((y % 400 == 0) || (y % 100 != 0)) && (y % 4 == 0)){
dm = 29
}else{
dm = 28
}
if(d > 28){
d = 28
}
}
let days = []
for (let i = d; i <= dm; i++) {
if (i < 10) {
i = "0" + i;
}
days.push("" + i);
}
return days
},
}
})
</script>
json部分: //再次在这里说明我使用的是mpx框架,和原生不太一样
<script type="application/json">
{
"component": true
}
</script>
css部分没有
ps:写这篇博文的时候才发现自己的第一篇博文没发出去… |