ElementUI实现在下拉列表里面进行搜索功能详解

 更新时间:2023年04月01日 15:28:09   作者:寒墨茗殇  
有时候需要用到下拉列表全选和搜索,下面这篇文章主要给大家介绍了关于ElementUI实现在下拉列表里面进行搜索功能的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下

分析:

  1. 首先我们需要实现上图的效果,然后Element-UI的el-select是没有的,所以需要自己写
  2. 我们需要用到el-popover组件,然后使用它的v-model="visible"来实现控制显示
  3. 我们在el-popover的slot="reference" 放一个el-select
    1. 使用popper-append-to-body="false"不需要插入浮动元素
    2. 使用popper-class="hide-popper"定义浮窗class为hide-popper,并设置display:none,这样选中了就不会存在el-select的下拉选项
    3. el-option 循环下面选择的list里面的元素,这样就可以在el-select展示选中的并存在删除
    4. el-select双向绑定的就是自定义选择的数组

html:

<template>
	<div class="arrbox">
		<!-- 通过visible控制显示还是隐藏 -->
		<el-popover
		v-model="visible"
		placement="bottom-start"
		width="auto"
		>
		<div slot="reference" class="check-select">
			<!-- popper-append-to-body:不需要插入浮动元素,popper-class:设置类名并隐藏 -->
			<el-select
			ref="select"
			v-model="currentval"
			:style="{width:`${width}px`,height:`${height}`}"
			multiple
			:placeholder="placeholder"
			:popper-append-to-body="false"
			popper-class="hide-popper"
			style="width:100%"
			@visible-change="visibleChange"
			@focus="getFocus"
			> <el-option
			v-for="item in selectItem"
			:key="`${item.value}_k`"
			:label="item.label"
			:value="item.value"
			/></el-select>
		</div>
		<!-- selectBxClick让select强制选中 -->
		<div class="selectMain" :style="{'min-width':`${width-20}px`}" @click="selectBxClick">
			<div class="seachButton">
			<el-select
				v-model="seachValue"
				placeholder=" 请选择筛选"
				style="width:70%;margin-right:10px;max-width:195px"
				@visible-change="selectBxClick()"
			>
				<el-option
				v-for="item in seachList"
				:key="item.value"
				:value="item.value"
				:label="item.label"
				/>
			</el-select>
			<div class="btn" @click="seachBtn">搜索</div>
			</div>
			 <div class="selectDiv">
                              <div v-for="item in list.filter(n=>n.value=='all')" :key="item.value" class="list" :class="[currentval.indexOf(item.value)!=-1?'selected':'',item.value=='all'?'allCheck':'']" @click="clickItem(item)">{{ item.label }}</div>

                              <div class="selectDivAuto">
                                <div v-for="item in list.filter(n=>n.value!='all')" :key="item.value" class="list" :class="[currentval.indexOf(item.value)!=-1?'selected':'',item.value=='all'?'allCheck':'']" @click="clickItem(item)">{{ item.label }}</div>
                              </div>

                            </div>
		</div>
		</el-popover>
	</div>
	</template>

js:

使用getFocus获取是否聚焦,聚焦了让visible=true,这样就可以显示出自定义的下拉选择项

通过visibleChange实施监听el-select,控制el-popover显示

在点击自定义的下拉选择项时,通过@click="selectBxClick"el-select一直聚焦,这样箭头就会一直向上

通过 @click="seachBtn"getList获取列表,具体需要自己去自定义

