(精华)2020年7月30日 微信小程序 选择器的使用

picker 从底部弹起的滚动选择器

<view class="section">
  <view class="section__title">普通选择器</view>
  <picker bindchange="bindPickerChange" value="{
     {index}}" range="{
     {array}}">
    <view class="picker">
      当前选择:{
     {
     array[index]}}
    </view>
  </picker>
</view>
<view class="section">
  <view class="section__title">多列选择器</view>
  <picker mode="multiSelector" bindchange="bindMultiPickerChange" bindcolumnchange="bindMultiPickerColumnChange" value="{
     {multiIndex}}" range="{
     {multiArray}}">
    <view class="picker">
      当前选择:{
     {
     multiArray[0][multiIndex[0]]}}{
     {
     multiArray[1][multiIndex[1]]}}{
     {
     multiArray[2][multiIndex[2]]}}
    </view>
  </picker>
</view>
<view class="section">
  <view class="section__title">时间选择器</view>
  <picker mode="time" value="{
     {time}}" start="09:01" end="21:01" bindchange="bindTimeChange">
    <view class="picker">
      当前选择: {
     {
     time}}
    </view>
  </picker>
</view>

<view class="section">
  <view class="section__title">日期选择器</view>
  <picker mode="date" value="{
     {date}}" start="2015-09-01" end="2017-09-01" bindchange="bindDateChange">
    <view class="picker">
      当前选择: {
     {
     date}}
    </view>
  </picker>
</view>
<view class="section">
  <view class="section__title">省市区选择器</view>
  <picker mode="region" bindchange="bindRegionChange" value="{
     {region}}" custom-item="{
     {customItem}}">
    <view class="picker">
      当前选择:{
     {
     region[0]}}{
     {
     region[1]}}{
     {
     region[2]}}
    </view>
  </picker>
</view>
Page({
     
  data: {
     
    array: ['美国', '中国', '巴西', '日本'],
    objectArray: [
      {
     
        id: 0,
        name: '美国'
      },
      {
     
        id: 1,
        name: '中国'
      },
      {
     
        id: 2,
        name: '巴西'
      },
      {
     
        id: 3,
        name: '日本'
      }
    ],
    index: 0,
    multiArray: [['无脊柱动物', '脊柱动物'], ['扁性动物', '线形动物', '环节动物', '软体动物', '节肢动物'], ['猪肉绦虫', '吸血虫']],
    objectMultiArray: [
      [
        {
     
          id: 0,
          name: '无脊柱动物'
        },
        {
     
          id: 1,
          name: '脊柱动物'
        }
      ], [
        {
     
          id: 0,
          name: '扁性动物'
        },
        {
     
          id: 1,
          name: '线形动物'
        },
        {
     
          id: 2,
          name: '环节动物'
        },
        {
     
          id: 3,
          name: '软体动物'
        },
        {
     
          id: 3,
          name: '节肢动物'
        }
      ], [
        {
     
          id: 0,
          name: '猪肉绦虫'
        },
        {
     
          id: 1,
          name: '吸血虫'
        }
      ]
    ],
    multiIndex: [0, 0, 0],
    date: '2016-09-01',
    time: '12:01',
    region: ['广东省', '广州市', '海珠区'],
    customItem: '全部'
  },
  bindPickerChange: function(e) {
     
    console.log('picker发送选择改变,携带值为', e.detail.value)
    this.setData({
     
      index: e.detail.value
    })
  },
  bindMultiPickerChange: function (e) {
     
    console.log('picker发送选择改变,携带值为', e.detail.value)
    this.setData({
     
      multiIndex: e.detail.value
    })
  },
  bindMultiPickerColumnChange: function (e) {
     
    console.log('修改的列为', e.detail.column, ',值为', e.detail.value);
    var data = {
     
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    };
    data.multiIndex[e.detail.column] = e.detail.value;
    switch (e.detail.column) {
     
      case 0:
        switch (data.multiIndex[0]) {
     
          case 0:
            data.multiArray[1] = ['扁性动物', '线形动物', '环节动物', '软体动物', '节肢动物'];
            data.multiArray[2] = ['猪肉绦虫', '吸血虫'];
            break;
          case 1:
            data.multiArray[1] = ['鱼', '两栖动物', '爬行动物'];
            data.multiArray[2] = ['鲫鱼', '带鱼'];
            break;
        }
        data.multiIndex[1] = 0;
        data.multiIndex[2] = 0;
        break;
      case 1:
        switch (data.multiIndex[0]) {
     
          case 0:
            switch (data.multiIndex[1]) {
     
              case 0:
                data.multiArray[2] = ['猪肉绦虫', '吸血虫'];
                break;
              case 1:
                data.multiArray[2] = ['蛔虫'];
                break;
              case 2:
                data.multiArray[2] = ['蚂蚁', '蚂蟥'];
                break;
              case 3:
                data.multiArray[2] = ['河蚌', '蜗牛', '蛞蝓'];
                break;
              case 4:
                data.multiArray[2] = ['昆虫', '甲壳动物', '蛛形动物', '多足动物'];
                break;
            }
            break;
          case 1:
            switch (data.multiIndex[1]) {
     
              case 0:
                data.multiArray[2] = ['鲫鱼', '带鱼'];
                break;
              case 1:
                data.multiArray[2] = ['青蛙', '娃娃鱼'];
                break;
              case 2:
                data.multiArray[2] = ['蜥蜴', '龟', '壁虎'];
                break;
            }
            break;
        }
        data.multiIndex[2] = 0;
        break;
    }
    console.log(data.multiIndex);
    this.setData(data);
  },
  bindDateChange: function(e) {
     
    console.log('picker发送选择改变,携带值为', e.detail.value)
    this.setData({
     
      date: e.detail.value
    })
  },
  bindTimeChange: function(e) {
     
    console.log('picker发送选择改变,携带值为', e.detail.value)
    this.setData({
     
      time: e.detail.value
    })
  },
  bindRegionChange: function (e) {
     
    console.log('picker发送选择改变,携带值为', e.detail.value)
    this.setData({
     
      region: e.detail.value
    })
  }
})

