| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- <template>
- <view class="dict-selection">
- <view class="select-input" :class="{ disabled: disabled }" @click="handleClick">
- <text class="value-text">{{ displayValue || placeholder }}</text>
- <text class="arrow" v-if="!disabled">›</text>
- </view>
- <u-picker
- :show="showPicker"
- :columns="[dictList]"
- keyName="label"
- @confirm="onConfirm"
- @cancel="showPicker = false"
- ></u-picker>
- </view>
- </template>
- <script>
- import dictEnum from '@/enum/dict';
- import { mapActions, mapGetters } from 'vuex';
- export default {
- model: {
- prop: 'value',
- event: 'updateVal'
- },
- props: {
- value: {
- type: [String, Number],
- default: ''
- },
- dictName: {
- type: String,
- required: true
- },
- labelName: {
- type: String,
- default: 'dictValue'
- },
- valueName: {
- type: String,
- default: 'dictCode'
- },
- listFormatte: Function,
- placeholder: {
- type: String,
- default: '请选择'
- },
- disabled: {
- type: Boolean,
- default: false
- }
- },
- data() {
- return {
- showPicker: false
- };
- },
- computed: {
- ...mapGetters(['dict', 'getDict']),
- dictList() {
- const list =
- (this.listFormatte &&
- this.listFormatte(this.dict[dictEnum[this.dictName]] || [])) ||
- this.dict[dictEnum[this.dictName]] ||
- [];
- return list.map(item => ({
- label: item[this.labelName],
- value: item[this.valueName],
- ...item
- }));
- },
- displayValue() {
- if (!this.value && this.value !== 0) return '';
- const item = this.dictList.find(d => d.value == this.value);
- return item ? item.label : '';
- }
- },
- created() {
- if (this.dictName) {
- this.requestDict(this.dictName);
- }
- },
- methods: {
- ...mapActions('dict', ['requestDict']),
- handleClick() {
- if (!this.disabled) {
- this.showPicker = true;
- }
- },
- onConfirm(e) {
- const value = e.value[0].value;
- this.$emit('updateVal', value);
- this.$emit('itemChange', this.getDict(this.dictName, value));
- this.showPicker = false;
- }
- }
- };
- </script>
- <style lang="scss" scoped>
- .dict-selection {
- width: 100%;
- }
- .select-input {
- display: flex;
- align-items: center;
- justify-content: space-between;
- padding: 20rpx 0;
- min-height: 70rpx;
- border-bottom: 1rpx solid #e5e5e5;
- &.disabled {
- opacity: 0.6;
- }
- .value-text {
- flex: 1;
- font-size: 28rpx;
- color: #333;
- &.empty {
- color: #999;
- }
- }
- .arrow {
- font-size: 40rpx;
- color: #999;
- line-height: 1;
- margin-left: 20rpx;
- }
- }
- </style>
|