vue使用el-table篩選tree樹(shù)形結(jié)構(gòu)的數(shù)據(jù)問(wèn)題
實(shí)現(xiàn)難點(diǎn)
1.對(duì)于普通列數(shù)據(jù)el-table表格可做到多條件篩選,但是對(duì)于帶tree樹(shù)形結(jié)構(gòu)類型的數(shù)據(jù)只能篩選到最上層節(jié)點(diǎn),子節(jié)點(diǎn)不會(huì)篩選
2.考慮到缺陷,因此查看文檔只能通過(guò)文檔提供的filter-change事件手動(dòng)篩選數(shù)據(jù)。
思路
1.通過(guò)filter-change事件使用filterObj對(duì)象保存點(diǎn)擊的篩選狀態(tài)
2.先將當(dāng)前樹(shù)形數(shù)據(jù)轉(zhuǎn)變成普通列數(shù)據(jù),再進(jìn)行手動(dòng)過(guò)濾,過(guò)濾后以 普通列數(shù)據(jù)展示
3.當(dāng)無(wú)篩選條件時(shí)再以樹(shù)形結(jié)構(gòu)展示
圖例

實(shí)現(xiàn)步驟
1.數(shù)據(jù)初始化
data:{
return{
targetNode:{},//總數(shù)據(jù),保持tree數(shù)據(jù)類型
tableData:[],//展示在table上的數(shù)據(jù)
filterObj:{isEnable:[],filterTypeAttr:[],filterTypeCondition:[]},//過(guò)濾條件,由于表 格組件filterchange方法只會(huì)提示當(dāng)前的篩選項(xiàng),
//所以使用filterObj來(lái)保存所有篩選過(guò)的選項(xiàng)
}
}2.表格
<el-table
:data="tableData"
row-key="id"
border
default-expand-all
v-loading="loading"
@filter-change="filterChange"
ref="filterTable"
:tree-props="{children: 'children', hasChildren: 'hasChildren'}">
<el-table-column prop="index" label="順序" width="180">
<template slot-scope="scope">{{(scope.$index + 1)}}</template>
</el-table-column>
<el-table-column prop="filterName" label="篩選項(xiàng)" width="180">
<template slot-scope="scope"><span :class="
[scope.row.filterTypeCondition==1?'filter-name':'']">{{ scope.row.filterName }}
</span><i v-if="scope.row.filterTypeAttr!==2" class="el-icon-plus" style="color:
#03a9f4;font-size: 12px;margin-left: 10px;cursor: pointer;"
@click="addCondition(scope.row)"></i>
</template>
</el-table-column>
<el-table-column
prop="filterTypeAttr"
column-key="filterTypeAttr"
:filters="[{ text: '自定義', value: '1' }, { text: '機(jī)構(gòu)名稱', value: '2'},{ text: '無(wú)',
value: ''}]"
label="篩選類型屬性">
<template slot-scope="scope">{{ scope.row.filterTypeAttr==1?'自定義':
scope.row.filterTypeAttr==2?'機(jī)構(gòu)名稱':''}}</template>
<template slot="header" slot-scope="scope">
<!-- {{filterObj.filterTypeAttr.length==0?'篩選類型屬
性':filterObj.filterTypeCondition.length==1 &&
filterObj.filterTypeCondition[0]==1?'1-篩選類型'
:filterObj.filterTypeCondition.length==1 &&
filterObj.filterTypeCondition[0]==2?'2-篩選條件':'篩選類型屬性-全部'}} -->
{{filterObj.filterTypeAttr.length==3?'篩選類型屬性-全部':'篩選類型
屬性'+(filterObj.filterTypeAttr.includes(1)?'-自定義':'')+
(filterObj.filterTypeAttr.includes(2)?'-機(jī)構(gòu)名稱':'')+
(filterObj.filterTypeAttr.includes(0)?'-無(wú)':'')}}
</template>
</el-table-column>
<el-table-column
prop="filterTypeCondition"
column-key="filterTypeCondition"
:filters="[{ text: '1-篩選類型', value: '1' }, { text: '2-篩選條件', value: '2'}]"
label="標(biāo)識(shí)">
<template slot-scope="scope">{{ scope.row.filterTypeCondition==1?'1-篩選類型':'2-篩選條件'}}</template>
<template slot="header" slot-scope="scope">
{{filterObj.filterTypeCondition.length==0?'標(biāo)識(shí)':filterObj.filterTypeCondition.length==1 && filterObj.filterTypeCondition[0]==1?'標(biāo)識(shí)-1-篩選類型':filterObj.filterTypeCondition.length==1 && filterObj.filterTypeCondition[0]==2?'標(biāo)識(shí)-2-篩選條件':'標(biāo)識(shí)-全部'}}
</template>
</el-table-column>
<el-table-column prop="defaultSelected" label="是否屬于默認(rèn)展示項(xiàng)">
<template slot-scope="scope">
{{scope.row.defaultSelected==1?'是':scope.row.defaultSelected==0?'否':''}}
</template>
</el-table-column>
<el-table-column prop="action" label="操作" width="250">
<template slot-scope="scope">
<span v-if="scope.row.filterTypeCondition==2" class="scope-span" :class="
[scope.row.rule && scope.row.rule!=''?'filter-span':'']"
@click="showRegular(scope.row)">規(guī)則式</span>
<span class="scope-span" @click="updateFilter(scope.row)">編輯</span>
<el-popconfirm
title="是否確定刪除?"
@confirm="delFilter(scope.row)"
>
<span class="scope-span" slot="reference" >刪除</span>
</el-popconfirm>
</template>
</el-table-column>
<el-table-column
prop="isEnable"
label="狀態(tài)"
column-key="isEnable"
:filters="[{ text: '啟用', value: '1' }, { text: '禁用', value: '0' }]"
>
<template slot-scope="scope">{{ scope.row.isEnable==1?'啟用':'禁用'}}</template>
<template slot="header" slot-scope="scope">
{{filterObj.isEnable.length==0?'狀態(tài)'
:filterObj.isEnable.length==1 && filterObj.isEnable[0]==1?'狀態(tài)-啟用'
:filterObj.isEnable.length==1 && filterObj.isEnable[0]==0?'狀態(tài)-禁用':'狀態(tài)-全部'}}
</template>
</el-table-column>
</el-table>要點(diǎn):
default-expand-all:默認(rèn)展開(kāi)全部子節(jié)點(diǎn)filterChange:獲取點(diǎn)擊的篩選條件狀態(tài),對(duì)應(yīng)列表頭上設(shè)置:filters屬性的列
<template slot="header" slot-scope="scope">設(shè)置篩選后用來(lái)替換表頭的label內(nèi)容
3.filterChange函數(shù)
filterChange(filters){//觸發(fā)過(guò)濾事件時(shí)過(guò)濾數(shù)據(jù)
console.log('filters',filters);
if(filters['isEnable']){
this.filterObj['isEnable'] = filters['isEnable'].map(Number);
}
if(filters['filterTypeAttr']){
this.filterObj['filterTypeAttr'] = filters['filterTypeAttr'].map(Number);
}
if(filters['filterTypeCondition']){
this.filterObj['filterTypeCondition'] = filters['filterTypeCondition'].map(Number);
}
if(this.filterObj['isEnable'].length==0 && this.filterObj['filterTypeAttr'].length==0 && this.filterObj['filterTypeCondition'].length==0){
this.tableData = this.targetNode.filterList;
}else{
let dataList = [];
this.targetNode.filterList.forEach(item=>{//層級(jí)數(shù)據(jù)轉(zhuǎn)變成普通列數(shù)據(jù)
let obj = {
filterName:item.filterName,
filterTypeAttr:item.filterTypeAttr,
sort:item.sort,
isEnable:item.isEnable,
columnId:item.columnId,
id:item.id,
filterTypeCondition:1,
index:item.index
}
dataList.push(obj);
if(item.children){
dataList = dataList.concat(this.getLineData(item.children))
}
})
console.log('線性數(shù)據(jù)',dataList);
if(this.filterObj['isEnable'].length>0){//狀態(tài)
dataList = dataList.filter(item=>{
return this.filterObj['isEnable'].includes(item.isEnable)
})
}
if(this.filterObj['filterTypeCondition'].length>0){//標(biāo)識(shí)
dataList = dataList.filter(item=>{
return this.filterObj['filterTypeCondition'].includes(item.filterTypeCondition)
})
}
if(this.filterObj['filterTypeAttr'].length>0){//屬性
console.log('11',this.filterObj['filterTypeAttr']);
dataList = dataList.filter(item=>{
return this.filterObj['filterTypeAttr'].includes(item.filterTypeAttr?item.filterTypeAttr:0)
})
}
console.log('過(guò)濾數(shù)據(jù)',dataList);
this.tableData = dataList;
}
},
getLineData(data){//層級(jí)數(shù)據(jù)變成行數(shù)據(jù)
let list = [];
data.forEach(item=>{
let obj = {};
if(item.filterTypeCondition==1){//篩選類型
obj = {
filterName:item.filterName,
filterTypeAttr:item.filterTypeAttr,
sort:item.sort,
isEnable:item.isEnable,
parentId:item.parentId,
id:item.id,
filterTypeCondition:1,
index:item.index
}
}else{
obj = {
filterName:item.filterName,
sort:item.sort,
isEnable:item.isEnable,
parentId:item.parentId,
id:item.id,
defaultSelected:item.defaultSelected,
filterTypeCondition:2,
index:item.index
}
}
list.push(obj);
if(item.children){
list = list.concat(this.getLineData(item.children))
}
})
return list;
},步驟
- 1.fiters參數(shù)為當(dāng)前點(diǎn)擊的篩選項(xiàng),數(shù)據(jù)結(jié)構(gòu)如下:

