Picker选择器

在一组预设数据中进行选择,e.g. 省市区选择。

规则

  • 尽量使用 Picker 来帮助用户完成输入,避免用户通过键盘直接输入。
  • DatePicker 是 Picker 的特定模式。

代码演示

基本用法

数据级联选择示例.

  1. import { Component } from '@angular/core';
  2. import { PickerService } from 'ng-zorro-antd-mobile';
  3. import { district, provinceLite } from 'antd-mobile-demo-data';
  4. @Component({
  5. selector: 'demo-picker-basic',
  6. template: `
  7. <div Button (click)="showPicker()">{{ name }}</div>
  8. <WhiteSpace></WhiteSpace>
  9. <List className="my-list">
  10. <ListItem
  11. Picker
  12. [data]="data"
  13. [extra]="name1"
  14. [arrow]="'horizontal'"
  15. [mask]="true"
  16. [title]="'Areas'"
  17. [(ngModel)]="value1"
  18. (ngModelChange)="onOk1($event)"
  19. (onDismiss)="onDismiss1()"
  20. >
  21. Multiple & cascader
  22. </ListItem>
  23. <ListItem
  24. Picker
  25. [data]="delayData"
  26. [extra]="name1"
  27. [arrow]="'horizontal'"
  28. [mask]="true"
  29. [title]="'Areas'"
  30. [(ngModel)]="value1"
  31. (ngModelChange)="onOk1($event)"
  32. (onDismiss)="onDismiss1()"
  33. >
  34. Multiple & delayData
  35. </ListItem>
  36. <ListItem
  37. Picker
  38. [extra]="name2"
  39. [arrow]="'horizontal'"
  40. [cascade]="false"
  41. [data]="seasons"
  42. [title]="'选择季节'"
  43. [(ngModel)]="value2"
  44. (ngModelChange)="onOk2($event)"
  45. >
  46. Multiple
  47. </ListItem>
  48. <ListItem
  49. Picker
  50. [extra]="name3"
  51. [arrow]="'horizontal'"
  52. [data]="singleArea"
  53. [(ngModel)]="value3"
  54. (ngModelChange)="onOk3($event)"
  55. >
  56. Single
  57. </ListItem>
  58. <ListItem
  59. Picker
  60. [data]="asynData"
  61. [cols]="cols"
  62. [extra]="name4"
  63. [arrow]="'horizontal'"
  64. [(ngModel)]="value4"
  65. (onPickerChange)="onPickerChange($event)"
  66. (ngModelChange)="onOk4($event)"
  67. >
  68. Multiple & async
  69. </ListItem>
  70. <ListItem
  71. Picker
  72. [extra]="name1"
  73. [disabled]="true"
  74. [arrow]="'horizontal'"
  75. [mask]="true"
  76. [title]="'Areas'"
  77. [(ngModel)]="value1"
  78. (ngModelChange)="onOk1($event)"
  79. (onDismiss)="onDismiss1()"
  80. >
  81. Disabled
  82. </ListItem>
  83. </List>
  84. `
  85. })
  86. export class DemoPickerBasicComponent {
  87. data = district;
  88. asynData = provinceLite;
  89. delayData = [];
  90. singleArea = [
  91. '东城区',
  92. '西城区',
  93. '崇文区',
  94. '宣武区',
  95. '朝阳区',
  96. '丰台区',
  97. '石景山区',
  98. '海淀区',
  99. '门头沟区',
  100. '房山区',
  101. '通州区',
  102. '顺义区',
  103. '昌平区',
  104. '大兴区',
  105. '平谷区',
  106. '怀柔区',
  107. '密云县',
  108. '延庆县'
  109. ];
  110. seasons = [
  111. {
  112. label: '2013',
  113. children: [
  114. {
  115. label: '春',
  116. children: []
  117. },
  118. {
  119. label: '夏',
  120. children: []
  121. }
  122. ]
  123. },
  124. {
  125. label: '2014',
  126. children: [
  127. {
  128. label: '春'
  129. },
  130. {
  131. label: '夏'
  132. }
  133. ]
  134. }
  135. ];
  136. name = '选择';
  137. name1 = '选择';
  138. name2 = '选择';
  139. name3 = '选择';
  140. name4 = '选择';
  141. value = [];
  142. value1 = [];
  143. value2 = [];
  144. value3 = [];
  145. value4 = [];
  146. cols = 1;
  147. constructor(private _picker: PickerService) {
  148. setTimeout(() => {
  149. this.delayData = this.data;
  150. }, 10000);
  151. }
  152. onDismiss1() {
  153. console.log('cancel');
  154. }
  155. onOk1(result) {
  156. this.name1 = this.getResult(result);
  157. }
  158. onOk2(result) {
  159. this.name2 = this.getResult(result);
  160. }
  161. onOk3(result) {
  162. this.name3 = this.getResult(result);
  163. }
  164. onOk4(result) {
  165. this.name4 = this.getResult(result);
  166. }
  167. onPickerChange(result) {
  168. const val = this.getValue(result);
  169. console.log(val);
  170. let colNum = 1;
  171. const d = [...this.asynData];
  172. const asyncValue = [...val];
  173. if (val[0] === 'zj') {
  174. d.forEach((i) => {
  175. if (i.value === 'zj') {
  176. colNum = 2;
  177. if (!i.children) {
  178. i.children = [{
  179. value: 'zj-nb',
  180. label: '宁波',
  181. }, {
  182. value: 'zj-hz',
  183. label: '杭州',
  184. }];
  185. asyncValue.push('zj-nb');
  186. } else if (val[1] === 'zj-hz') {
  187. i.children.forEach((j) => {
  188. if (j.value === 'zj-hz') {
  189. j.children = [{
  190. value: 'zj-hz-xh',
  191. label: '西湖区',
  192. }];
  193. asyncValue.push('zj-hz-xh');
  194. }
  195. });
  196. colNum = 3;
  197. }
  198. }
  199. });
  200. } else {
  201. colNum = 1;
  202. }
  203. this.asynData = d;
  204. this.cols = colNum;
  205. this.value4 = asyncValue;
  206. }
  207. getResult(result) {
  208. this.value = [];
  209. let temp = '';
  210. result.forEach(item => {
  211. this.value.push(item.label || item);
  212. temp += item.label || item;
  213. });
  214. return this.value.map(v => v).join(',');
  215. }
  216. getValue(result) {
  217. let value = [];
  218. let temp = '';
  219. result.forEach(item => {
  220. value.push(item.value || item);
  221. temp += item.value || item;
  222. });
  223. return value;
  224. }
  225. showPicker() {
  226. PickerService.showPicker(
  227. { value: this.value, data: this.singleArea },
  228. result => {
  229. this.name = this.getResult(result);
  230. this.value = this.getValue(result);
  231. },
  232. cancel => {
  233. console.log('cancel');
  234. }
  235. );
  236. }
  237. }

Picker 选择器 - 图1

API

属性说明类型默认值
ngModel当前值, 格式是[value1, value2, value3], 对应数据源的相应级层value, 可双向绑定Array-
data数据源Array<{value, label, children: Array}>-
cols列数Number3
onChange选中后的回调(val): void-
onPickerChange每列数据选择变化后的回调函数(val): void-
okText选中的文案String确定
dismissText取消选中的文案String取消
ngModelChange点击选中时执行的回调EventEmitter<any[]>-
onDismiss点击取消时执行的回调(): void
title大标题String-
disabled是否不可用Booleanfalse
cascade是否联动Booleantrue

注: 组件不再提供默认的城市初始化数据。

PickerService.showPicker(config, confirm?, cancel?)

属性说明类型默认值
config初始化配置{data: [], value: [],…}
confirm选中后的回调(val): void
cancel点击取消时执行的回调(): void