Picker选择器

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

规则

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

代码演示

基本用法

数据级联选择示例.

  1. import { Component } from '@angular/core';
  2. import { PickerService, PickerRef } 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. {
  180. value: 'zj-nb',
  181. label: '宁波'
  182. },
  183. {
  184. value: 'zj-hz',
  185. label: '杭州'
  186. }
  187. ];
  188. asyncValue.push('zj-nb');
  189. } else if (val[1] === 'zj-hz') {
  190. i.children.forEach(j => {
  191. if (j.value === 'zj-hz') {
  192. j.children = [
  193. {
  194. value: 'zj-hz-xh',
  195. label: '西湖区'
  196. }
  197. ];
  198. asyncValue.push('zj-hz-xh');
  199. }
  200. });
  201. colNum = 3;
  202. }
  203. }
  204. });
  205. } else {
  206. colNum = 1;
  207. }
  208. this.asynData = d;
  209. this.cols = colNum;
  210. this.value4 = asyncValue;
  211. }
  212. getResult(result) {
  213. this.value = [];
  214. let temp = '';
  215. result.forEach(item => {
  216. this.value.push(item.label || item);
  217. temp += item.label || item;
  218. });
  219. return this.value.map(v => v).join(',');
  220. }
  221. getValue(result) {
  222. let value = [];
  223. let temp = '';
  224. result.forEach(item => {
  225. value.push(item.value || item);
  226. temp += item.value || item;
  227. });
  228. return value;
  229. }
  230. showPicker() {
  231. const ref: PickerRef = this._picker.showPicker(
  232. { value: this.value, data: this.singleArea },
  233. result => {
  234. this.name = this.getResult(result);
  235. this.value = this.getValue(result);
  236. },
  237. cancel => {
  238. console.log('cancel');
  239. }
  240. );
  241. }
  242. }

Picker选择器 - 图1

API

参数说明类型默认值
[data]数据源Array<{value, label, children: Array}>-
[cols]列数number3
[okText]选中的文案string‘确定’
[dismissText]取消选中的文案string‘取消’
[title]大标题string-
[disabled]是否不可用booleanfalse
[cascade]是否联动booleantrue
[indicatorStyle]indicator的样式object-
[(ngModel)]当前值, 格式是[value1, value2, value3], 对应数据源的相应级层valueArray-
(onChange)选中后的回调EventEmitter<object>-
(onPickerChange)每列数据选择变化后的回调函数EventEmitter<object>-
(ngModelChange)点击选中时执行的回调EventEmitter<any[]>-
(onDismiss)点击取消时执行的回调EventEmitter<void>-

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

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

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

以上函数调用后,会返回一个引用,可以通过该引用关闭弹窗。

  1. constructor(picker: PickerService) {
  2. const ref: PickerRef = picker.showPicker(
  3. { value: this.value, data: this.singleArea },
  4. result => {
  5. this.name = this.getResult(result);
  6. this.value = this.getValue(result);
  7. },
  8. cancel => {
  9. console.log('cancel');
  10. }
  11. );
  12. ref.close(); // 或 ref.destroy(); 将直接销毁对话框
  13. }

PickerRef

PickerRef 对象用于控制对话框以及进行内容间的通信

通过服务方式 PickerService.xxx() 创建的对话框,都会返回一个 PickerRef 对象,该对象具有以下方法:

方法/属性说明
afterOpen和 AfterOpen 一样, 但返回值是Observable
afterClose和 AfterClose 一样, 但返回值是Observable
close(result: any) => void关闭(隐藏)对话框 注:当用于以服务方式创建的对话框,此方法将直接 销毁 对话框(同destroy方法)
destroy(result: any) => void销毁对话框 注:仅用于服务方式创建的对话框(非服务方式创建的对话框,此方法只会隐藏对话框)
getContentComponent() => Component获取对话框内容中Content的Component实例instance 注:当对话框还未初始化完毕(ngOnInit未执行)时,此函数将返回undefined