gx
queenwuli
2021-01-28 b83ba3cc4687f21d744e9866e10e30e91229e8a4
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<template>
    <view class="main">
        <view class="input" :style="disabled?'background-color:#f5f7fa':''">
            <input @click="showModal" v-model="bValue" :style="disabled?'color:#c0c4cc':''" :placeholder="placeholder" disabled/>
            <text v-if="clearable&&!disabled" @click="empty" class="selectIcon iconcross"></text>
        </view>
        <view class="select-modal" :class="isShowModal?'show':''" @tap="hideModal">
            <view class="select-dialog" @tap.stop="" :style="{backgroundColor:bgColor}">
                <view class="select-bar bg-white">
                    <view class="action text-blue" @tap="cancelClick">{{cancelText}}</view>
                    <view class="action text-green" @tap="confirmClick">{{confirmText}}</view>
                </view>
                <view class="select-content">
                    <view class="select-item" v-for="(item,index) in list" :key="index"
                    :style="valueIndexOf(item)?'color:'+selectColor+';background-color:'+selectBgColor+';':'color:'+color+';'"
                     @click="select(item)">
                        <view class="title">{{getListKeyValue(item)}}</view>
                        <text class="selectIcon icongou" v-if="valueIndexOf(item)"></text>
                    </view>
                </view>
            </view>
        </view>
    </view>
</template>
 
<script>
    import {Function} from './eval5.min.js';
    export default {
        data() {
            return {
                isShowModal:false,
                sValue: null,
                bValue: ""
            };
        },
        props: {
            value:[String,Array],
            placeholder:{ // 占位符
                default: "",
                type: String
            },
            multiple:{ // 是否多选
                default: false,
                type: Boolean
            },
            list: {
                default: () => [],
                type: Array
            },
            valueKey:{ // 指定list中valueKey的值作为下拉框绑定内容
                default: null,
                type: String
            },
            listKey:{ // 指定list中listKey的值作为下拉框显示内容
                default: null,
                type: String
            },
            disabled: {
                default: false,
                type: Boolean
            },
            clearable:{
                default: false,
                type: Boolean
            },
            cancelText:{
                default: "取消",
                type: String
            },
            confirmText:{
                default: "确定",
                type: String
            },
            color:{
                default: "#000000",
                type: String
            },
            selectColor:{
                default: "#0081ff",
                type: String
            },
            bgColor:{
                default: "#F1F1F1",
                type: String
            },
            selectBgColor:{
                default: "#FFFFFF",
                type: String
            }
        },
        watch: {
            sValue (val) {
                this.bValue = this.getBValye(val)
                this.$emit('change', val)
            }
        },
        created() {
            if(Array.isArray(this.value)){
                this.sValue = this.value?this.value:[]
            } else {
                this.sValue = this.value
            }
        },
        methods: {
            getBValye(val){ // 将数组值转换为以,隔开的字符串
                if(val.length>0){
                    if(Array.isArray(val)){
                        let chooseAttr = []
                        val.forEach(item=>{
                            let choose = this.list.find(temp => {
                                let val_val = this.getValueKeyValue(temp)
                                return item == val_val
                            })
                            if(choose){
                                chooseAttr.push(choose)
                            }
                        })
                        let values = chooseAttr.map(temp => this.getListKeyValue(temp)).join(',')
                        return values
                    } else {
                        let choose = this.list.find(temp => {
                            let val_val = this.getValueKeyValue(temp)
                            return val == val_val
                        })
                        return this.getListKeyValue(choose)
                    }
                } else {
                    return ""
                }
            },
            select(item){
                let val = this.getValueKeyValue(item);
                if(this.multiple){
                    let index = this.sValue.indexOf(val);
                    if(index!=-1){
                        this.sValue.splice(index,1)
                    } else {
                        this.sValue.push(val)
                    }
                } else {
                    this.sValue = val
                    this.hideModal()
                }
            },
            valueIndexOf(item){
                if(this.valueKey){
                    return this.value.indexOf((this.evil(item)).toString())!=-1
                } else {
                    return this.value.indexOf(item)!=-1
                }
            },
            getListKeyValue(item){
                if(this.listKey){
                    let func = new Function('item', 'return ' + 'item.'+this.listKey);
                    return func(item)
                } else {
                    return item
                }
            },
            getValueKeyValue(item){
                if(this.valueKey){
                    return (this.evil(item)).toString()
                } else {
                    return item
                }
            },
            evil(item){
                let func = new Function('item', 'return ' + 'item.'+this.valueKey);
                return func(item)
            },
            empty(){
                if(this.multiple){
                    this.sValue = []
                } else {
                    this.sValue = ''
                }
            },
            cancelClick(){ // 点击取消
                this.$emit('cancel', this.sValue)
                this.hideModal()
            },
            confirmClick(){ // 点击确定
                this.$emit('confirm', this.sValue)
                this.hideModal()
            },
            showModal(){
                if(!this.disabled){
                    this.isShowModal = true
                }
            },
            hideModal(){
                this.isShowModal = false
            }
        }
    }
