schangxiang@126.com
2025-09-19 9be9c3784b2881a3fa25e93ae2033dc2803c0ed0
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
<template>
    <el-popover v-model:visible="visible" :width="pannelWidth" :placement="placement" :trigger="trigger" popper-class="packaged-dropdown-menu-popover">         <!-- 下拉面板响应区域 start -->
        <template #reference>
            <div class="packaged-dropdown-menu-trigger-target" ref="target" v-on:[trigger]="targetTrigger">
                <div><slot></slot></div>
                <el-icon class="drop-icon"><arrow-down /></el-icon>
            </div>
        </template>
        <!-- 下拉面板响应区域 end -->
        <div class="packaged-dropdown-menu-popper" :class="[multiple?'has-bottom':'',hasSearch?'has-search':'']" :style="{
            maxHeight:maxHeight,
            paddingTop:(hasSearch?'48px':'0'),
            paddingBottom:(multiple?'48px':'0')
        }" @click.stop="">
            <!-- 搜索输入框 start -->
            <div class="search-bar" v-if="hasSearch">
                <el-input size="small" :placeholder="placeholder" v-model="searchValue" @change="onSearch">
                    <template #append>
                        <el-button @click.stop="onSearch"><el-icon class="drop-icon"><el-search /></el-icon></el-button>
                    </template>
                </el-input>
            </div>
            <!-- 搜索输入框 end -->
            <!-- 底部操作区域 start -->
            <div class="bottom-row" v-if="multiple">
                <div class="all-check">
                    <el-checkbox v-model="selectAll" label="全选" @change="onChangeSelectAll" />
                </div>
                <div class="btns-group">
                    <el-button size="small" @click="onCancel">取 消</el-button>
                    <el-button size="small" type="primary" @click="onConfirm">确 定</el-button>
                </div>
            </div>
            <!-- 底部操作区域 end -->
            <!-- 菜单列表 start -->
            <div class="menu-list" v-loading="loading">
                <el-scrollbar>
                    <div class="menu-list-item-group">
                        <div class="menu-list-item" v-for="(item,index) in menuList" :key="'drop-down-item-'+index" @click="onItemClick(index,$event)">
                            <div class="item-checkbox" v-if="multiple">
                                <el-checkbox v-model="item.checked" @change="onChangeSelect" />
                            </div>
                            <div class="item-content">{{item[labelProp]}}</div>
                        </div>
                    </div>
                </el-scrollbar>
            </div>
            <!-- 菜单列表 end -->
        </div>
    </el-popover>
</template>
 
