u-picker-data.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <template>
  2. <view class="u-picker-data">
  3. <view class="u-picker-data__trigger">
  4. <slot name="trigger"></slot>
  5. <up-input
  6. v-if="!$slots['trigger']"
  7. :modelValue="current"
  8. disabled
  9. disabledColor="#ffffff"
  10. :placeholder="title"
  11. border="none"
  12. ></up-input>
  13. <view @click="show = true"
  14. class="u-picker-data__trigger__cover"></view>
  15. </view>
  16. <up-picker
  17. :show="show"
  18. :columns="optionsInner"
  19. :keyName="labelKey"
  20. @confirm="select"
  21. @cancel="cancel">
  22. </up-picker>
  23. </view>
  24. </template>
  25. <script>
  26. export default {
  27. props: {
  28. modelValue: {
  29. type: [String, Number],
  30. default: ''
  31. },
  32. title: {
  33. type: String,
  34. default: ''
  35. },
  36. description: {
  37. type: String,
  38. default: ''
  39. },
  40. options: {
  41. type: Array,
  42. default: () => {
  43. return []
  44. }
  45. },
  46. valueKey: {
  47. type: String,
  48. default: 'id'
  49. },
  50. labelKey: {
  51. type: String,
  52. default: 'name'
  53. }
  54. },
  55. data() {
  56. return {
  57. show: false,
  58. current: '',
  59. }
  60. },
  61. created() {
  62. if (this.modelValue) {
  63. this.options.forEach((ele) => {
  64. if (ele[this.valueKey] == this.modelValue) {
  65. this.current = ele[this.labelKey]
  66. }
  67. })
  68. }
  69. },
  70. watch: {
  71. modelValue() {
  72. if (this.modelValue) {
  73. this.options.forEach((ele) => {
  74. if (ele[this.valueKey] == this.modelValue) {
  75. this.current = ele[this.labelKey]
  76. }
  77. })
  78. }
  79. }
  80. },
  81. computed: {
  82. optionsInner() {
  83. return [this.options];
  84. }
  85. },
  86. emits: ['update:modelValue'],
  87. methods: {
  88. hideKeyboard() {
  89. uni.hideKeyboard()
  90. },
  91. cancel() {
  92. this.show = false;
  93. },
  94. select(e) {
  95. const {
  96. columnIndex,
  97. index,
  98. value,
  99. } = e;
  100. this.show = false;
  101. // console.log(value);
  102. this.$emit('update:modelValue', value[0][this.valueKey]);
  103. this.current = value[0][this.labelKey];
  104. },
  105. }
  106. }
  107. </script>
  108. <style lang="scss" scoped>
  109. .u-picker-data {
  110. &__trigger {
  111. position: relative;
  112. &__cover {
  113. position: absolute;
  114. top: 0;
  115. left: 0;
  116. right: 0;
  117. bottom: 0;
  118. }
  119. }
  120. }
  121. </style>