微信小程式—picker(滾動選擇器)
阿新 • • 發佈:2019-02-14
從底部彈起的滾動選擇器,現支援五種選擇器,通過mode來區分,分別是普通選擇器(selector),多列選擇器(multiSelector),時間選擇器(time),日期選擇器(date),省市區選擇器(region),預設是普通選擇器。
效果圖如下,
1.index.js中:
//index.js
//獲取應用例項
const app = getApp()
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;
console.log(data.multiIndex);
break;
}
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
})
}
})
2.index.wxml中:
<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>
3.index.wxss中:
.section__title {
margin: 20rpx;
}
.section {
width: 100%;
font-size: 35rpx;
margin: 10rpx;
color: #000;
}
.picker {
color: #6BD44E;
}
本人菜鳥一個,有什麼不對的地方希望大家指出評論,大神勿噴,希望大家一起學習進步!