Picker 选择器

Scan me!

滚动多列选择

引入

  1. import { Picker } from 'mand-mobile'
  2. Vue.component(Picker.name, Picker)

代码演示

单列数据 getColumnValuesgetColumnIndexs

禁用2-4项
  1. <template>
  2. <div class="md-example-child md-example-child-picker md-example-child-picker-0">
  3. <md-picker
  4. ref="picker"
  5. :data="pickerData"
  6. :invalid-index="[[2, 3, 4]]"
  7. @initialed="onPickerInitialed"
  8. @change="onPickerConfirm"
  9. is-view
  10. ></md-picker>
  11. </div>
  12. </template>
  13. <script>
  14. import {Picker, Dialog} from 'mand-mobile'
  15. import simple from 'mand-mobile/components/picker/demo/data/simple'
  16. export default {
  17. name: 'picker-demo',
  18. components: {
  19. [Picker.name]: Picker,
  20. },
  21. data() {
  22. return {
  23. pickerData: simple,
  24. pickerValue: '',
  25. }
  26. },
  27. mounted() {
  28. window.PickerTrigger0 = () => {
  29. this.getColumnValues('picker')
  30. }
  31. window.PickerTrigger1 = () => {
  32. this.getColumnIndexs('picker')
  33. }
  34. },
  35. methods: {
  36. onPickerInitialed() {
  37. const value = this.$refs.picker.getColumnValues()
  38. console.log(`[Mand Mobile] Picker Initialed: ${JSON.stringify(value)}`)
  39. },
  40. onPickerConfirm(columnIndex, itemIndex, value) {
  41. if (value) {
  42. this.pickerValue = value.text
  43. }
  44. },
  45. getColumnValues(picker) {
  46. const value = this.$refs[picker].getColumnValues()
  47. Dialog.alert({
  48. content: `<pre>${JSON.stringify(value)}</pre>`,
  49. })
  50. },
  51. getColumnIndexs(picker) {
  52. const value = this.$refs[picker].getColumnIndexs()
  53. Dialog.alert({
  54. content: `<pre>${JSON.stringify(value)}</pre>`,
  55. })
  56. },
  57. },
  58. }
  59. </script>

弹出展示

  1. <template>
  2. <div class="md-example-child md-example-child-picker md-example-child-picker-2">
  3. <md-field>
  4. <md-field-item
  5. title="起保年份"
  6. arrow="arrow-right"
  7. align="right"
  8. :value="pickerValue0"
  9. @click.native="isPickerShow0 = true">
  10. </md-field-item>
  11. <md-field-item
  12. title="省市区/县"
  13. arrow="arrow-right"
  14. align="right"
  15. :value="pickerValue1"
  16. @click.native="isPickerShow1 = true">
  17. </md-field-item>
  18. </md-field>
  19. <md-picker
  20. ref="picker0"
  21. v-model="isPickerShow0"
  22. :data="pickerData0"
  23. @confirm="onPickerConfirm(0)"
  24. title="选择年份"
  25. ></md-picker>
  26. <md-picker
  27. ref="picker1"
  28. v-model="isPickerShow1"
  29. :data="pickerData1"
  30. :cols="3"
  31. is-cascade
  32. title="选择省市区/县"
  33. @confirm="onPickerConfirm(1)"
  34. ></md-picker>
  35. </div>
  36. </template>
  37. <script>
  38. import {Picker, Field, FieldItem} from 'mand-mobile'
  39. import simple from 'mand-mobile/components/picker/demo/data/simple'
  40. import district from 'mand-mobile/components/picker/demo/data/district'
  41. export default {
  42. name: 'picker-demo',
  43. components: {
  44. [Picker.name]: Picker,
  45. [Field.name]: Field,
  46. [FieldItem.name]: FieldItem,
  47. },
  48. data() {
  49. return {
  50. isPickerShow0: false,
  51. isPickerShow1: false,
  52. pickerData0: simple,
  53. pickerData1: district,
  54. pickerValue0: '',
  55. pickerValue1: '',
  56. }
  57. },
  58. methods: {
  59. onPickerConfirm(index) {
  60. const values = this.$refs[`picker${index}`].getColumnValues()
  61. let res = ''
  62. values.forEach(value => {
  63. value && (res += `${value.text || value.label} `)
  64. })
  65. this[`pickerValue${index}`] = res
  66. },
  67. },
  68. }
  69. </script>

联动数据 getColumnValue(0)getColumnIndex(0)

