picker.md 10.4 KB
Newer Older
M
mehaotian 已提交
1 2
#### picker

W
wanganxp 已提交
3
从底部弹起的滚动选择器。支持五种选择器,通过mode来区分,分别是普通选择器,多列选择器,时间选择器,日期选择器,省市区选择器,默认是普通选择器。
M
mehaotian 已提交
4

W
wanganxp 已提交
5
#### 普通选择器
M
mehaotian 已提交
6 7 8 9 10

``mode = selector``

**属性说明**

Q
qiang 已提交
11 12 13 14 15
|属性名|类型|默认值|说明|平台差异说明|
|:-|:-|:-|:-|:-|
|range|Array / Array<Object>|[]|mode为 selector 或 multiSelector 时,range 有效||
|range-key|String||当 range 是一个 Array<Object> 时,通过 range-key 来指定 Object 中 key 的值作为选择器显示内容||
|value|Number|0|value 的值表示选择了 range 中的第几个(下标从 0 开始)||
Q
qiang 已提交
16
|selector-type|String|auto|大屏时UI类型,支持 picker、select、auto,默认在 iPad 以 picker 样式展示而在 PC 以 select 样式展示|H5 2.9.9+|
Q
qiang 已提交
17 18 19
|@change|EventHandle||value 改变时触发 change 事件,event.detail = {value: value}||
|disabled|Boolean|false|是否禁用||
|@cancel|EventHandle||取消选择或点遮罩层收起 picker 时触发||
M
mehaotian 已提交
20

Q
qiang 已提交
21
- picker在各平台的实现是有UI差异的,有的平台如百度、支付宝小程序的Android端是从中间弹出的;有的平台支持循环滚动如百度小程序;有的平台没有取消按钮如App-iOS端。但均不影响功能使用。
M
mehaotian 已提交
22

W
wanganxp 已提交
23
#### 多列选择器
M
mehaotian 已提交
24 25 26 27 28

``mode = multiSelector``

**平台差异说明**

29
|App|H5|微信小程序|支付宝小程序|百度小程序|字节跳动小程序|QQ小程序
W
wanganxp 已提交
30 31
|:-:|:-:|:-:|:-:|:-:|:-:|:-:|
|vue支持,nvue自2.4起支持|√|√|x|√|√|√|
M
mehaotian 已提交
32

W
wanganxp 已提交
33
支付宝小程序 picker 组件不支持多列选择,可以使用 picker-view 组件替代。
M
mehaotian 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46

**属性说明**

|属性名|类型|默认值|说明|
|:-|:-|:-|:-|
|range|二维 Array / 二维 Array<Object>|[]|mode为 selector 或 multiSelector 时,range 有效。二维数组,长度表示多少列,数组的每项表示每列的数据,如[["a","b"], ["c","d"]]|
|range-key|String||当 range 是一个二维 Array<Object> 时,通过 range-key 来指定 Object 中 key 的值作为选择器显示内容|
|value|Array|[]|value 每一项的值表示选择了 range 对应项中的第几个(下标从 0 开始)|
|@change|EventHandle||value 改变时触发 change 事件,event.detail = {value: value}|
|@columnchange|EventHandle||某一列的值改变时触发 columnchange 事件,event.detail = {column: column, value: value},column 的值表示改变了第几列(下标从0开始),value 的值表示变更值的下标|
|@cancel|EventHandle||取消选择时触发|
|disabled|Boolean|false|是否禁用|

