Vue之Element級(jí)聯(lián)選擇器多選傳值以及回顯方式(樹形結(jié)構(gòu))
Vue Element級(jí)聯(lián)選擇器多選傳值以及回顯
后臺(tái)需要的數(shù)據(jù)格式:‘a’, ‘a1’, ‘b’, ‘b1’(字符串以逗號(hào)分隔)
后臺(tái)返回的數(shù)據(jù):同上
級(jí)聯(lián)組建
<el-cascader ? :options="industrialList" ? placeholder="請(qǐng)選擇產(chǎn)業(yè)鏈" ? :props="industrialProps" ? @change="changeIndustrial" ? clearable ? v-model="form.industrialChain" ></el-cascader>
組建選擇處理
changeIndustrial (val) { ? let ids = []; ? val.forEach((item) => { ? ? ids.push(item[item.length - 1]); ? }) ? this.industrialChainResult = ids.join(','); }
處理后臺(tái)返回?cái)?shù)據(jù)進(jìn)行回顯
codes
: 后臺(tái)返回的逗號(hào)分隔的字符串。this.industrialList
:從后臺(tái)取回來渲染級(jí)聯(lián)多選的樹形結(jié)構(gòu)數(shù)據(jù)。categoryResultList
:樹形結(jié)構(gòu)子集字段名
? ? // 級(jí)聯(lián)回顯 ? ? industrialChinaShow (codes) { ? ? ? let echoTreeArr = []; ? ? ? let eachAry = codes.split(',') ? ? ? let itemAry = [];//分類樹組件,每一項(xiàng)的code數(shù)組 ? ? ? // 遞歸分類數(shù)據(jù) ? ? ? let recursionCategory = (data) => { ? ? ? ? let len = data.length; ? ? ? ? for (let i = 0; i < len; i++) {//循環(huán)data參數(shù),匹配回顯的code ? ? ? ? ? itemAry.push(data[i].code);//構(gòu)建分類樹數(shù)組項(xiàng),入棧 ? ? ? ? ? for (let j = 0; j < eachAry.length; j++) {//遍歷子節(jié)點(diǎn)分類code,拼湊成數(shù)組項(xiàng)code,并終止循環(huán) ? ? ? ? ? ? if (eachAry[j] == data[i].code) {//匹配到子節(jié)點(diǎn)code ? ? ? ? ? ? ? echoTreeArr.push(JSON.parse(JSON.stringify(itemAry)));//push進(jìn)樹分類數(shù)據(jù) ? ? ? ? ? ? ? eachAry.splice(j, 1);//刪除以匹配到的code ? ? ? ? ? ? ? break; ? ? ? ? ? ? } ? ? ? ? ? } ? ? ? ? ? if (eachAry.length <= 0) {//所有回顯code匹配完成后,跳出循環(huán) ? ? ? ? ? ? break; ? ? ? ? ? } else if (data[i].categoryResultList && data[i].categoryResultList.length > 0) {// 如果存在子分類,遞歸繼續(xù) ? ? ? ? ? ? recursionCategory(data[i].categoryResultList); ? ? ? ? ? } ? ? ? ? ? itemAry.pop();//出棧 ? ? ? ? } ? ? ? } ? ? ? recursionCategory(this.industrialList);//調(diào)用遞歸 ? ? ? this.form.industrialChain = echoTreeArr; ? ? },
在后臺(tái)返回的地方調(diào)用第三部的方法
this.industrialChinaShow(res.industrialChain) // res.industrialChain 后臺(tái)返回的'a', 'a1', 'b', 'b1'(字符串以逗號(hào)分隔)
好了 就這樣。
element-ui動(dòng)態(tài)級(jí)聯(lián)選擇器回顯問題解決
場(chǎng)景描述
后臺(tái)返回類目數(shù)組,其中有一級(jí)類目;二級(jí)類目,三級(jí)類目;這種情況下如何回顯數(shù)據(jù)
效果圖如下:
先給大家展示一些數(shù)據(jù)結(jié)構(gòu),后面會(huì)給大家一一講解;尤其是回顯那部分
數(shù)據(jù)結(jié)構(gòu)如下
id
:自己的idlevel
:等級(jí)pid
:父級(jí)id,0位第一級(jí)name
:名字
[ { id:1, level:0,pid:0,name:'測(cè)試1' }, { id:2, level:0,pid:0,name:'測(cè)試2' }, { id:11, level:1,pid:1,name:'測(cè)試1-1' }, { id:12, level:2,pid:11,name:'測(cè)試1-1-1' } ]
先貼上html部分
<el-cascader :props="categoryProps" v-model="cascaderData"></el-cascader>
然后再貼上data數(shù)據(jù)中的部分
這兒需要注意幾個(gè)點(diǎn):label、value需要改為你數(shù)據(jù)結(jié)構(gòu)一致的字段;lazyLoad函數(shù)中的node有許多參數(shù),如果目前的不能滿足你的需求;你可以查看里面的一些參數(shù)是否有你需要的數(shù)值;現(xiàn)在data中的lazyLoad函數(shù)主要是一些默認(rèn)值;
cascaderData: '12', categoryProps: { emitPath: false, label:'name', // 字段必須統(tǒng)一 value:'id', // 字段必須統(tǒng)一 lazy: true, lazyLoad (node, resolve) { const { level,value } = node; console.log(value); let nodes = []; if (level == 0) { // 第一級(jí) nodes = catalogueList.filter(v=>{return v.level == 0}); }else { // 后面兩級(jí) nodes = catalogueList.filter(v=>{return v.pid == value}); }; resolve(nodes.map(v=>({ id: v.id, name: v.name, leaf: level >= 2 }))); } }, // 級(jí)聯(lián)選擇器顯示數(shù)據(jù)
回顯的方法
將以下方法賦值給lazyLoad函數(shù)即可實(shí)現(xiàn)回顯了;
邏輯:
- cascaderData:是選擇的參數(shù)最后一級(jí)id
- value:我們?cè)谶x擇是(鼠標(biāo)點(diǎn)擊選擇的)會(huì)觸發(fā)并返回id,如果沒有選擇點(diǎn)擊,則返回undefined(我們就在這兒進(jìn)行判斷是回顯還是手動(dòng)觸發(fā))
- 先說回顯:threeFind:我們根據(jù)有的最后一級(jí)id(cascaderData),去查找改數(shù)據(jù)并查詢到他父級(jí)(twoFind);然后根據(jù)他父級(jí)查找到(第三級(jí))的所有數(shù)據(jù)(threeData)
- 然后根據(jù)二級(jí)數(shù)據(jù)(twoFind)去查找一級(jí)(oneFind),然后根據(jù)一級(jí)(oneFind)查找到二級(jí)的所有數(shù)據(jù);一級(jí)不用查(level==0)全是
- 這個(gè)時(shí)候開始將對(duì)應(yīng)(二級(jí),三級(jí))的數(shù)據(jù)放到children下面;children是默認(rèn)的值;會(huì)自動(dòng)去查找
- 在放置第三級(jí)的數(shù)據(jù)的時(shí)候需要注意一個(gè)值:leaf,為true時(shí)就是結(jié)束;也必須寫這個(gè);否則選擇器不會(huì)顯示,但是展開的時(shí)候是顯示狀態(tài)
- 現(xiàn)在回顯也完成了;但是我們從二級(jí)或者一級(jí)選擇分類的時(shí)候,會(huì)出現(xiàn)一級(jí)或者二級(jí)數(shù)據(jù)跑到二級(jí)或者三級(jí)目錄下等清楚;這個(gè)時(shí)候就需要將選中的數(shù)據(jù)查找到
- 然后將其子集的數(shù)據(jù)查找出來;并判斷如果是第三級(jí)就leaf:true;即可完成
- 趕緊去試試吧!
setLazyLoad(node, resolve) { const { level,value } = node; const cascaderData = this.cascaderData; // 第一級(jí)數(shù)據(jù) let nodes = []; nodes = catalogueList.filter(v=>{return v.level == 0}); // 選中的第三級(jí)某位數(shù)據(jù) const threeFind = catalogueList.find(v=>{return v.id == cascaderData}); if (value) { // 查詢數(shù)據(jù) const unknownFind = catalogueList.find(v=>v.id == value); if (level == 1) { let twoData = catalogueList.filter(v=>v.pid == unknownFind.id); nodes = twoData; // 第二級(jí) }else if (level == 2) { let threeData = catalogueList.filter(v=>v.pid == unknownFind.id); nodes = threeData.map(v=>{return {id:v.id,name:v.name,level:v.level,leaf:true}}); // 第三級(jí) }; }else { // 選中的第二級(jí)某位數(shù)據(jù) let twoFind = catalogueList.find(v=>v.id == threeFind.pid); // 第三級(jí)數(shù)據(jù) let threeData = catalogueList.filter(v=>v.pid == twoFind.id); // 選中的第一級(jí)某位數(shù)據(jù) const oneFind = catalogueList.find(v=>{return v.id == twoFind.pid}); // 第二級(jí)數(shù)據(jù) let twoData = catalogueList.filter(v=>{return v.pid == oneFind.id}); // nodes = nodes.map(v=>{return {id:v.id,name:v.name,level:v.level}}); // 第一級(jí) const oneIndex = nodes.findIndex(v=>{return v.id == oneFind.id}); nodes[oneIndex].children = twoData; // 第二級(jí) const twoIndex = nodes[oneIndex].children.findIndex(v=>v.id == twoFind.id); nodes[oneIndex].children[twoIndex].children = threeData.map(v=>{ // 第三季 return { id:v.id, name:v.name, level:v.level, leaf: true, } }); } resolve(nodes); },
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue使用Antd中a-table實(shí)現(xiàn)表格數(shù)據(jù)列合并展示示例代碼
文章介紹了如何在Vue中使用Ant?Design的a-table組件實(shí)現(xiàn)表格數(shù)據(jù)列合并展示,通過處理函數(shù)對(duì)源碼數(shù)據(jù)進(jìn)行操作,處理相同數(shù)據(jù)時(shí)合并列單元格2024-11-11vue+video.js實(shí)現(xiàn)視頻播放列表
這篇文章主要為大家詳細(xì)介紹了vue+video.js實(shí)現(xiàn)視頻播放列表,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10Vue3使用Proxy實(shí)現(xiàn)數(shù)據(jù)監(jiān)聽的原因分析
在本篇文章里小編給大家整理的是一篇關(guān)于Vue3使用Proxy實(shí)現(xiàn)數(shù)據(jù)監(jiān)聽的原因分析內(nèi)容,有需要的朋友們可以跟著學(xué)習(xí)參考下。2021-11-11使用websocket和Vue2中的props實(shí)時(shí)更新數(shù)據(jù)方式
這篇文章主要介紹了使用websocket和Vue2中的props實(shí)時(shí)更新數(shù)據(jù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08Vue無法讀取HTMLCollection列表的length問題解決
這篇文章主要介紹了Vue無法讀取HTMLCollection列表的length問題解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03vuejs開發(fā)組件分享之H5圖片上傳、壓縮及拍照旋轉(zhuǎn)的問題處理
這篇文章主要介紹了vuejs開發(fā)組件分享之H5圖片上傳、壓縮及拍照旋轉(zhuǎn)的問題處理,需要的朋友可以參考下2017-03-03Vue+Spring Boot簡(jiǎn)單用戶登錄(附Demo)
這篇文章主要介紹了Vue+Spring Boot簡(jiǎn)單用戶登錄,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11