</script>
<style>    
    @font-face {font-family: "selectIcon";
      src: url('//at.alicdn.com/t/font_1833441_ycfzdhg2u3.eot?t=1590375117208'); /* IE9 */
      src: url('//at.alicdn.com/t/font_1833441_ycfzdhg2u3.eot?t=1590375117208#iefix') format('embedded-opentype'), /* IE6-IE8 */
      url('data:application/x-font-woff2;charset=utf-8;base64,d09GMgABAAAAAAMEAAsAAAAABvQAAAK4AAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAHEIGVgCDBgqBRIFCATYCJAMMCwgABCAFhQUHNRsfBsg+QCa3uoO0oAJTMwhxVu965keqWBy1hkbwtfzWb2Z279/shRhJisKF6FApKLI7oyBbpAaHo3w24k+ca9EUJbDmjaeznUdZ/FOUlkWdJ33rizZY/Pw6J5Xw0qKYxHTMesePHVT6EFpaC4zV70sKi2bYgNPc1w0WHnDVC/e/UnNTgyP+4Jq6BBpIHoisgypLaIAFEtU0wgeaIG8Yu4nAIZwnUK1QgFfOT6nUUoBpgXjj2lqplTMpiuXtCW3N2iK+aPTS2/Qdnzny8d+5IEiaDMy99exklra//FrKnX48pChmgrq5QcYRQCEe17ruqgqLAKv8WntwqwhpLms/nB5yW/iHRxJEC0QOgT3NnfgF01NBKvOuIzNoZdh5gJuAeGrsozE8vOJ7u5D832oz55039W5G+S52K0H+zNf1TJz07k26kqoQybRfwVFV4rjDS/K8EXUyuF1cXnT3weKS9Rvdm/xe7h8oA1hLwOR18R+Y4n4zwpr4z5SU089Vc+cpfWL+mn5APmT3Z39jeOs/GbWjK+DnmsuL/u6ehMX4j4yedSVkAUUuPh3TY022MtKZUEOtPqCb8Bkvnr5XT6imU0gGrEJW7aAL/gw0OhegVV2F6pC7uTOppirKIA4MFQhTrpCM+AbZlDu64L/QmAkQWlMhQXU75D07O9Gtl0PUYjTBLyAzOLNQYtypIEEjvsXtBLQTooV2nrQrGEau2gKmZlR4L8gwnGtBJbUn1diCOOQUnEkTkRAOeci9KHOQxvFro+tx3ZcGAaeljstCSBNDJuArgIyBYyy6OdZxAhHIELu1IC9AtgShCVtLltEKrSff1XoHJo3RC33hM63o3j6pSNkmqmIWEAtxFHB2OwoRBAfyeqE3r2ogHeF42dBhs7gvf7CukH5MmlUGOCpHihxFfs6TehDyKCqVAA==') format('woff2'),
      url('//at.alicdn.com/t/font_1833441_ycfzdhg2u3.woff?t=1590375117208') format('woff'),
      url('//at.alicdn.com/t/font_1833441_ycfzdhg2u3.ttf?t=1590375117208') format('truetype'), /* chrome, firefox, opera, Safari, Android, iOS 4.2+ */
      url('//at.alicdn.com/t/font_1833441_ycfzdhg2u3.svg?t=1590375117208#selectIcon') format('svg'); /* iOS 4.1- */
    }
    
    .selectIcon {
      font-family: "selectIcon" !important;
      font-size: 16px;
      font-style: normal;
      -webkit-font-smoothing: antialiased;
      -moz-osx-font-smoothing: grayscale;
    }
    
    .icongou:before {
      content: "\e61c";
    }
    
    .iconcross:before {
      content: "\e61a";
    }
 
</style>
<style lang="scss" scoped>
    .main{
        font-size: 28rpx;
    }
    .bg-white{
        background-color: #FFFFFF;
    }
    .text-blue{
        color: #0081ff;
    }
    .text-green{
        color: #39b54a;
    }
    .input {
        display: flex;
        align-items:center;
        flex: 1;
        // font-size: 28rpx;
        // height: 60rpx;
        // padding: 10rpx 20rpx;
        // border-radius: 10rpx;
        // border-style: solid;
        // border-width: 1rpx;
        // border-color: rgba(0, 0, 0, 0.1);
        input{
            flex: 1;
        }
    }
    .select-modal {
        position: fixed;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
        z-index: 9999;
        opacity: 0;
        outline: 0;
        text-align: center;
        -ms-transform: scale(1.185);
        transform: scale(1.185);
        backface-visibility: hidden;
        perspective: 2000rpx;
        background: rgba(0, 0, 0, 0.6);
        transition: all 0.3s ease-in-out 0s;
        pointer-events: none;
        margin-bottom: -1000rpx;
        &::before {
            content: "\200B";
            display: inline-block;
            height: 100%;
            vertical-align: bottom;
        }
        .select-dialog {
            position: relative;
            display: inline-block;
            margin-left: auto;
            margin-right: auto;
            background-color: #f8f8f8;
            overflow: hidden;
            width: 100%;
            border-radius: 0;
            margin-bottom: -4px;
            .select-content{
                // background-color: #F1F1F1;
                max-height: 420rpx;
                overflow:auto;
                .select-item{
                    padding: 20rpx;
                    display: flex;
                    .title{
                        flex: 1;
                    }
                }
            }
        }
    }
    .select-modal.show {
        opacity: 1;
        transition-duration: 0.3s;
        -ms-transform: scale(1);
        transform: scale(1);
        overflow-x: hidden;
        overflow-y: auto;
        pointer-events: auto;
        margin-bottom: 0;
    }
    .select-bar {
        background: #F6F6F8;
        padding: 0 20rpx;
        display: flex;
        position: relative;
        align-items: center;
        min-height: 80rpx;
        justify-content: space-between;
        .action {
            display: flex;
            align-items: center;
            height: 100%;
            justify-content: center;
            max-width: 100%;
        }
    }
</style>