zs
2025-05-14 8944412929ca57bf921d4dd12f419778bd6cce62
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<template>
    <u-popup mode="center" :show="visible" @close="close" :round="10" :safeAreaInsetTop="true" :safeAreaInsetBottom="true">
 
        <view class="picker-boxWrap" :style="{maxHeight:maxHeight+'px'}">
 
            <!-- 添加输入框:模糊查询 调用接口 -->
            <view class="picker-input" v-if="isShowSearch">
                <u-input v-model="searchValue" type="text"  placeholder="请输入..." clearable></u-input>
                <u-button type="primary" @click="onSearch" :disabled="!searchValue">搜索</u-button>
            </view>
 
            <view class="picker-box">
                <view class="picker-item-group">
                            <view class="picker-item auto-wrap" v-for="(item,index) in list" :key="'picker-item-'+index" @tap.stop="onItemClick(index,item)">
                                {{(typeof(item)==='string'||typeof(item)==='number'||typeof(item)==='boolean'||item===null)?item:item[labelField]}}
                            </view>
 
                            <view v-if="list.length===0" class="text-tip">
                            暂无数据
                        </view>
                        </view>
 
                    
            </view>
 
        
        </view>
    </u-popup>
</template>
 
<script>
const MAX_HEIGHT = 500
export default {
    name:'easyPickerCompontent',
    emits:['update:visible','select','getCurrentData'],
    props:{
        visible:{
            type:Boolean,
            default:false
        },
        list:{
            type:Array,
            default:function(){
                return []
            }
        },
        valueField:{
            type:String,
            default:'value'
        },
        labelField:{
            type:String,
            default:'label'
        },
        isShowSearch:{
            type:Boolean,
            default:false
        },
    },
    data(){
        return {
            maxHeight:MAX_HEIGHT,
            searchValue:''
        }
    },
    methods:{
        close(){
            this.$emit('update:visible',false)
        },
        onItemClick(index,selection){
            let valObj = selection;
            let val;
            if (typeof selection === 'string' || typeof selection === 'number'||typeof(selection)==='boolean'||selection===null) {
                val = selection;
            } else {
                try{
                    val = selection[this.valueField];
                }catch(e){
                    val = selection;
                }
            }
            this.close()
            this.$emit('select',val,valObj,index)
        },
        setMaxHeight(calHeight) {
            const spaceHeight = 10
            if (calHeight>spaceHeight) {
                calHeight = calHeight - spaceHeight
                this.maxHeight = calHeight
            }
        },
        // 模糊查询  
        onSearch(){
            let searchValue = this.searchValue.trim()
            if (searchValue) {
                this.$emit('getCurrentData',searchValue)
            } 
        }
    },
    beforeMount(){
        let safety = this.$store.getters['system/getSafety'];
        if (safety) {
            this.setMaxHeight(safety.height)
        } else {
            uni.getSystemInfo({
                success:(res=>{
                    if (!res.safeAreaInsets) res.safeArea = {}
                    this.setMaxHeight(res.safeArea.height || 0)
                })
            })
        }
    }
}
</script>
 
<style scoped lang="scss">
.picker-boxWrap{
    width: 80vw;
    background-color:#f0f8ff;
    overflow: auto;
    box-sizing: border-box;
}
.picker-box{
    width: 80vw;
    background-color:#f0f8ff;
    border-radius: 20rpx;
    padding-top: 30rpx;
    overflow: auto;
    display: flex;
    justify-content: center;
    box-sizing: border-box;
    .picker-item-group{
        width: 80%;
    }
    .picker-item{
        margin: 0 20rpx 30rpx 20rpx;
        text-align: center;
        background-image: linear-gradient(to right, $color-paimary-darken, $color-paimary-lighten);
        border: 1px solid #e4e4e4;
        border-radius: 10rpx;
        font-size: 1.2rem;
        padding: 20rpx;
    }
}
.picker-input{
    display: flex;
    align-items: center;
    margin: 20rpx;
    .u-input{
        flex: 1;
    }
    .u-button{
        margin-left: 10rpx;
        width: 150rpx;
    }
}
.text-tip{
    text-align: center;
    font-size: 1.2rem;
    color: #999;
    padding: 20rpx 0;
}
</style>