123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- <template>
- <view class="u-picker-data">
- <view class="u-picker-data__trigger">
- <slot name="trigger"></slot>
- <up-input
- v-if="!$slots['trigger']"
- :modelValue="current"
- disabled
- disabledColor="#ffffff"
- :placeholder="title"
- border="none"
- ></up-input>
- <view @click="show = true"
- class="u-picker-data__trigger__cover"></view>
- </view>
- <up-picker
- :show="show"
- :columns="optionsInner"
- :keyName="labelKey"
- @confirm="select"
- @cancel="cancel">
- </up-picker>
- </view>
- </template>
- <script>
- export default {
- props: {
- modelValue: {
- type: [String, Number],
- default: ''
- },
- title: {
- type: String,
- default: ''
- },
- description: {
- type: String,
- default: ''
- },
- options: {
- type: Array,
- default: () => {
- return []
- }
- },
- valueKey: {
- type: String,
- default: 'id'
- },
- labelKey: {
- type: String,
- default: 'name'
- }
- },
- data() {
- return {
- show: false,
- current: '',
- }
- },
- created() {
- if (this.modelValue) {
- this.options.forEach((ele) => {
- if (ele[this.valueKey] == this.modelValue) {
- this.current = ele[this.labelKey]
- }
- })
- }
- },
- watch: {
- modelValue() {
- if (this.modelValue) {
- this.options.forEach((ele) => {
- if (ele[this.valueKey] == this.modelValue) {
- this.current = ele[this.labelKey]
- }
- })
- }
- }
- },
- computed: {
- optionsInner() {
- return [this.options];
- }
- },
- emits: ['update:modelValue'],
- methods: {
- hideKeyboard() {
- uni.hideKeyboard()
- },
- cancel() {
- this.show = false;
- },
- select(e) {
- const {
- columnIndex,
- index,
- value,
- } = e;
- this.show = false;
- // console.log(value);
- this.$emit('update:modelValue', value[0][this.valueKey]);
- this.current = value[0][this.labelKey];
- },
- }
- }
- </script>
- <style lang="scss" scoped>
- .u-picker-data {
- &__trigger {
- position: relative;
- &__cover {
- position: absolute;
- top: 0;
- left: 0;
- right: 0;
- bottom: 0;
- }
- }
- }
- </style>
|