微信小程序自定义时间段picker选择器_第1页
微信小程序自定义时间段picker选择器_第2页
微信小程序自定义时间段picker选择器_第3页
微信小程序自定义时间段picker选择器_第4页
微信小程序自定义时间段picker选择器_第5页
已阅读5页,还剩10页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第微信小程序自定义时间段picker选择器本文实例为大家分享了微信小程序自定义时间段picker选择器的具体代码,供大家参考,具体内容如下

想实现一个可以选择年份和时间段的日期选择器,如下所示

微信小程序自带的picker组件虽然能实现如上的内容,但不能实现样式的修改,不太符合小程序的设计主题,所以考虑了以下两种方法来实现如上的设计。

1.自定义date-picker

把要实现的date-picker封装为一个组件,组件内套用小程序自带的picker-view组件,其中picker_view-column表示不同的选择列,这样可以方便地实现样式的自定义。

实现效果:

具体实现

wxml文件:

viewwx:if="{{isShow}}"catchtap="cancel"

viewanimation="{{animation}}"

view

viewhover-catchtap="cancel"取消/view

viewhover-catchtap="confirm"确定/view

/view

picker-viewvalue="{{value}}"bindchange="change"catchtap="no"

picker-view-column

viewwx:for="{{date_list}}"wx:key="date_list"{{item}}/view

/picker-view-column

picker-view-column

viewwx:for="{{time_list}}"wx:key="time_list"{{item}}/view

/picker-view-column

/picker-view

/view

/view

wxss文件:

