selector-picker.vue 2.5 KB

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