multi-selector-picker.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <template>
  2. <view class="lb-multi-selector lb-picker-item"
  3. :style="{ height: height }">
  4. <picker-view :value="pickerValue"
  5. :indicator-style="indicatorStyle"
  6. :style="{ height: height }"
  7. @change="handleChange">
  8. <picker-view-column v-for="(column, index) in pickerColumns"
  9. :key="index">
  10. <!-- #ifdef H5 -->
  11. <view v-for="(item, i) in column || []"
  12. :class="[
  13. 'lb-picker-column',
  14. item[props.value] === selectValue[index]
  15. ? 'lb-picker-column-active'
  16. : ''
  17. ]"
  18. :key="i"
  19. :data-item="pressEnable ? JSON.stringify(item) : ''"
  20. @touchstart="touchstart"
  21. @touchmove="touchmove"
  22. @touchend="touchend">
  23. <!-- #endif -->
  24. <!-- #ifndef H5 -->
  25. <view v-for="(item, i) in column || []"
  26. :class="[
  27. 'lb-picker-column',
  28. item[props.value] === selectValue[index]
  29. ? 'lb-picker-column-active'
  30. : ''
  31. ]"
  32. :key="i"
  33. :data-item="item"
  34. @touchstart="touchstart"
  35. @touchmove="touchmove"
  36. @touchend="touchend">
  37. <!-- #endif -->
  38. <!-- #ifdef APP-PLUS || H5 -->
  39. <text :class="[
  40. 'lb-picker-column-label',
  41. `lb-picker-column-label-${align}`
  42. ]"
  43. :style="[
  44. item[props.value] === selectValue[index]
  45. ? activeColumnStyle
  46. : columnStyle
  47. ]">{{ getLabel(item, i, index) }}</text>
  48. <!-- #endif -->
  49. <!-- #ifndef APP-PLUS || H5 -->
  50. <text :class="[
  51. 'lb-picker-column-label',
  52. `lb-picker-column-label-${align}`
  53. ]">{{ item[props.label] || item }}</text>
  54. <!-- #endif -->
  55. </view>
  56. </picker-view-column>
  57. </picker-view>
  58. </view>
  59. </template>
  60. <script>
  61. import { commonMixin } from '../mixins'
  62. export default {
  63. props: {
  64. value: Array,
  65. list: Array,
  66. mode: String,
  67. props: Object,
  68. level: Number,
  69. visible: Boolean,
  70. height: String,
  71. columnStyle: Object,
  72. activeColumnStyle: Object,
  73. align: String,
  74. pressEnable: Boolean,
  75. pressTime: Number,
  76. formatter: Function
  77. },
  78. mixins: [commonMixin],
  79. data () {
  80. return {
  81. pickerValue: [],
  82. pickerColumns: [],
  83. selectValue: [],
  84. selectItem: []
  85. }
  86. },
  87. methods: {
  88. handleChange (item) {
  89. const pickerValue = item.detail.value
  90. const columnIndex = pickerValue.findIndex(
  91. (item, i) => item !== this.pickerValue[i]
  92. )
  93. const valueIndex = pickerValue[columnIndex]
  94. this.setPickerChange(pickerValue, valueIndex, columnIndex)
  95. },
  96. setPickerChange (pickerValue, valueIndex, columnIndex) {
  97. for (let i = 0; i < this.level; i++) {
  98. if (i > columnIndex) {
  99. pickerValue[i] = 0
  100. const column =
  101. this.pickerColumns[i - 1][valueIndex] ||
  102. this.pickerColumns[i - 1][0]
  103. this.$set(this.pickerColumns, i, column[this.props.children] || [])
  104. valueIndex = 0
  105. }
  106. this.$set(this.pickerValue, i, pickerValue[i])
  107. const selectItem = this.pickerColumns[i][pickerValue[i]]
  108. if (selectItem) {
  109. this.selectItem[i] = selectItem
  110. this.selectValue[i] = selectItem[this.props.value]
  111. } else {
  112. const spliceNum = this.level - i
  113. this.pickerValue.splice(i, spliceNum)
  114. this.selectValue.splice(i, spliceNum)
  115. this.selectItem.splice(i, spliceNum)
  116. this.pickerColumns.splice(i, spliceNum)
  117. break
  118. }
  119. }
  120. this.$emit('change', {
  121. value: this.selectValue,
  122. item: this.selectItem,
  123. index: this.pickerValue,
  124. change: 'scroll'
  125. })
  126. }
  127. }
  128. }
  129. </script>
  130. <style lang="scss" scoped>
  131. @import "../style/picker-item.scss";
  132. </style>