// 模拟获取的数据
	const seachClickList = [{value: '1',label: '测试1',type: '1'},{value: '2',label: '测试2',type: '1'},{value: '3',label: '测试3',type: '1'},{value: '4',label: '测试4',type: '2'},{value: '5',label: '测试5',type: '2'},{value: '6',label: '测试6',type: '2'},{value: '7',label: '测试7',type: '2'}]
	export default {
	model: {
		prop: 'parentArr',
		event: 'change-parentArr'
	},
	props: {
		parentArr: {
		type: Array,
		default() {
			return []
		}
		},
		// 传入选中的item,主要时防止list里面没有选中的数据
		parentSelectItem: {
		type: Array,
		default() {
			return []
		}
		},
		width: {
		type: Number,
		default: 300
		},
		height: {
		type: Number,
		default: 30
		},
		placeholder: {
		type: String,
		default: '请输入'
		}
	},
	data() {
		return {
		seachList: [
			{
			value: '1',
			label: '条件一'
			},
			{
			value: '2',
			label: '条件二'
			}
		],
		visible: false,
		currentval: [],
		list: [],
		selectItem: [],
		seachValue: '1'
		}
	},
	watch: {
		seachValue: {
		handler(value) {
			this.getList(value)
		},
		deep: true,
		immediate: true
		},
		parentArr: {
		handler(value) {
			this.currentval = value
		},
		deep: true,
		immediate: true
		},
		parentSelectItem: {
		handler(value) {
			this.selectItem =  value.map(n => {
                              if (n.value == 'all') {
                                n.label = '全部'
                              }
                              return n
                            })
		},
		deep: true,
		immediate: true
		},
		currentval: {
                        handler(value) {
                                this.$emit('change-parentArr', value)
                        }
		}
	},
	created() {
	},
	methods: {
		getList(value) {
                        this.list = [{
                                label: '全部',
                                value: 'all'
                        }, ...seachClickList.filter(n => n.type == value)]
                        this.getSelectItem()
		},
		// 获取选中的item
		getSelectItem() {
                        const noItemList = this.currentval.map(n => {
                                if (this.selectItem.findIndex(i => i.value == n) == -1) {
                                return n
                                }
                                return null
                        }).filter(n => n != null)
                        noItemList.forEach(item => {
                                const index = this.list.findIndex(i => i.value == item)
                                if (index != -1) {
                                this.selectItem.push(this.list[index])
                                }
                        })
		},
		getFocus() {
                        this.visible = true
		},
		visibleChange(data) {
                        this.visible = data
		},
		selectBxClick() {
                        // 避免点击框体时组件消失
                        this.$refs.select.visible = true
		},
		// 选择
		clickItem(item) {
                      const index = this.currentval.indexOf(item.value)
                      if (index == -1) {
                        if (item.value == 'all') {
                          this.currentval = ['all']
                          this.selectItem = [{
                            label: '全部',
                            value: 'all'
                          }]
                        } else {
                          this.currentval.push(item.value)
                          this.selectItem.push(item)
                          const currentvalIndex = this.currentval.indexOf('all')
                          const selectItemIndex = this.selectItem.findIndex(n => n.value == 'all')
                          if (currentvalIndex != -1 && selectItemIndex != -1) {
                            this.selectItem.splice(selectItemIndex, 1)
                            this.currentval.splice(currentvalIndex, 1)
                          }
                        }
                      } else {
                        const itemIndex = this.selectItem.findIndex(n => n.value == item.value)
                        this.selectItem.splice(itemIndex, 1)
                        this.currentval.splice(index, 1)
                      }
                    },
		// 搜索
		seachBtn() {
                        this.getList()
		}
	}
	}

css:

selected属性使用了el-select的样式,让样子尽量一致

.arrbox {
display: inline-block;
}
.check-select{
::v-deep.hide-popper{
	display: none;
}
}
::v-deep .el-input__suffix{
i:not(.el-select__caret){
	display: none;
}
}
.selectMain {
width: 100%;
height: 100%;
.seachButton{
	width: 100%;
	align-items: center;
	display: flex;
	div.btn{
	width: 25%;
	max-width: 70px;
	max-width: 80px;
	height: 40px;
	display: flex;
	align-items: center;
	justify-content: center;
	font-size: 12px;
	color: #fff;
	background-color: #409EFF;
	border-radius: 5px;
	cursor: pointer;
	}
}
.selectDiv{
        width: 100%;
        max-width: 500px;
        margin-top: 10px;
        padding:  0 10px 0 0;
        .list{
          width: 100%;
          padding: 10px 20px 10px 10px;
          color: #666;
          cursor: pointer;
          position: relative;
          &.selected{
            color: #409EFF;
            &::after{
              position: absolute;
              right: 0px;
              top: 50%;
              transform: translateY(-50%);
              font-family: element-icons;
              content: "\e6da";
              font-size: 12px;
              font-weight: 700;
              -webkit-font-smoothing: antialiased;
            }
          }
        }
        .selectDivAuto{
          width: calc(100% + 15px);
          max-height: 300px;
          overflow-y: auto;
          .list{
            padding: 10px 30px 10px 10px;
            &.selected::after{
              right: 10px;
            }
          }
        }

      }
}
.allCheck{
border-bottom: 1px solid rgb(228, 225, 225);
}

