u-picker-data.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. } else {
  74. this.clear();
  75. }
  76. },
  77. watch: {
  78. modelValue() {
  79. if (this.modelValue) {
  80. this.options.forEach((ele, index) => {
  81. if (ele[this.valueKey] == this.modelValue) {
  82. this.current = ele[this.labelKey]
  83. this.defaultIndex = [index]
  84. }
  85. })
  86. } else {
  87. this.clear();
  88. }
  89. }
  90. },
  91. computed: {
  92. optionsInner() {
  93. return [this.options];
  94. }
  95. },
  96. emits: ['update:modelValue', 'cancel', 'close', 'confirm'],
  97. methods: {
  98. hideKeyboard() {
  99. uni.hideKeyboard()
  100. },
  101. cancel() {
  102. this.show = false;
  103. this.$emit('cancel')
  104. },
  105. close() {
  106. this.$emit('close')
  107. },
  108. clear() {
  109. this.current = '';
  110. this.defaultIndex = [];
  111. },
  112. confirm(e) {
  113. const {
  114. columnIndex,
  115. index,
  116. value,
  117. } = e;
  118. this.show = false;
  119. // console.log(value);
  120. this.$emit('update:modelValue', value[0][this.valueKey]);
  121. this.defaultIndex = columnIndex;
  122. this.current = value[0][this.labelKey];
  123. this.$emit('confirm')
  124. }
  125. }
  126. }
  127. </script>
  128. <style lang="scss" scoped>
  129. .u-picker-data {
  130. &__trigger {
  131. position: relative;
  132. &__cover {
  133. position: absolute;
  134. top: 0;
  135. left: 0;
  136. right: 0;
  137. bottom: 0;
  138. z-index:10;
  139. }
  140. }
  141. }
  142. </style>