商城系統(tǒng) 注冊

小程序日期時間選擇器如何實現

2020-09-27|HiShop
導讀:小程序日期時間選擇器是需要自定義實現的,一起來看看怎么實現的呢?...

  

  小程序日期時間選擇器是需要自定義實現的,一起來看看怎么實現的呢?

  要用到picker組件,動畫從底部彈起的滾動選擇器,現支持三種選擇器,通過mode來區(qū)分,分別是普通選擇器,時間選擇器,日期選擇器,默認是普通選擇器。

  看下相應的屬性:

 ?、倨胀ㄟx擇器

  選擇器用mode來區(qū)別,默認是普通選擇器,e.detail.value拿到的值是選擇了項的索引index,再通過array拿到值.在data里面做初始化的時候,將備選項加入array即可.

小程序日期時間選擇器如何實現

  選擇時觸發(fā)bindPickerChange事件,獲取index.

 ?、跁r間選擇器

  mode = time時,是時間選擇器.start,end分別是有效時間范圍的開始和結束.格式hh:mm

  選擇時觸發(fā)bindTimeChange事件,獲取time.

 ?、廴掌谶x擇器

  mode = date時,是時間選擇器.start,end分別是有效日期范圍的開始和結束.格式y(tǒng)yyy-MM-dd

  選擇時觸發(fā)bindDateChange事件,獲取date

  具體的來看看代碼,布局:

 

<view class="section" >  
  <picker bindchange="bindPickerChange" value="{{index}}" range="{{objectArray}}" mode = "selector">  
    <view class="picker">  
      國家:{{objectArray[index]}}  
    </view>  
  </picker>  
</view>  

<view class="section">  
  <picker mode="time" value="{{time}}" start="00:00" end="23:59" bindchange="bindTimeChange">  
    <view class="picker">  
     時間 : {{times}}
    </view>  
  </picker>  
</view>  
<view class="section">  
  <picker mode="date" value="{{date}}" start="1978-01-01" end="2017-1-23" bindchange="bindDateChange">  
    <view class="picker">  
      日期: {{dates}}  
    </view>  
  </picker>  
</view>  

css樣式:

.section{
   background:#CABBC7;
   margin:20rpx;
   padding:20rpx

js代碼:
Page({
  data: {
    dates: '2016-11-08',
    times: '12:00',
    objectArray: ['中國', '英國', '美國'],
    index: 0,
  },
  //  點擊時間組件確定事件  
  bindTimeChange: function (e) {
    console.log("誰哦按")
    this.setData({
      times: e.detail.value
    })
  },
  //  點擊日期組件確定事件  
  bindDateChange: function (e) {
     console.log(e.detail.value)
    this.setData({
      dates: e.detail.value
    })
  },
  //  點擊城市組件確定事件  
  bindPickerChange: function (e) {
     console.log(e.detail.value)
    this.setData({
      index: e.detail.value
    })
  }

代碼很簡單,分別綁定事件,點擊切換就Ok。


 

HiShop小程序工具提供多類型商城/門店小程序制作,可視化編輯 1秒生成5步上線。通過拖拽、拼接模塊布局小程序商城頁面,所看即所得,只需要美工就能做出精美商城。

更多小程序資訊,盡在:descansotropical.com/xiaocx/

電話咨詢 預約演示 0元開店