<script>
import {ArrowDown,Search} from '@element-plus/icons'
export default {
    name:'packagedDropdownMenu',
    components:{ArrowDown,elSearch:Search},
    props:{
        trigger:{
            type:String,
            default:'click'
        },
        placement:{
            type:String,
            default:'bottom'  /* 弹出层位置 top/top-start/top-end/bottom/bottom-start/bottom-end/left/left-start/left-end/right/right-start/right-end */
        },
        /* 下拉面板宽度 */
        pannelWidth:{
            type:String,
            default:'240px'
        },
        /* 下拉菜单数据源,可通过改变menuRefresh更新菜单视图 */
        dataSource:{
            type:Array,
            default:function(){
                return []
            }
        },
        valueProp:{
            type:String,
            default:'value'
        },
        labelProp:{
            type:String,
            default:'label'
        },
        /* 下拉项数据是否不是json对象,默认是json对象false,不是json对象为true */
        metaItem:{
            type:Boolean,
            default:false
        },
        /* 下拉面板最大高度 */
        maxHeight:{
            type:String,
            default:'300px'
        },
        /* 搜索输入框默认提示 */
        placeholder:{
            type:String,
            default:'请输入...'
        },
        /* 是否是多选 */
        multiple:{
            type:Boolean,
            default:true
        },
        /* 是否开启搜索 */
        hasSearch:{
            type:Boolean,
            default:false
        },
        /* 菜单列表更新标记,此数据变化会触发菜单列表更新 */
        menuRefresh:{
            type:Number,
            default:0
        },
        /* 菜单列表的loading效果 */
        loading:{
            type:Boolean,
            default:false
        },
    },
    emits:['confirm','search'],
    data(){
        return {
            visible:false,
            selectAll:true,
            menuList:[],
            searchValue:''
        }
    },
    created() {
        this.refreshMenus()
    },
    watch:{
        menuRefresh(newVal,oldVal){
            if (newVal!==oldVal) {
                this.refreshMenus()
            }
        }
    },
    methods:{
        /* 更新菜单 */
        refreshMenus(){
            this.menuList = this.dataSource.map((item)=>{
                let obj = {};
                if (this.metaItem) {
                    obj[this.valueProp] = item;
                    obj[this.labelProp] = item;
                } else {
                    obj[this.valueProp] = item[this.valueProp];
                    obj[this.labelProp] = item[this.labelProp];
                }
                obj.checked = true;
                return obj;
            })
            this.selectAll = true;
        },
        close(){
            this.visible = false;
        },
        onCancel(){
            this.close()
        },
        onConfirm(){
            let arr1=[],arr2=[];
            this.menuList.forEach((item,index)=>{
                if (item.checked) {
                    if (this.metaItem) {
                        arr1.push(item[this.valueProp])
                    } else {
                        let obj = {}
                        obj[this.valueProp] = item[this.valueProp];
                        obj[this.labelProp] = item[this.labelProp];
                        arr1.push(obj)
                    }
                    arr2.push(index)
                }
            })
            this.$emit('confirm',arr1,arr2)
            this.close()
        },
        /* 多选时候单个菜单选择 */
        onChangeSelect(value){
            let allChecked = true;
            this.menuList.forEach((item)=>{
                if (!item.checked) {
                    allChecked = false;
                }
            })
            this.selectAll = allChecked
        },
        /* 全选事件 */
        onChangeSelectAll(value) {
            this.menuList.forEach((item)=>{
                item.checked = value
            })
        },
        onItemClick(index,e){
            /* 点击区域判断 */
            let targetClass = e.target.className;
            if (targetClass!=='item-content') return false;
            /* ---------------- */
            if (!this.multiple) {
                /* 单选 */
                let obj = {}
                obj[this.valueProp] = this.menuList[index][this.valueProp];
                obj[this.labelProp] = this.menuList[index][this.labelProp];
                this.$emit('confirm',obj,index)
                this.close()
            }
        },
        onSearch(){
            this.$emit('search',this.searchValue)
        }
    }
}
</script>
 
<style lang="scss">
.packaged-dropdown-menu-trigger-target{
    display: flex;
    align-items: center;
    .drop-icon{
        margin-left: 4px;
    }
}
.el-popover{
    &.el-popper{
        &.packaged-dropdown-menu-popover{
            padding: 0;
        }
    }
}
.packaged-dropdown-menu-popper{
    position: relative;
    width: 100%;
    box-sizing: border-box;
    &.has-bottom,&.has-search{
        min-height: 52px;
    }
    &.has-bottom.has-search{
        min-height: 102px;
    }
    .search-bar,.bottom-row{
        position: absolute;
        width: 100%;
        left: 0;
        box-sizing: border-box;
    }
    .search-bar{
        padding: 8px;
        border-bottom: 1px solid #e4e7ed;
        top:0;
    }
    .bottom-row{
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 4px 8px;
        border-top: 1px solid #e4e7ed;
        bottom: 0;
    }
    .menu-list{
        height: 100%;
    }
    .menu-list-item-group{
        padding: 6px 0;
    }
    .menu-list-item{
        display: flex;
        align-items: center;
        padding: 2px 8px;
        cursor: default;
        &:hover{
            background-color: #F0F8FF;
        }
        .item-checkbox{
            width: 22px;
            flex-shrink: 0;
            display: flex;
            align-items: center;
            .el-checkbox{
                height: auto;
            }
        }
        .item-content{
            flex: 1;
            word-break:break-all;
            word-wrap:break-word; 
            font-size: 14px;
            font-weight: normal;
            line-height: 1.4em;
            color: #333333;
        }
    }
}
</style>