Houjie
2025-04-11 1bf977929dd324f3ac64b70debd8a79443c54392
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<template>
        <view class="cu-form-group">
            <view class="title">{{label}}</view>
        <picker @change="PickerChange" :value="index" :range-key="rangeKey" :range="dictOptions">
            <view class="picker">
                {{index>-1?dictOptions[index][rangeKey]:'请选择'}}
            </view>
        </picker>
        </view>
</template>
 
<script>
    export default {
        name: 'MySelect',
        props: {
            dictCode: String,
            value: String,
            label:String,
            rangeKey:{type:String,default:'label'},
            valueKey:{type:String,default:'value'},
            searchUrl:String,
 
        },
        watch: {
            dictCode: {
                immediate: true,
                handler() {
                    this.initDictData()
                },
            },
        },
        computed: {
            
        },
        data() {
            return {
                dictOptions: [],
                index: -1,
            }
        },
        methods: {
            initDictData() {
                //根据字典Code, 初始化字典数组
                if (this.searchUrl){
                    this.$http.get(this.searchUrl,{"code":this.dictCode}).then(res=>{
                        if(res.data.success){
                            this.dictOptions = res;
                            this.getIndex()
                        }
                    })
                }else{
                    let code = this.dictCode;
                    this.$http.get(`/sys/dict/getDictItems/${code}`).then(res=>{
                        if(res.data.success){
                            this.dictOptions = res.data.result;
                            this.getIndex()
                        }
                    })
                }
            },
            PickerChange(e) {
                this.index=e.detail.value
                if(this.index==-1){
                    this.index=0
                    this.$emit('input',this.dictOptions[0][this.valueKey])
                }else{
                    this.$emit('input', this.dictOptions[this.index][this.valueKey]);
                }
            },
            getIndex() {
                for (var i = 0; i < this.dictOptions.length; i++) {
                    if (this.dictOptions[i].value == this.value) {
                        this.index = i
                        return
                    }
                }
                this.index=-1
            },
        }
    }
</script>
 
<style>
 
</style>