u-picker-data.vue 2.4 KB

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