需求:在用列表展示數(shù)據(jù)時(shí),出現(xiàn)了很多項(xiàng)信息需要展示導(dǎo)致表格橫向特別長(zhǎng),展示就不夠明晰,用戶使用起來可能會(huì)覺得抓不住自己的重點(diǎn)。
設(shè)想實(shí)現(xiàn):用戶手動(dòng)選擇表格的列隱藏還是展示,并且記錄用戶選擇的狀態(tài),在下次進(jìn)入該時(shí)仍保留選擇的狀態(tài)。
效果圖如下:
原:
不需要的關(guān)掉默認(rèn)的勾選:
實(shí)現(xiàn)代碼:
HTML部分就是用一個(gè)多選框組件展示列選項(xiàng)
用v-if="colData[i].istrue"
控制顯示隱藏,把列選項(xiàng)傳到checkbox里再綁定勾選事件。
1
2
3
4
5
6
|
<el-popover placement= "right" title= "列篩選" trigger= "click" width= "420" > <el-checkbox-group v-model= "checkedColumns" size= "mini" > <el-checkbox v- for = "item in checkBoxGroup" :key= "item" :label= "item" :value= "item" ></el-checkbox> </el-checkbox-group> <el-button slot= "reference" type= "primary" size= "small" plain><i class= "el-icon-arrow-down el-icon-menu" />列表項(xiàng)展示篩選</el-button> </el-popover> |
1
2
3
4
5
6
7
8
9
10
|
<el-table :data= "attendanceList" @sort-change= "sort" highlight-current-row :row-class-name= "holidayRow" @selection-change= "editAll" ref= "multipleTable" > <el-table-column type= "selection" width= "55" align= "center" ></el-table-column> <el-table-column label= "員工基本信息" > <el-table-column v- if = "colData[0].istrue" align= "center" prop= "user_id" label= "工號(hào)" width= "80" fixed></el-table-column> <el-table-column v- if = "colData[1].istrue" align= "center" prop= "name" label= "姓名" width= "80" fixed></el-table-column> <el-table-column v- if = "colData[2].istrue" align= "center" prop= "age" label= "年齡" width= "60" ></el-table-column> <el-table-column v- if = "colData[3].istrue" align= "center" prop= "gender" label= "性別" width= "80" ></el-table-column> <el-table-column v- if = "colData[4].istrue" align= "center" prop= "department" label= "部門名稱" width= "100" ></el-table-column> </el-table-column> ...... |
js 數(shù)據(jù)存放的data部分
1
2
3
4
5
6
7
8
9
10
|
//列表動(dòng)態(tài)隱藏 colData: [ { title: "工號(hào)" , istrue: true }, { title: "姓名" , istrue: true }, { title: "年齡" , istrue: true }, { title: "性別" , istrue: true }, { title: "部門名稱" , istrue: true }, ], checkBoxGroup: [], checkedColumns: [], |
js 方法實(shí)現(xiàn)部分
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
created() { // 列篩選 this .colData.forEach((item, index) => { this .checkBoxGroup.push(item.title); this .checkedColumns.push(item.title); }) this .checkedColumns = this .checkedColumns let UnData = localStorage.getItem( this .colTable) UnData = JSON.parse(UnData) if (UnData != null ) { this .checkedColumns = this .checkedColumns.filter((item) => { return !UnData.includes(item) }) } }, // 監(jiān)控列隱藏 watch: { checkedColumns(val,value) { let arr = this .checkBoxGroup.filter(i => !val.includes(i)); // 未選中 localStorage.setItem( this .colTable, JSON.stringify(arr)) this .colData.filter(i => { if (arr.indexOf(i.title) != -1) { i.istrue = false ; } else { i.istrue = true ; } }); } }, |
這樣就可以實(shí)現(xiàn)了,并且在刷新頁(yè)面等會(huì)記錄勾選情況,本來想加一個(gè)全選的選擇框,最后沒實(shí)現(xiàn),先這樣用吧。但是肯定有更好的方法,以后優(yōu)化了再更新~
到此這篇關(guān)于vue+element table表格實(shí)現(xiàn)動(dòng)態(tài)列篩選的示例代碼的文章就介紹到這了,更多相關(guān)element table表格動(dòng)態(tài)列篩選內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://blog.csdn.net/m0_46538057/article/details/112480900