默认选中3, 2, 1项
  1. <template>
  2. <div class="md-example-child md-example-child-picker md-example-child-picker-1">
  3. <md-picker
  4. ref="picker"
  5. :data="pickerData"
  6. :cols="3"
  7. :default-index="pickerDefaultIndex"
  8. is-view
  9. is-cascade
  10. @initialed="onPickerInitialed"
  11. @change="onPickerConfirm"
  12. ></md-picker>
  13. </div>
  14. </template>
  15. <script>
  16. import {Picker, Dialog} from 'mand-mobile'
  17. import district from 'mand-mobile/components/picker/demo/data/district'
  18. export default {
  19. name: 'picker-demo',
  20. components: {
  21. [Picker.name]: Picker,
  22. },
  23. data() {
  24. return {
  25. pickerData: [],
  26. pickerDefaultIndex: [],
  27. pickerValue: '',
  28. }
  29. },
  30. mounted() {
  31. this.pickerData = district
  32. this.pickerDefaultIndex = [3, 2, 1]
  33. window.PickerTrigger2 = () => {
  34. this.getColumnValue('picker', 0)
  35. }
  36. window.PickerTrigger3 = () => {
  37. this.getColumnIndex('picker', 0)
  38. }
  39. },
  40. methods: {
  41. onPickerInitialed() {
  42. const columnValues = this.$refs.picker.getColumnValues()
  43. let value = ''
  44. columnValues.forEach(item => {
  45. value += `${item.label}-`
  46. })
  47. console.log(`[Mand Mobile] Picker getColumnValues: ${value.substr(0, value.length - 1)}`)
  48. },
  49. onPickerConfirm(columnIndex, itemIndex, value) {
  50. if (value) {
  51. this.pickerValue = value.text
  52. }
  53. },
  54. getColumnValue(picker, index) {
  55. const value = this.$refs.picker.getColumnValue(index)
  56. delete value.children
  57. Dialog.alert({
  58. content: JSON.stringify(value),
  59. })
  60. },
  61. getColumnIndex(picker, index) {
  62. const value = this.$refs.picker.getColumnIndex(index)
  63. Dialog.alert({
  64. content: `<pre>${JSON.stringify(value)}</pre>`,
  65. })
  66. },
  67. },
  68. }
  69. </script>

API

Picker Props

属性说明类型默认值备注
v-model选择器是否可见Booleanfalse-
data数据源Array<{value, lable, …}>[][]-
cols数据列数Number1-
default-index选择器各列初始选中项索引Array[]-
invalid-index选择器各列不可用选项索引Array[]某列多个不可用项使用数组,单个使用数字, 如[[1,2], 2]
is-view是否内嵌在页面内展示,否则以弹层形式Booleanfalse-
is-cascade各列数据是否级联Booleanfalse级联数据格式见附录
title选择器标题String--
ok-text选择器确认文案String确认-
cancel-text选择器取消文案String取消-
mask-closable点击蒙层是否可关闭弹出层Booleantrue-

Picker Methods

refresh(callback, startColumnIndex)

重新初始化选择器,如更新data, default-indexinvalid-index

参数说明类型
callback初始化完成回调Function
startColumnIndex从某列开始重置,默认为0Function
getColumnValue(index): activeItemValue

获取某列当前选中项的值,需在initialed事件触发之后或异步调用

参数说明类型
index列索引Number

返回

属性说明类型
activeItemValue选中项的值Object: {value, lable, …}
getColumnValues(): columnsValue

获取所有列选中项的值,需在initialed事件触发之后或异步调用

返回

属性说明类型
columnsValue所有列选中项的值Array<{value, lable, …}>
getColumnIndex(index): activeItemIndex

获取某列当前选中项的索引值,需在initialed事件触发之后或异步调用

参数说明类型
index列索引Number

返回

属性说明类型
activeItemIndex选中项的索引值Number
getColumnIndexs(): columnsIndex

获取所有列选中项的索引值,需在initialed事件触发之后或异步调用

返回

属性说明类型
columnsIndex所有列选中项的索引值Array
setColumnValues(index, values, callback)

设置某列数据

参数说明类型
index列索引Number
values列数据Array<{value, lable, …}>
callback列数据设置完成回调Function

Picker Events

@initialed()

选择器数据初始化完成事件,可调用getColumnIndex, getColumnIndexs, getColumnValue, getColumnValues方法

@change(columnIndex, itemIndex, value)

选择器选中项更改事件

参数说明类型
columnIndex更改列的索引值Number
itemIndex更改列选中项的索引值Number
value更改列选中项的的值Object: {value, lable, …}
@confirm(columnsValue)

选择器确认选择事件(仅is-viewfalse

参数说明类型
columnsValue所有列选中项的值Array<{value, lable, …}>
@cancel()

选择器取消选择事件(仅is-viewfalse

@show()

选择器弹层展示事件(仅is-viewfalse

@hide()

选择器弹层隐藏事件(仅is-viewfalse

附录

  • 非级联数据源数据格式
  1. [
  2. [
  3. {
  4. // 选项展示文案
  5. "text": "",
  6. // 以下自定义字段
  7. "value": ""
  8. },
  9. // ...
  10. ],
  11. // ...
  12. ]
  • 级联数据源数据格式
  1. [
  2. [
  3. {
  4. // 选项展示文案
  5. "text": "",
  6. // 第二列对应数据
  7. "children": [
  8. {
  9. "text": "",
  10. // 第三列对应数据
  11. "children": [
  12. // ...
  13. ]
  14. },
  15. // ...
  16. ]
  17. // 以下自定义字段
  18. "value": ""
  19. },
  20. // ...
  21. ]
  22. ]