ElementUI實現(xiàn)在下拉列表里面進行搜索功能詳解
分析:
- 首先我們需要實現(xiàn)上圖的效果,然后Element-UI的el-select是沒有的,所以需要自己寫
- 我們需要用到el-popover組件,然后使用它的v-model="visible"來實現(xiàn)控制顯示
- 我們在el-popover的slot="reference" 放一個el-select
- 使用popper-append-to-body="false"不需要插入浮動元素
- 使用popper-class="hide-popper"定義浮窗class為hide-popper,并設(shè)置display:none,這樣選中了就不會存在el-select的下拉選項
- el-option 循環(huán)下面選擇的list里面的元素,這樣就可以在el-select展示選中的并存在刪除
- el-select雙向綁定的就是自定義選擇的數(shù)組
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:設(shè)置類名并隱藏 --> <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
實施監(jiān)聽el-select
,控制el-popover
顯示
在點擊自定義的下拉選擇項時,通過@click="selectBxClick"
讓el-select
一直聚焦,這樣箭頭就會一直向上
通過 @click="seachBtn"
和getList
獲取列表,具體需要自己去自定義
// 模擬獲取的數(shù)據(jù) 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里面沒有選中的數(shù)據(jù) 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' } ] } } }
總結(jié)
到此這篇關(guān)于ElementUI實現(xiàn)在下拉列表里面進行搜索功能的文章就介紹到這了,更多相關(guān)ElementUI下拉列表搜索內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue 使用v-model實現(xiàn)控制子組件顯隱效果
v-model 可以實現(xiàn)雙向綁定的效果,允許父組件控制子組件的顯示/隱藏,同時允許子組件自己控制自身的顯示/隱藏,本文給大介紹Vue 使用v-model實現(xiàn)控制子組件顯隱,感興趣的朋友一起看看吧2023-11-11html中創(chuàng)建并調(diào)用vue組件的幾種方法匯總
這篇文章主要介紹了html中創(chuàng)建并調(diào)用vue組件的幾種方法匯總,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下2020-11-11Vue.js 2.0窺探之Virtual DOM到底是什么?
大家可能聽說Vue.js 2.0已經(jīng)發(fā)布,并且在其中新添加如了一些新功能。其中一個功能就是“Virtual DOM”。那么下面這篇文章就來給大家詳細介紹Vue.js 2.0中的Virtual DOM到底是什么?需要的朋友可以參考借鑒,下面來一起看看吧。2017-02-02vue3+springboot部署到Windows服務(wù)器的詳細步驟
這篇文章主要介紹了vue3+springboot部署到Windows服務(wù)器,配置Nginx時,因為現(xiàn)在是把vue前端交給了Nginx代理,所以這里的端口號不一定是我們在vue項目中設(shè)置的端口號,本文給大家介紹的非常詳細,需要的朋友參考下吧2022-10-10Vue中div contenteditable 的光標定位方法
今天小編就為大家分享一篇Vue中div contenteditable 的光標定位方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08Nuxt引用cookie-universal-nuxt在服務(wù)端請求cookie方式
這篇文章主要介紹了Nuxt引用cookie-universal-nuxt在服務(wù)端請求cookie方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10解決vue數(shù)據(jù)更新但table內(nèi)容不更新的問題
這篇文章主要給大家介紹了vue數(shù)據(jù)更新table內(nèi)容不更新解決方法,文中有詳細的代碼示例供大家作為參考,感興趣的同學(xué)可以參考閱讀一下2023-08-08