unlinked-selector-picker.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <template>
  2. <view class="selector-picker 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. <view v-for="(item, i) in column"
  11. :class="[
  12. 'lb-picker-column',
  13. item[props.value] || item === selectValue[index]
  14. ? 'lb-picker-column-active'
  15. : ''
  16. ]"
  17. :key="i"
  18. :style="{ height: columnHeight, 'line-height': columnHeight }">
  19. <view class="lb-picker-column-label">
  20. {{ item[props.label] || item }}
  21. </view>
  22. </view>
  23. </picker-view-column>
  24. </picker-view>
  25. </view>
  26. </template>
  27. <script>
  28. import { getIndicatorHeight, isObject } from '../utils.js'
  29. const indicatorHeight = getIndicatorHeight()
  30. export default {
  31. props: {
  32. value: Array,
  33. list: Array,
  34. props: Object,
  35. visible: Boolean,
  36. height: String,
  37. indicatorHeight: Number
  38. },
  39. data () {
  40. return {
  41. pickerValue: [],
  42. pickerColumns: [],
  43. selectValue: [],
  44. selectItem: [],
  45. columnHeight: indicatorHeight + 'px',
  46. indicatorStyle: `height: ${indicatorHeight}px`
  47. }
  48. },
  49. created () {
  50. this.init('init')
  51. },
  52. methods: {
  53. init (changeType) {
  54. if (this.list && this.list.length) {
  55. this.pickerColumns = this.list
  56. this.setPickerValue()
  57. this.$emit('change', {
  58. value: this.selectValue,
  59. item: this.selectItem,
  60. index: this.pickerValue,
  61. change: changeType
  62. })
  63. }
  64. },
  65. setPickerValue (value) {
  66. this.list.forEach((item, i) => {
  67. let index = item.findIndex(item => {
  68. return isObject(item)
  69. ? item[this.props.value] === this.value[i]
  70. : item === this.value[i]
  71. })
  72. index = index > -1 ? index : 0
  73. const columnItem = this.list[i][index]
  74. const valueItem = isObject(columnItem)
  75. ? columnItem[this.props.value]
  76. : columnItem
  77. this.$set(this.pickerValue, i, index)
  78. this.$set(this.selectValue, i, valueItem)
  79. this.$set(this.selectItem, i, columnItem)
  80. })
  81. },
  82. handleChange (item) {
  83. const pickerValue = item.detail.value
  84. const columnIndex = pickerValue.findIndex(
  85. (item, i) => item !== this.pickerValue[i]
  86. )
  87. if (columnIndex > -1) {
  88. const valueIndex = pickerValue[columnIndex]
  89. const columnItem = this.list[columnIndex][valueIndex]
  90. const valueItem = isObject(columnItem)
  91. ? columnItem[this.props.value]
  92. : columnItem
  93. this.pickerValue = pickerValue
  94. this.$set(this.selectValue, columnIndex, valueItem)
  95. this.$set(this.selectItem, columnIndex, columnItem)
  96. this.$emit('change', {
  97. value: this.selectValue,
  98. item: this.selectItem,
  99. index: this.pickerValue,
  100. change: 'scroll'
  101. })
  102. }
  103. }
  104. },
  105. watch: {
  106. list () {
  107. this.init('list')
  108. },
  109. value (newVal) {
  110. this.init('value')
  111. }
  112. }
  113. }
  114. </script>
  115. <style lang="scss" scoped>
  116. @import "../style/picker-item.scss";
  117. </style>