element-ui 遠(yuǎn)程搜索組件el-select在項(xiàng)目中組件化的實(shí)現(xiàn)代碼
在項(xiàng)目中發(fā)現(xiàn)使用el-select時(shí)寫的比較多重復(fù)代碼,還有就是同一個(gè)頁(yè)面使用el-select會(huì)出現(xiàn)label值會(huì)顯示value值,
el-select組件化:
<template>
<el-select
:class="obj&&keyword[keywordAttr.label]? 'selected': ''"
:value="keyword"
:placeholder="obj && keyword[keywordAttr.label]? keyword[keywordAttr.label]: placeholder"
filterable
:clearable="clear"
remote
:multiple="multiple"
:loading="selectLoading"
:reserve-keyword="reserve"
:remote-method="remoteMethod"
:style="{width: width}"
:disabled="disabled"
:value-key="keywordAttr.id"
@change="changeSelect"
@clear="handleClear"
@blur="handleBlur"
@focus="handleFocus"
@visible-change="handlerVisible"
>
<el-option
v-for="item in keywordOptions"
:key="item[keywordAttr.id]"
:label="item[keywordAttr.label]"
:value="obj? item: item[keywordAttr.value]"
:disabled="item.disabled"
>
<slot :item="item"/>
</el-option>
</el-select>
</template>
<script>
export default {
name: 'SelectRemote',
props: {
value: {
type: [String, Object],
default: ''
},
reserve: {
type: Boolean,
default: true
},
clear: {
type: Boolean,
default: false
},
disabled: {
type: Boolean,
default: false
},
multiple: {
type: Boolean,
default: false
},
selectLoading: {
type: Boolean,
default: false
},
width: {
type: String,
default: '100%'
},
keywordOptions: {
type: Array,
default: function() {
return []
}
},
keywordAttr: {
type: Object,
required: true,
default: function() {
return {
id: '',
label: '',
value: ''
}
}
},
obj: {
type: Boolean,
default: false
},
placeholder: {
type: String,
default: function() { return '請(qǐng)輸入關(guān)鍵詞' }
}
},
data() {
return {
keyword: this.value
}
},
watch: {
value(newVal) {
this.keyword = newVal
}
},
methods: {
remoteMethod(query) {
this.$emit('remoteMethod', query)
},
changeSelect(item) {
this.$emit('changeSelect', item)
},
handleClear() {
this.$emit('clear')
},
handleBlur() {
this.$emit('blur')
},
handleFocus() {
this.$emit('focus')
},
handlerVisible() {
this.$emit('visible-change')
}
}
}
</script>
<style lang="scss" scoped>
.selected ::-webkit-input-placeholder{
color: #606266 !important;
}
</style>
主要是使用了placeholder來(lái)顯示;
在父組件中:
<SeletcRemote
v-model="nodeOperate.saleEmp"
:keyword-attr="nodeObjPerson"
:keyword-options="empOptions"
:clear="true"
:obj="true"
:select-loading="selectLoading"
@remoteMethod="remoteMethod"
@changeSelect="handleProductChange($event, nodeOperate, 'saleEmp')"
/>
nodeObjPerson: {
id: 'id',
label: 'empName'
}
handleProductChange(val, row, field) {
this.$set(row, field, val)
}
可以在單選的的狀態(tài)下完美解決了label顯示值;
在多選狀態(tài)下如何顯示?
multipletrue時(shí),暫時(shí)無(wú)法解決;只是使用了另一個(gè)組件:vue-multiselect
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
快速修改antd?vue單個(gè)組件的默認(rèn)樣式
這篇文章主要介紹了快速修改antd?vue單個(gè)組件的默認(rèn)樣式方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2022-08-08
Vue-cli 移動(dòng)端布局和動(dòng)畫(huà)使用詳解
這篇文章主要介紹了Vue-cli和移動(dòng)端布局和動(dòng)畫(huà)使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
vue全局過(guò)濾器概念及注意事項(xiàng)和基本使用方法
這篇文章主要給大家分享了vue全局過(guò)濾器概念及注意事項(xiàng)和基本使用方法,下面文字圍繞vue全局過(guò)濾器的相關(guān)資料展開(kāi)具體的詳細(xì)內(nèi)容,需要的朋友可以參考一下,希望對(duì)你有所幫助2021-11-11
vue中js實(shí)現(xiàn)點(diǎn)擊復(fù)制文本到剪貼板的3種方案
今天遇到一個(gè)復(fù)制粘貼的需求,研究之后發(fā)現(xiàn)太簡(jiǎn)單了,這篇文章主要給大家介紹了關(guān)于vue中js實(shí)現(xiàn)點(diǎn)擊復(fù)制文本到剪貼板的3種方案,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-09-09
一篇文章,教你學(xué)會(huì)Vue CLI 插件開(kāi)發(fā)
這篇文章主要介紹了Vue CLI插件開(kāi)發(fā),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
vue項(xiàng)目啟動(dòng)后沒(méi)有局域網(wǎng)地址問(wèn)題
這篇文章主要介紹了vue項(xiàng)目啟動(dòng)后沒(méi)有局域網(wǎng)地址問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09

