修改源碼來解決el-select值不匹配導(dǎo)致回顯id的問題
背景介紹
基于vue2+element
el-select數(shù)據(jù)的回顯是根據(jù)id去匹配值的,最近項(xiàng)目出現(xiàn)了回顯id的情況,一查是沒有匹配數(shù)據(jù)的問題,于是就想怎么處理(針對單選的情況)
實(shí)現(xiàn)思路
有下面兩個(gè)方案
- 獲取完值和下拉數(shù)據(jù)后,通過方法遍歷有沒有匹配id的選項(xiàng),沒有就強(qiáng)塞一個(gè)選項(xiàng)進(jìn)去,然后回顯
- 改源碼,看能不能直接回顯,不然來一個(gè)就得處理一次,很麻煩
具體實(shí)現(xiàn)
- 沒有數(shù)據(jù)就塞數(shù)據(jù)
setData(list, id, name) {
let item = list.some(item => item.id === id)
if (item) {
list.push({
id: id,
name: name
})
}
}
實(shí)現(xiàn)比較簡單,就不多說了
- 改源碼,一次搞定
- 先看源碼,看為什么會回顯id,如下
getOption(value) {
let option;
const isObject = Object.prototype.toString.call(value).toLowerCase() === '[object object]';
const isNull = Object.prototype.toString.call(value).toLowerCase() === '[object null]';
const isUndefined = Object.prototype.toString.call(value).toLowerCase() === '[object undefined]';
for (let i = this.cachedOptions.length - 1; i >= 0; i--) {
const cachedOption = this.cachedOptions[i];
const isEqual = isObject
? getValueByPath(cachedOption.value, this.valueKey) === getValueByPath(value, this.valueKey)
: cachedOption.value === value;
if (isEqual) {
option = cachedOption;
break;
}
}
if (option) return option;
const label = (!isObject && !isNull && !isUndefined)
? String(value) : '';
let newOption = {
value: value,
currentLabel: label
};
if (this.multiple) {
newOption.hitState = false;
}
return newOption;
}
- 可以看到,第17、18行,如果有匹配的值,就返回值,如果沒有匹配上,就返回String(value),也就是id
- 要想回顯值,就得把這行改掉,計(jì)劃將20行的newOption改為這個(gè);其中defaultValue就是我們希望回顯的值
let newOption = {
value: value,
currentLabel: this.defaultValue || label
};
- 上面用了一個(gè)defaultValue的prop,通過mixin添加defaultValue
寫一個(gè)element-mixin.js
export default ElementUI => {
ElementUI.Select.mixins[ElementUI.Select.mixins.length] = {
props: {
defaultValue: {
type: String || Number,
default: ''
}
},
}
}
在main.js里面把mixin加上,源碼getOption的修改也寫這里
import ElementUI from 'element-ui'
import addSelectDefaultValue from './mixins/addSelectDefaultValue'
ElementUI.Select.methods.getOption = function (value) {
let option;
const isObject = Object.prototype.toString.call(value).toLowerCase() === '[object object]';
const isNull = Object.prototype.toString.call(value).toLowerCase() === '[object null]';
const isUndefined = Object.prototype.toString.call(value).toLowerCase() === '[object undefined]';
for (let i = this.cachedOptions.length - 1; i >= 0; i--) {
const cachedOption = this.cachedOptions[i];
const isEqual = isObject
? getValueByPath(cachedOption.value, this.valueKey) === getValueByPath(value, this.valueKey)
: cachedOption.value === value;
if (isEqual) {
option = cachedOption;
break;
}
}
if (option) return option;
const label = (!isObject && !isNull && !isUndefined)
? String(value) : '';
let newOption = {
value: value,
currentLabel: this.defaultValue || label
};
if (this.multiple) {
newOption.hitState = false;
}
return newOption;
}
addSelectDefaultValue(ElementUI)
-界面使用的時(shí)候添加一個(gè)default的值就可以了
<el-select v-model="value" clearable placeholder="請選擇">
<el-option
v-for="item in options"
:key="item.value"
:label="item.label"
:value="item.value"
:defaultValue="defaultValue"
>
</el-option>
</el-select>
上面這樣初次進(jìn)入不匹配的時(shí)候就會顯示defaultValue的值而不是id了
帶來的問題
- 得讓后端把值回傳,不然你不知道defaultvalue的值
- 每次clear下拉數(shù)據(jù)的時(shí)候也會回顯defaultvalue,需要添加clear的回調(diào),將defaultvalu設(shè)為""
- 源碼的修改是直接寫在main里面的,如果項(xiàng)目三方包是私有化的可以直接改源碼用,如果不是建議用patch-package打補(bǔ)丁
以上就是修改源碼來解決el-select值不匹配導(dǎo)致回顯id的問題的詳細(xì)內(nèi)容,更多關(guān)于el-select值不匹配導(dǎo)致回顯id的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
JavaScript 創(chuàng)建隨機(jī)數(shù)和隨機(jī)圖片
關(guān)于javascript隨機(jī)數(shù)的,很早以前的文章了,不過內(nèi)容還是不錯(cuò)的,如果想要更多的效果,可以去腳本之家搜下。2009-12-12
妙用Bootstrap的 popover插件實(shí)現(xiàn)校驗(yàn)表單提示功能
最近使用bootstrap開發(fā)項(xiàng)目比較多,在表單校驗(yàn)功能中用popover插件實(shí)現(xiàn)出錯(cuò)提示功能很方面,下面小編給大家?guī)砹艘黄P(guān)于Bootstrap的 popover插件實(shí)現(xiàn)校驗(yàn)表單提示功能的實(shí)現(xiàn)代碼,非常不錯(cuò),感興趣的朋友一起看看吧2016-08-08
js位運(yùn)算在實(shí)際中使用的實(shí)例教程
我們可能很少在編程中用位運(yùn)算,如果沒深入學(xué)習(xí),可能也很難理解,下面這篇文章主要給大家介紹了關(guān)于js位運(yùn)算在實(shí)際中使用的相關(guān)資料,需要的朋友可以參考下2022-03-03
微信小程序授權(quán)登錄解決方案的代碼實(shí)例(含未通過授權(quán)解決方案)
這篇文章主要介紹了微信小程序授權(quán)登錄解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
javascript實(shí)現(xiàn)頁面刷新時(shí)自動清空表單并選中的方法
這篇文章主要介紹了javascript實(shí)現(xiàn)頁面刷新時(shí)自動清空表單并選中的方法,涉及javascript中reset與focus方法的相關(guān)使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
JS常見問題之為什么點(diǎn)擊彈出的i總是最后一個(gè)
最近有很多朋友問我,為什么點(diǎn)擊彈出的i總是最后一個(gè),于是抽時(shí)間寫了這篇文章,特此分享到腳本之家平臺,供大家參考2016-01-01
兩種JavaScript的AES加密方式(可與Java相互加解密)
這篇文章主要介紹了兩種JavaScript的AES加密方式(可與Java相互加解密) 的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-08-08