- 2.使用filterObj保存當(dāng)前篩選狀態(tài),篩選重置時(shí)為[]空數(shù)組
- 3.保存后判斷filterObj各項(xiàng)是否都是空數(shù)組,是則直接賦值
- 4.否則將樹(shù)形結(jié)構(gòu)轉(zhuǎn)換成普通列數(shù)據(jù),注意考慮淺克隆的問(wèn)題,getLineData函數(shù)作用是將子節(jié)點(diǎn)children也插入到普通列中
- 5.數(shù)據(jù)轉(zhuǎn)換成普通列數(shù)據(jù)后進(jìn)行過(guò)濾
實(shí)現(xiàn)效果

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vite使用unplugin-auto-import實(shí)現(xiàn)vue3中的自動(dòng)導(dǎo)入
本文主要介紹了Vite使用unplugin-auto-import實(shí)現(xiàn)vue3中的自動(dòng)導(dǎo)入,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-06-06
Vue使用Pinia輕松實(shí)現(xiàn)狀態(tài)管理
pinia,一個(gè)基于Vue3的狀態(tài)管理庫(kù),它可以幫助開(kāi)發(fā)人員管理Vue應(yīng)用程序的狀態(tài),本文主要為大家介紹了Pinia的用法,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-06-06
Vue多頁(yè)面配置打包性能優(yōu)化方式(解決加載包太大加載慢問(wèn)題)
這篇文章主要介紹了Vue多頁(yè)面配置打包性能優(yōu)化方式(解決加載包太大加載慢問(wèn)題),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
vue實(shí)現(xiàn)一個(gè)獲取按鍵展示快捷鍵效果的Input組件
這篇文章主要介紹了vue如何實(shí)現(xiàn)一個(gè)獲取按鍵展示快捷鍵效果的Input組件,幫助大家更好的理解和使用vue框架,感興趣的朋友可以了解下2021-01-01
對(duì)vue中v-on綁定自定事件的實(shí)例講解
今天小編就為大家分享一篇對(duì)vue中v-on綁定自定事件的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09
vuex中this.$store.commit和this.$store.dispatch的基本用法實(shí)例
在vue的項(xiàng)目里常常會(huì)遇到父子組件間需要進(jìn)行數(shù)據(jù)傳遞的情況,下面這篇文章主要給大家介紹了關(guān)于vuex中this.$store.commit和this.$store.dispatch的基本用法的相關(guān)資料,需要的朋友可以參考下2023-01-01
vue新建項(xiàng)目并配置標(biāo)準(zhǔn)路由過(guò)程解析
這篇文章主要介紹了vue新建項(xiàng)目并配置標(biāo)準(zhǔn)路由過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12