.mask{

position:fixed;

width:100%;

height:100%;

left:0;

right:0;

top:0;

bottom:0;

display:flex;

background-color:rgba(0,0,0,0.7);

z-index:9999;

flex-direction:column;

justify-content:flex-end;

.content{

display:flex;

flex-direction:column;

width:100%;

background:white;

border-top-right-radius:20rpx;

border-top-left-radius:20rpx;

.top{

display:flex;

flex-direction:row;

justify-content:space-between;

align-items:center;

height:100rpx;

border-bottom:1rpxsolid#d3cfcf;

.top-text{

font-size:30rpx;

width:150rpx;

height:100rpx;

display:flex;

flex-direction:row;

justify-content:center;

align-items:center;

.top-left-color{

color:#878787;

.top-left-color-hover{

color:#f1eaea;

.top-right-color{

color:#1296DB;

.top-right-color-hover{

color:#82ccf3;

.item{

width:100%;

align-items:center;

justify-content:center;

display:flex;

flex-direction:row;

font-size:18px;

}

Component({

/**

*组件的属性列表

*/

properties:{

range:{//可预约的日期范围。默认日期从今天开始,到第range天后为止,这里设为10天

type:Number,

value:10

},

start_time:{//开始时间,设为整点

type:Number,

value:8

},

step:{//预约时间的步长,设置为30,表示30分钟

type:Number

},

end_time:{//结束时间,设为整点

type:Number,

value:22

}

},

/**

*组件的初始数据

*/

data:{

isShow:false,

selectDate:"",

dialogh:0,

//日期列表和时间列表

date_list:[],

time_list:[]

},

attached:function(){

letstart_day=this.ts_string(newDate().getTime());

console.log(start_day);//2025-08-31

console.log(newDate());

letend_day=this.ts_string(newDate().setDate(newDate().getDate()+perties.range))

//获取日期列表

letdate_list=this.getDiffDate(start_day,end_day);

//获取时间列表

lettime_list=this.getTimeList(perties.start_time,perties.end_time,perties.step);

console.log(time_list);

this.setData({

//date_time:[date_column,time_column],

date_list:date_list,

time_list:time_list,

})

//动画

this.animation=wx.createAnimation({

duration:300

})

//500rpx转成px

letdialoghpx=800/750*wx.getSystemInfoSync().windowWidth

this.setData({

dialogh:dialoghpx,

selectDate:this.data.date_list[0]+this.data.time_list[0]

})

},

methods:{

getDiffDate(start,end){

letstartTime=newDate(start);

letendTime=newDate(end);

letdateArr=[];

while((endTime.getTime()-startTime.getTime())=0){

dateArr.push(this.ts_string(startTime.getTime()));

startTime.setDate(startTime.getDate()+1);

}

returndateArr;

},

zfill(num,length){

return(Array(length).join('0')+num).slice(-length);

},

//把日期转换成xxxx-xx-xx的形式

ts_string(timestamp){

letd=newDate(timestamp);

letday="";

switch(d.getDay()){

case1:

day="周一";

break;

case2:

day="周二";

break;

case3:

day="周三";

break;

case4:

day="周四";

break;

case5:

day="周五";

break;

case6:

day="周六";

break;

case0:

day="周日";

break;

}

letstring=(d.getFullYear())+"-"+

this.zfill((d.getMonth()+1),2)+"-"+

this.zfill((d.getDate()),2)+"("+day+")"

returnstring

},

//获取时间区间列表,输入(起始时间,结束时间,步长)

getTimeList(start,end,step){

letstart_time=newDate();

//设置起始时间

start_time.setHours(start,0,0);

console.log(start_time);

//设置结束时间

letend_time=newDate();

end_time.setHours(end,0,0);

letstartG=start_time.getTime();//起始时间的格林时间

letendG=end_time.getTime();//起始时间的格林时间

letstep_ms=step*60*1000;

lettimeArr=[];

while(startGendG){

lettime=this.timeAdd(startG,step_ms);

timeArr.push(time);

startG+=step_ms;

}

returntimeArr;

},

timeAdd(time1,add){

varnd=newDate(time1);//创建时间对象

//获取起始时间的时分秒

varhh1=nd.getHours();

varmm1=nd.getMinutes();

if(hh1=9)hh1="0"+hh1;

if(mm1=9)mm1="0"+mm1;

nd=nd.valueOf();//转换为毫秒数

nd=nd+Number(add);

nd=newDate(nd);

varhh2=nd.getHours();

varmm2=nd.getMinutes();

if(hh2=9)hh2="0"+hh2;

if(mm2=9)mm2="0"+mm2;

vartime=hh1+":"+mm1+"-"+hh2+":"+mm2;

returntime;//时间段

},

change:function(e){

constval=e.detail.value;

//val[0]表示选择的第一列序号,val[1]表示选择的第二列序号

letselect=this.data.date_list[val[0]]+this.data.time_list[val[1]]

console.log(select);

this.setData({

selectDate:select

})

},

showDialog(){

this.setData({

isShow:true

})

//先向下移动dialog高度,然后恢复原位从而形成从下向上弹出效果

this.animation.translateY(this.data.dialogh).translateY(0).step()

this.setData({

animation:this.animation.export()

})

},

dimsss(){

//从原位向下移动dailog高度,形成从上向下的收起效果

this.animation.translateY(this.data.dialogh).step()

this.setData({

animation:this.animation.export()

})

//动画结束后蒙层消失

setTimeout(()={

this.setData({

isShow:false

})

},300)

},

cancel(){

this.triggerEvent("cancel")

this.dimsss()

},

confirm(){

this.triggerEvent("confirm",{

selectDate:this.data.selectDate

})

this.dimsss()

}

}

})

组件的使用

想在父组件中使用封装好的date-picker组件,先要在父组件的json文件中声明。

{

"usingComponents":{

"date-picker":"../../components/date-picker/date-picker"

}

如果想实现简单的选择时间段并在页面中显示的功能,父组件代码如下编写即可。

父组件wxml文件:

viewbindtap="timeOpen"

{{selectDate}}

/view

date-pickerid="picker"range="8"step="40"bindconfirm="confirm"/date-picker

父组件js文件:

Page({

data:{

selectDate:"",

machineShow:false,

},

onLoad:function(){

this.picker=this.selectComponent("#picker")

},

timeOpen(){

this.picker.showDialog();

},

confirm(e){

this.setData({

selectDate:e.detail.selectDate

})

},

})

2.结合vantweapp的date-picker

自定义定义的date-picker组件已经可以实现想实现的功能,但自定义的组件样式不够美观。这时,我注意到了小程序可以使用vant的组件库,组件库多列选择器的样式可以自由修改,而且自带样式已经足够美观,所以下面考虑结合vant组件库实现date-picker。

实现效果

具体实现

使用vantweapp的picker组件和popup组件可以更简洁地实现想要的效果,打造自定义的date-picker组件。

首先需要在编写组件的json文件中导入vantweapp的相应组件

{

"component":true,

"usingComponents":{

"van-picker":"@vant/weapp/picker/index",

"van-popup":"@vant/weapp/popup/index"

}

}

然后编写date-picker的wxml文件:

van-popuproundshow="{{isShow}}"bind:close="cancel"position="bottom"custom-

van-pickershow-toolbar

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论