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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<template>
    <view class="w-picker-view">
        <picker-view class="d-picker-view" :indicator-style="itemHeight" :value="pickVal" @change="handlerChange">
            <picker-view-column>
                <view class="w-picker-item" v-for="(item,index) in range" :key="index">{{item[nodeKey]}}</view>
            </picker-view-column>
        </picker-view>
    </view>
</template>
 
<script>
    export default {
        props:{
            itemHeight:{
                type:String,
                default:"44px"
            },
            options:{
                type:[Array,Object],
                default(){
                    return []
                }
            },
            value:{
                type:String,
                default:""
            },
            defaultType:{
                type:String,
                default:"label"
            },
            defaultProps:{
                type:Object,
                default(){
                    return{
                        label:"label",
                        value:"value"
                    }
                }
            }
        },
        data() {
            return {
                pickVal:[]
            };
        },
        computed:{
            nodeKey(){
                return this.defaultProps.label;
            },
            nodeValue(){
                return this.defaultProps.value;
            },
            range(){
                return this.options
            }
        },
        watch:{
            value(val){
                if(this.options.length!=0){
                    this.initData();
                }
            },
            options(val){
                this.initData();
            }
        },
        created() {
            if(this.options.length!=0){
                this.initData();
            }
        },
        methods:{
            initData(){
                let dVal=this.value||"";
                let data=this.range;
                let pickVal=[0];
                let cur=null;
                let label="";
                let value,idx;
                if(this.defaultType==this.nodeValue){
                    value=data.find((v)=>v[this.nodeValue]==dVal);
                    idx=data.findIndex((v)=>v[this.nodeValue]==dVal);
                }else{
                    value=data.find((v)=>v[this.nodeKey]==dVal);
                    idx=data.findIndex((v)=>v[this.nodeKey]==dVal);
                }
                pickVal=[idx!=-1?idx:0];
                this.$nextTick(()=>{
                    this.pickVal=pickVal;
                });
                if(this.defaultType==this.nodeValue){
                    this.$emit("change",{
                        result:value?value[this.nodeKey]:data[0][this.nodeKey],
                        value:dVal||data[0][this.nodeKey],
                        obj:value?value:data[0]
                    })
                }else{
                    this.$emit("change",{
                        result:dVal||data[0][this.nodeKey],
                        value:value?value[this.nodeValue]:data[0][this.nodeValue],
                        obj:value?value:data[0]
                    })
                }
                
            },
            handlerChange(e){
                let arr=[...e.detail.value];
                let pickVal=[arr[0]||0];
                let data=this.range;
                let cur=data[arr[0]];
                let label="";
                let value="";
                this.$nextTick(()=>{
                    this.pickVal=pickVal;
                });
                this.$emit("change",{
                    result:cur[this.nodeKey],
                    value:cur[this.nodeValue],
                    obj:cur
                })
            }
        }
    }
</script>
 
<style lang="scss">
    @import "./w-picker.css";
</style>