修改源碼來解決el-select值不匹配導(dǎo)致回顯id的問題
背景介紹
基于vue2+element
el-select數(shù)據(jù)的回顯是根據(jù)id去匹配值的,最近項(xiàng)目出現(xiàn)了回顯id的情況,一查是沒有匹配數(shù)據(jù)的問題,于是就想怎么處理(針對(duì)單選的情況)
實(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)比較簡單,就不多說了
- 改源碼,一次搞定
- 先看源碼,看為什么會(huì)回顯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="請(qǐng)選擇">
<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í)候就會(huì)顯示defaultValue的值而不是id了
帶來的問題
- 得讓后端把值回傳,不然你不知道defaultvalue的值
- 每次clear下拉數(shù)據(jù)的時(shí)候也會(huì)回顯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的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
JavaScript存儲(chǔ)方式Cookie到IndexedDB全解析
這篇文章主要介紹了JavaScript存儲(chǔ)方式Cookie到IndexedDB的相關(guān)資料,IndexedDB是一種客戶端存儲(chǔ)技術(shù),允許Web應(yīng)用程序在用戶的瀏覽器中存儲(chǔ)和檢索數(shù)據(jù),即使在沒有網(wǎng)絡(luò)連接的情況下也能工作,需要的朋友可以參考下2026-01-01
用JavaScript 判斷用戶使用的是 IE6 還是 IE7
判斷IE瀏覽器的腳本,方便根據(jù)瀏覽器不懂,支持不同的代碼的分別調(diào)用。2008-01-01
JavaScript實(shí)現(xiàn)忘記密碼功能的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何使用HTML、CSS和JavaScript實(shí)現(xiàn)一個(gè)完整的忘記密碼功能,文中的示例代碼講解詳細(xì),需要的可以參考一下2024-01-01
JavaScript實(shí)現(xiàn)返回頂部按鈕案例
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)返回頂部按鈕案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
Openlayers實(shí)現(xiàn)距離面積測(cè)量
這篇文章主要為大家詳細(xì)介紹了Openlayers實(shí)現(xiàn)距離面積測(cè)量,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09
JavaScript 用Node.js寫Shell腳本[譯]
你懂JavaScript嗎?你需要寫一個(gè)Shell腳本嗎?那么你應(yīng)該試一下Node.js,它很容易安裝,而且很適合通過寫Shell腳本來學(xué)習(xí)它2012-09-09
javascript 刪除數(shù)組中重復(fù)項(xiàng)(uniq)
巧妙去除數(shù)組中的重復(fù)項(xiàng)的方法參考,需要的朋友可以參考下。2010-01-01
WebView啟動(dòng)支付寶客戶端支付失敗的問題小結(jié)
這篇文章主要介紹了WebView啟動(dòng)支付寶客戶端支付失敗的問題小結(jié)的相關(guān)資料,需要的朋友可以參考下2017-01-01

