element-ui 遠(yuǎn)程搜索組件el-select在項(xiàng)目中組件化的實(shí)現(xiàn)代碼
在項(xiàng)目中發(fā)現(xiàn)使用el-select時寫的比較多重復(fù)代碼,還有就是同一個頁面使用el-select會出現(xiàn)label值會顯示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 '請輸入關(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來顯示;
在父組件中:
<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時,暫時無法解決;只是使用了另一個組件:vue-multiselect
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue.js自定義組件實(shí)現(xiàn)v-model雙向數(shù)據(jù)綁定的示例代碼
這篇文章主要介紹了vue.js自定義組件實(shí)現(xiàn)v-model雙向數(shù)據(jù)綁定的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01VSCode寫vue項(xiàng)目一鍵生成.vue模版,修改定義其他模板的方法
這篇文章主要介紹了VSCode寫vue項(xiàng)目一鍵生成.vue模版,修改定義其他模板的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-04-04Vue axios與Go Frame后端框架的Options請求跨域問題詳解
這篇文章主要介紹了Vue axios與Go Frame后端框架的Options請求跨域問題詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03在vue中使用vue-echarts-v3的實(shí)例代碼
這篇文章主要介紹了在vue中使用vue-echarts-v3的實(shí)例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-09-09vue實(shí)現(xiàn)輸入框只允許輸入數(shù)字
在vue項(xiàng)目中,輸入框只允許輸入數(shù)字,現(xiàn)將自己使用的一種方法記錄,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2023-11-11vue?實(shí)現(xiàn)動態(tài)設(shè)置元素的高度
這篇文章主要介紹了在vue中實(shí)現(xiàn)動態(tài)設(shè)置元素的高度,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08快速解決Vue、element-ui的resetFields()方法重置表單無效的問題
這篇文章主要介紹了快速解決Vue、element-ui的resetFields()方法重置表單無效的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08