使用

<template>
	<seachSelectInput v-model="from.tag" :parentSelectItem='selectItem' :width="302" placeholder="请选择标签" />
</template>
<script>
import seachSelectInput from ./seachSelectInput'
export default {
components: {
	seachSelectInput
},
data(){
	return{
		from:{
			tag:['1']
		},
		selectItem:[
			{
			value: '1',
			label: '测试1'
			}
		]
	}
}
}

总结

到此这篇关于ElementUI实现在下拉列表里面进行搜索功能的文章就介绍到这了,更多相关ElementUI下拉列表搜索内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Vue 使用v-model实现控制子组件显隐效果

    Vue 使用v-model实现控制子组件显隐效果

    v-model 可以实现双向绑定的效果,允许父组件控制子组件的显示/隐藏,同时允许子组件自己控制自身的显示/隐藏,本文给大介绍Vue 使用v-model实现控制子组件显隐,感兴趣的朋友一起看看吧
    2023-11-11
  • html中创建并调用vue组件的几种方法汇总

    html中创建并调用vue组件的几种方法汇总

    这篇文章主要介绍了html中创建并调用vue组件的几种方法汇总,帮助大家更好的理解和使用vue框架,感兴趣的朋友可以了解下
    2020-11-11
  • Vue.js 2.0窥探之Virtual DOM到底是什么?

    Vue.js 2.0窥探之Virtual DOM到底是什么?

    大家可能听说Vue.js 2.0已经发布,并且在其中新添加如了一些新功能。其中一个功能就是“Virtual DOM”。那么下面这篇文章就来给大家详细介绍Vue.js 2.0中的Virtual DOM到底是什么?需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-02-02
  • vue3+springboot部署到Windows服务器的详细步骤

    vue3+springboot部署到Windows服务器的详细步骤

    这篇文章主要介绍了vue3+springboot部署到Windows服务器,配置Nginx时,因为现在是把vue前端交给了Nginx代理,所以这里的端口号不一定是我们在vue项目中设置的端口号,本文给大家介绍的非常详细,需要的朋友参考下吧
    2022-10-10
  • Vue中div contenteditable 的光标定位方法

    Vue中div contenteditable 的光标定位方法

    今天小编就为大家分享一篇Vue中div contenteditable 的光标定位方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-08-08
  • Vue在H5 项目中使用融云进行实时个人单聊通讯

    Vue在H5 项目中使用融云进行实时个人单聊通讯

    这篇文章主要介绍了Vue在H5 项目中使用融云进行实时个人单聊通讯,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-12-12
  • Nuxt引用cookie-universal-nuxt在服务端请求cookie方式

    Nuxt引用cookie-universal-nuxt在服务端请求cookie方式

    这篇文章主要介绍了Nuxt引用cookie-universal-nuxt在服务端请求cookie方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-10-10
  • 解决vue数据更新但table内容不更新的问题

    解决vue数据更新但table内容不更新的问题

    这篇文章主要给大家介绍了vue数据更新table内容不更新解决方法,文中有详细的代码示例供大家作为参考,感兴趣的同学可以参考阅读一下
    2023-08-08
  • 利用VUE框架,实现列表分页功能示例代码

    利用VUE框架,实现列表分页功能示例代码

    本篇文章主要介绍了利用VUE框架,实现列表分页功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-01-01
  • vue中输入框事件的使用及数值校验方式

    vue中输入框事件的使用及数值校验方式

    这篇文章主要介绍了vue中输入框事件的使用及数值校验方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-08-08

最新评论