u-picker-data.vue 2.3 KB

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