如何解決element-ui動態(tài)加載級聯(lián)選擇器默認(rèn)選中問題
前言
最近在開發(fā)任務(wù)中碰到需要在新增和修改時使用動態(tài)加載級聯(lián)選擇器,但是當(dāng)在修改時設(shè)置默認(rèn)選中項時,出現(xiàn)了后端數(shù)據(jù)返回慢的情況,就導(dǎo)致無法選中和級聯(lián)框選中了但input框不顯示的問題,網(wǎng)上找到的方法也不是很有效,還得使用ref查看組件實例和element-ui源碼來尋找解決方法。
完整的實現(xiàn)代碼在最后
1.解決方法
我們知道使用動態(tài)加載的級聯(lián)選擇器需要使用lazyLoad函數(shù),那就先開始尋找lazyLoad函數(shù),使用ref查看組件實例,可以看到是在panel下。
再來看看源碼,可以發(fā)現(xiàn)只需要傳入Object數(shù)據(jù)即可,如:this.$refs.xxx.panel.lazyLoad(val)。
知道了傳參和調(diào)用,但是傳參的參數(shù)從哪來?如下圖可以看到是從this.$refs.xxx.panel.menus[0]循環(huán)比對獲取。
如果后端數(shù)據(jù)返回快的話可以達(dá)到要實現(xiàn)的選中效果,如果后端數(shù)據(jù)返回的較慢就會出現(xiàn)沒選中的情況(可以自己在lazyLoad加個一秒延遲測試),這時就需要使用handleExpand(handleExpand也在panel下),并且要在請求結(jié)束后執(zhí)行handleExpand,如何在請求結(jié)束時執(zhí)行handleExpand,我們對代碼進(jìn)行如下修改:
先聲明一個waitItemExecution函數(shù)并賦給menus[0]循環(huán)出的數(shù)據(jù),最后在lazyLoad中執(zhí)行waitItemExecution函數(shù)。效果如下圖,是不是發(fā)現(xiàn)input框沒有數(shù)據(jù),這還是得去element-ui源碼內(nèi)尋找答案,接著往下看。
當(dāng)級聯(lián)選擇器選擇完畢時,watch下的checkedValue會觸發(fā),并執(zhí)行computePresentContent接著computePresentContent再調(diào)用computePresentText。
是不是認(rèn)為只要對checkedValue進(jìn)行賦值,input框就有值了?可以試著去查看ref獲取的實例下的checkedValue,你會發(fā)現(xiàn)已經(jīng)有值在里面,那為什么沒有觸發(fā)呢?因為!isEqual(val, value)是判斷不相等為true,而我們調(diào)用的方法不全,導(dǎo)致val和value相等,所以if里面的方法不執(zhí)行,其實我們只需要調(diào)用computePresentText。
waitItemExecution函數(shù)修改如下:
這樣就實現(xiàn)了動態(tài)加載級聯(lián)選擇器默認(rèn)選中的問題。
2.不同層級間的默認(rèn)選中解決方法
以上的方法只解決了一層和二層的級聯(lián)的情況,超過二層的級聯(lián)情況不適用。對不同層級的默認(rèn)選中只需要封裝函數(shù)并互相調(diào)用就可實現(xiàn)(一到N層都可使用),以下是完整的代碼:
<template> <div id="app"> <el-cascader v-model="current" :props="props" ref="cascader"></el-cascader> </div> </template> <script> let id = 0; let list = [ { value: '0', }, { value: '1', }, { value: '2', }, { value: '3', }, ] export default { name: 'App', data() { return { current: [], props: { lazy: true, lazyLoad(node, resolve) { const {level} = node; function f() { list = list.map((item, i) => ({ label: level ? `${node.value}-${i}` : item.value, value: level ? `${node.value}-${i}` : item.value, leaf: item.value === '2' && level === 0 ? true : `${node.value}-${i}` === '3-2' ? true : level >= 2 })) resolve(list) } if (level > 0) { setTimeout(() => { f() node.func && node.func(node) }, 1000) return } f() } } }; }, mounted() { this.current = ['1', '1-3', '1-3-0'] // this.current = ['3', '3-2'] // this.current = ['2'] // this.current = ['0', '0-2', '0-2-3'] for (let item of this.$refs.cascader.panel.menus[0]) { if (item.value === this.current[0]) { this.loadItem(item) return } } }, methods: { handleItem(val) { if (val && val instanceof Object) { this.loadItem(val) } }, loadItem(val) { val.func = this.waitItemExecution this.$refs.cascader.panel.lazyLoad(val) }, waitItemExecution(val) { for (let item of val.children) { if (this.current[val.level] === item.value) { this.handleItem(item) break } } this.$refs.cascader.panel.handleExpand(val) this.$refs.cascader.computePresentText() }, } } </script>
到此這篇關(guān)于如何解決element-ui動態(tài)加載級聯(lián)選擇器默認(rèn)選中問題的文章就介紹到這了,更多相關(guān)element-ui動態(tài)加載內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
element plus tree拖動節(jié)點交換位置和改變層級問題(解決方案)
圖層list里有各種組件,用element plus的tree來渲染,可以把圖片等組件到面板里,面板是容器,非容器組件,比如圖片、文本等,就不能讓其他組件拖進(jìn)來,這篇文章主要介紹了element plus tree拖動節(jié)點交換位置和改變層級問題(解決方案),需要的朋友可以參考下2024-04-04Vue3?$emit用法指南(含選項API、組合API及?setup?語法糖)
這篇文章主要介紹了Vue3?$emit用法指南,使用?emit,我們可以觸發(fā)事件并將數(shù)據(jù)傳遞到組件的層次結(jié)構(gòu)中,本文通過實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-07-07解決antd 表單設(shè)置默認(rèn)值initialValue后驗證失效的問題
這篇文章主要介紹了解決antd 表單設(shè)置默認(rèn)值initialValue后驗證失效的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11