qianziyu-select.vue
4.58 KB
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
<template>
<view>
<u-popup :show="show" @close="cancel">
<view class="title">{{popupTitle}}</view>
<view style="padding: 20rpx;">
<u-search v-if="showSearch" @custom="search" @search="search" :placeholder="placeholder"
v-model="keyword"></u-search>
<u-gap v-if="showSearch" height="15"></u-gap>
<scroll-view :scroll-top="scrollTop" scroll-y="true" class="scroll-Y" @scrolltolower="$emit('lower')">
<!--单选-->
<u-radio-group v-if="type == 'radio'" :borderBottom="true" iconPlacement="right" placement="column"
@change="groupChange" v-model="radioValue">
<u-radio :customStyle="{marginBottom: '12px'}" v-for="(item, index) in dataLists" :key="index"
:label="item[name]" :name="index">
</u-radio>
</u-radio-group>
<!--多选-->
<u-checkbox-group v-if="type == 'checkbox'" :borderBottom="true" placement="column"
iconPlacement="right" @change="checkboxChange" v-model="checkboxValue">
<u-checkbox :customStyle="{marginBottom: '12px',paddingBottom:'12px'}"
v-for="(item, index) in dataLists" :key="index" :label="item[name]" :name="index">
</u-checkbox>
</u-checkbox-group>
</scroll-view>
<u-gap height="45"></u-gap>
<view class="bottons">
<u-row>
<u-col customStyle="padding:0 10rpx 20rpx 20rpx" span="6">
<u-button @click="cancel">取消</u-button>
</u-col>
<u-col customStyle="padding:0 20rpx 20rpx 10rpx" span="6">
<u-button @click="submit" type="primary" throttleTime="1000" :disabled="(JSON.stringify(radioData) === '{}') && (checkboxData.length === 0)">确认</u-button>
</u-col>
</u-row>
</view>
</view>
</u-popup>
</view>
</template>
<script>
/**
* 公共选择下拉框,基于uview。支持下拉加载、列表搜索、单选|多选
* @author qianziyu
* @description 弹出层选择器,基于uview中u-popup实现
* @property {Array} dataLists 数据列表
* @property {String} name 列表显示的字段名
* @property {Boolean} show 是否展示弹窗 (默认 false )
* @property {String} type 选择类型 单选|多选 (默认 单选 )
* @property {Boolean} showSearch 是否显示搜索框 (默认 true )
* @property {String} popupTitle 列表标题
* @property {String} placeholder 搜索框placeholder
* @event {Function} search 搜索事件,返回keyword
* @event {Function} lower 滑动到底部触发,用于下拉加载新数据
* @event {Function} cancel 组件关闭事件
* @event {Function} submit 提交按钮,返回选中的列表数据
* @example <common-select :show="show" :popupTitle="popupTitle" @cancel="show=false" @search="selectSearch" name="cworkStationName" @submit="onsubmit"
:dataLists="dataLists" placeholder="输入工站名称搜索"></common-select>
*/
export default {
name: "qianziyu-select",
props: {
dataLists: {
default: {},
type: Array
},
name: {
default: 'name',
},
show: {
default: false,
type: Boolean
},
type: {
default: 'radio',
type: String
},
showSearch: {
default: true,
type: Boolean
},
popupTitle: {
default: '列表选择',
type: String
},
placeholder: {
default: '请输入搜索内容'
}
},
data() {
return {
keyword: '',
scrollTop: 0,
checkboxData: [],
checkboxValue:[],
radioData: {},
radioValue: ''
};
},
methods: {
checkboxChange(n) {
this.checkboxData=[]
n.forEach(key=>{
this.checkboxData.push(this.dataLists[key])
})
},
//选择列表项触发
groupChange(n) {
this.radioData = this.dataLists[n]
},
//点击搜索触发
search() {
this.$emit('search', this.keyword)
},
//点击取消按钮触发
cancel() {
this.$emit('cancel')
},
//提交触发
submit() {
if (this.type == 'radio') {
if (JSON.stringify(this.radioData) == '{}') {
uni.$u.toast('请选择数据')
return;
}
this.$emit('submit', this.radioData)
} else if (this.type == 'checkbox') {
if (this.checkboxData.length == 0) {
uni.$u.toast('请选择数据')
return;
}
this.$emit('submit', this.checkboxData)
}
}
}
}
</script>
<style lang="scss" scoped>
.u-popup {
.title {
border-bottom: 1px solid #f7f7f7;
padding: 20rpx;
text-align: center;
font-weight: bold;
}
}
.scroll-Y {
height: 650rpx;
}
.bottons {
background-color: white;
position: fixed;
left: 0;
bottom: 0;
right: 0;
bottom: constant(safe-area-inset-bottom);
bottom: env(safe-area-inset-bottom);
}
</style>