W
wanganxp 已提交
47
**bug & tips**
M
mehaotian 已提交
48 49 50
- 由于 JavaScript 的限制 vue 不能观测如下方式设置 value:``this.value[0] = 0``[vue 注意事项](https://cn.vuejs.org/v2/guide/list.html#注意事项)),解决方式参考:[hello-uniapp 示例](https://github.com/dcloudio/hello-uniapp/commit/59264474172a591c865431d02a2a1e3583978827)
- 微信开发工具的pc模拟器有可能出现拖动数据错乱,使用真机正常

W
wanganxp 已提交
51
#### 时间选择器
M
mehaotian 已提交
52 53 54 55 56

``mode = time``

**平台差异说明**

57
|App|H5|微信小程序|支付宝小程序|百度小程序|字节跳动小程序|QQ小程序|
W
wanganxp 已提交
58
|:-:|:-:|:-:|:-:|:-:|:-:|:-:|
W
wanganxp 已提交
59
|√|√|√|√|√|√|√|
W
wanganxp 已提交
60

W
wanganxp 已提交
61
- 时间选择在App端调用的是os的原生时间选择控件,在不同平台有不同的ui表现
M
mehaotian 已提交
62 63 64 65 66 67

**属性说明**

|属性名|类型|默认值|说明|平台差异说明|
|:-|:-|:-|:-|:-|
|value|String||表示选中的时间,格式为"hh:mm"||
雪洛's avatar
雪洛 已提交
68 69
|start|String||表示有效时间范围的开始,字符串格式为"hh:mm"|App 不支持|
|end|String||表示有效时间范围的结束,字符串格式为"hh:mm"|App 不支持|
M
mehaotian 已提交
70 71 72 73
|@change|EventHandle||value 改变时触发 change 事件,event.detail = {value: value}||
|@cancel|EventHandle||取消选择时触发||
|disabled|Boolean|false|是否禁用| |

W
wanganxp 已提交
74
#### 日期选择器
M
mehaotian 已提交
75 76 77 78 79

``mode = date``

**平台差异说明**

80
|App|H5|微信小程序|支付宝小程序|百度小程序|字节跳动小程序|QQ小程序|
W
wanganxp 已提交
81
|:-:|:-:|:-:|:-:|:-:|:-:|:-:|
W
wanganxp 已提交
82
|√|√|√|√|√|√|√|
W
wanganxp 已提交
83

Q
qiang 已提交
84
日期选择默认在App端和H5端(PC版Chrome以及PC版FireFox)调用的是os的原生日期选择控件,在不同平台有不同的ui表现,当配置fields参数后使用统一的展示方式。
M
mehaotian 已提交
85 86 87 88 89 90 91 92

**属性说明**

|属性名|类型|默认值|说明|平台差异说明|
|:-|:-|:-|:-|:-|
|value|String|0|表示选中的日期,格式为"YYYY-MM-DD"||
|start|String||表示有效日期范围的开始,字符串格式为"YYYY-MM-DD"||
|end|String||表示有效日期范围的结束,字符串格式为"YYYY-MM-DD"||
折腾笔记 已提交
93
|fields|String|day|有效值 year、month、day,表示选择器的粒度,默认为 day,App 端未配置此项时使用系统 UI|H5、App 2.6.3+、微信小程序、百度小程序、字节跳动小程序|
M
mehaotian 已提交
94 95 96 97 98 99 100 101 102 103 104 105
|@change|EventHandle||value 改变时触发 change 事件,event.detail = {value: value}||
|@cancel|EventHandle||取消选择时触发||
|disabled|Boolean|false|是否禁用| |

**fields 有效值**

|值|说明|
|:-|:-|
|year|选择器粒度为年|
|month|选择器粒度为月份|
|day|选择器粒度为天|

W
wanganxp 已提交
106
#### 省市区选择器
M
mehaotian 已提交
107 108 109 110 111

``mode = region``

**平台差异说明**

112
|App|H5|微信小程序|支付宝小程序|百度小程序|字节跳动小程序|QQ小程序|
W
wanganxp 已提交
113 114
|:-:|:-:|:-:|:-:|:-:|:-:|:-:|
|x|x|√|x|√|√|√|
M
mehaotian 已提交
115

W
wanganxp 已提交
116
- 因省市区选择器包含大量数据,占用体积,并非所有应用都需要,且很多城市数据有自维护需求,所以在App和H5平台没有内置。可以基于多列picker或picker-view,自行填充城市数据,插件市场有较多类似插件,[详见](https://ext.dcloud.net.cn/search?q=%E5%9F%8E%E5%B8%82%E9%80%89%E6%8B%A9)。注意基于多列picker方式的地区选择不能运行在支付宝小程序上,只有基于picker-view的可以全端运行。这些插件中,比较推荐的是[SimpleJalon的地址联动选择插件](https://ext.dcloud.net.cn/plugin?id=1084),它可以同时兼容app-nvue、app-vue、h5、及各端小程序。
M
mehaotian 已提交
117 118 119 120 121 122 123 124 125

|属性名|类型|默认值|说明|
|:-|:-|:-|:-|
|value|Array|[]|表示选中的省市区,默认选中每一列的第一个值|
|custom-item|String||可为每一列的顶部添加一个自定义的项|
|@change|EventHandle||value 改变时触发 change 事件,event.detail = {value: value}|
|@cancel|EventHandle||取消选择时触发|
|disabled|Boolean|false|是否禁用|

雪洛's avatar
雪洛 已提交
126
**示例** [查看演示](https://hellouniapp.dcloud.net.cn/pages/component/picker/picker)
M
mehaotian 已提交
127
 
W
wanganxp 已提交
128
以下示例代码,来自于[hello uni-app项目](https://github.com/dcloudio/hello-uniapp),推荐使用HBuilderX,新建uni-app项目,选择hello uni-app模板,可直接体验完整示例。
M
mehaotian 已提交
129
```html
W
wanganxp 已提交
130
<!-- 本示例未包含完整css,获取外链css请参考上文,在hello uni-app项目中查看 -->
M
mehaotian 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
<template>
	<view>
		<view class="uni-title uni-common-pl">地区选择器</view>
		<view class="uni-list">
			<view class="uni-list-cell">
				<view class="uni-list-cell-left">
					当前选择
				</view>
				<view class="uni-list-cell-db">
					<picker @change="bindPickerChange" :value="index" :range="array">
						<view class="uni-input">{{array[index]}}</view>
					</picker>
				</view>
			</view>
		</view>

		<view class="uni-title uni-common-pl">时间选择器</view>
		<view class="uni-list">
			<view class="uni-list-cell">
				<view class="uni-list-cell-left">
					当前选择
				</view>
				<view class="uni-list-cell-db">
					<picker mode="time" :value="time" start="09:01" end="21:01" @change="bindTimeChange">
						<view class="uni-input">{{time}}</view>
					</picker>
				</view>
			</view>
		</view>

		<view class="uni-title uni-common-pl">日期选择器</view>
		<view class="uni-list">
			<view class="uni-list-cell">
				<view class="uni-list-cell-left">
					当前选择
				</view>
				<view class="uni-list-cell-db">
					<picker mode="date" :value="date" :start="startDate" :end="endDate" @change="bindDateChange">
						<view class="uni-input">{{date}}</view>
					</picker>
				</view>
			</view>
		</view>
	</view>
</template>
```
```javascript
export default {
    data() {
        const currentDate = this.getDate({
            format: true
        })
        return {
            title: 'picker',
            array: ['中国', '美国', '巴西', '日本'],
            index: 0,
            date: currentDate,
            time: '12:01'
        }
    },
    computed: {
        startDate() {
            return this.getDate('start');
        },
        endDate() {
            return this.getDate('end');
        }
    },
    methods: {
        bindPickerChange: function(e) {
            console.log('picker发送选择改变,携带值为', e.target.value)
            this.index = e.target.value
        },
        bindDateChange: function(e) {
            this.date = e.target.value
        },
        bindTimeChange: function(e) {
            this.time = e.target.value
        },
        getDate(type) {
            const date = new Date();
            let year = date.getFullYear();
            let month = date.getMonth() + 1;
            let day = date.getDate();

            if (type === 'start') {
                year = year - 60;
            } else if (type === 'end') {
                year = year + 2;
            }
            month = month > 9 ? month : '0' + month;;
            day = day > 9 ? day : '0' + day;
            return `${year}-${month}-${day}`;
        }
    }
}
```

![uniapp](https://img-cdn-qiniu.dcloud.net.cn/uniapp/doc/img/picker.png?t=201857)

示例代码说明:以上示例代码从hello uni-app示例中复制,涉及的css样式在hello uni-app的app.vue和uni.css中

雪洛's avatar
雪洛 已提交
233
预览H5效果:使用浏览器的手机模式访问[https://hellouniapp.dcloud.net.cn/pages/component/picker/picker](https://hellouniapp.dcloud.net.cn/pages/component/picker/picker)
M
mehaotian 已提交
234 235 236

**注意**
- 在picker内容还在滚动时或滚动回弹动画还未结束时,点确定关闭弹出的picker,数据无法及时更新。需等待一下,或手动触停滚动再点确定。所有平台均如此
W
wanganxp 已提交
237
- 如果需要在PC端使用`picker`,需注意pc端没有touchmove事件,可以配置[H5模版](https://uniapp.dcloud.io/collocation/manifest?id=h5-template),并引入[touch-emulator.js](https://github.com/dcloudio/touchemulator)来解决。hello uni-app的pc版也是使用了这个方案。