u-picker-data.vue 2.2 KB

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