picker-view 嵌入页面的滚动选择器

<view class="container">
  <view class="page-body">
    <view class="selected-date">{
     {
     year}}{
     {
     month}}{
     {
     day}}{
     {
     isDaytime ? "白天" : "夜晚"}}</view>
    <picker-view indicator-style="height: 50px;" style="width: 100%; height: 300px;" value="{
     {value}}" bindchange="bindChange">
      <picker-view-column>
        <view wx:for="{
     {years}}" wx:key="{
     {years}}" style="line-height: 50px; text-align: center;">{
     {
     item}}</view>
      </picker-view-column>
      <picker-view-column>
        <view wx:for="{
     {months}}" wx:key="{
     {months}}" style="line-height: 50px; text-align: center;">{
     {
     item}}</view>
      </picker-view-column>
      <picker-view-column>
        <view wx:for="{
     {days}}" wx:key="{
     {days}}" style="line-height: 50px; text-align: center;">{
     {
     item}}</view>
      </picker-view-column>
      <picker-view-column>
        <view class="icon-container">
          <image class="picker-icon" src="../lib/daytime.png" />
        </view>
        <view class="icon-container">
          <image class="picker-icon" src="../lib/night.png" />
        </view>
      </picker-view-column>
    </picker-view>
  </view>

</view>
const date = new Date()
const years = []
const months = []
const days = []

for (let i = 1990; i <= date.getFullYear(); i++) {
     
  years.push(i)
}

for (let i = 1; i <= 12; i++) {
     
  months.push(i)
}

for (let i = 1; i <= 31; i++) {
     
  days.push(i)
}

Page({
     
  onShareAppMessage() {
     
    return {
     
      title: 'picker-view',
      path: 'page/component/pages/picker-view/picker-view'
    }
  },

  data: {
     
    years,
    year: date.getFullYear(),
    months,
    month: 2,
    days,
    day: 2,
    value: [9999, 1, 1],
    isDaytime: true,
  },

  bindChange(e) {
     
    const val = e.detail.value
    this.setData({
     
      year: this.data.years[val[0]],
      month: this.data.months[val[1]],
      day: this.data.days[val[2]],
      isDaytime: !val[3]
    })
  }
})

slider 滑动选择器

var pageData = {
     }
for (var i = 1; i < 5; ++i) {
     
  (function (index) {
     
    pageData[`slider${
       index}change`] = function (e) {
     
      console.log(`slider${
       index}发生change事件,携带值为`, e.detail.value)
    }
  })(i);
}
Page(pageData)
<view class="page">
    <view class="page__hd">
        <text class="page__title">slider</text>
        <text class="page__desc">滑块</text>
    </view>
    <view class="page__bd">
        <view class="section section_gap">
            <text class="section__title">设置left/right icon</text>
            <view class="body-view">
                <slider bindchange="slider1change" left-icon="cancel" right-icon="success_no_circle"/>
            </view>
        </view>

        <view class="section section_gap">
            <text class="section__title">设置step</text>
            <view class="body-view">
                <slider bindchange="slider2change" step="5"/>
            </view>
        </view>

        <view class="section section_gap">
            <text class="section__title">显示当前value</text>
            <view class="body-view">
                <slider bindchange="slider3change" show-value/>
            </view>
        </view>

        <view class="section section_gap">
            <text class="section__title">设置最小/最大值</text>
            <view class="body-view">
                <slider bindchange="slider4change" min="50" max="200" show-value/>
            </view>
        </view>
    </view>
</view>

你可能感兴趣的:(#,微信小程序,小程序)