vue+element table表格實(shí)現(xiàn)動(dòng)態(tài)列篩選的示例代碼
需求:在用列表展示數(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里再綁定勾選事件。
<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>
<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部分
//列表動(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)部分
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)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Vue.js實(shí)現(xiàn)多條件篩選、搜索、排序及分頁(yè)的表格功能
- vue分類篩選filter方法簡(jiǎn)單實(shí)例
- vuejs實(shí)現(xiàn)本地?cái)?shù)據(jù)的篩選分頁(yè)功能思路詳解
- 基于Vue實(shí)現(xiàn)的多條件篩選功能的詳解(類似京東和淘寶功能)
- vue+elementUI實(shí)現(xiàn)表格關(guān)鍵字篩選高亮
- VUE實(shí)現(xiàn)移動(dòng)端列表篩選功能
- vue商城中商品“篩選器”功能的實(shí)現(xiàn)代碼
- vue實(shí)現(xiàn)前端列表多條件篩選
- vue3自定義動(dòng)態(tài)不同UI組件篩選框案例
相關(guān)文章
解決vue-cli單頁(yè)面手機(jī)應(yīng)用input點(diǎn)擊手機(jī)端虛擬鍵盤彈出蓋住input問題
今天小編就為大家分享一篇解決vue-cli單頁(yè)面手機(jī)應(yīng)用input點(diǎn)擊手機(jī)端虛擬鍵盤彈出蓋住input問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08
vuex2中使用mapGetters/mapActions報(bào)錯(cuò)的解決方法
這篇文章主要介紹了vuex2中使用mapGetters/mapActions報(bào)錯(cuò)解決方法 ,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-10-10
vue實(shí)現(xiàn).md文件預(yù)覽功能的兩種方法詳解
這篇文章主要介紹了Vue預(yù)覽.md文件的兩種方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2025-04-04
Vue實(shí)現(xiàn)實(shí)時(shí)監(jiān)聽頁(yè)面寬度高度變化
這篇文章主要為大家詳細(xì)介紹了Vue如何實(shí)現(xiàn)實(shí)時(shí)監(jiān)聽頁(yè)面寬度高度變化,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03
npm ERR! code 128的錯(cuò)誤問題解決方法
這篇文章主要介紹了解決npm ERR! code 128的錯(cuò)誤問題,本文給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-02-02

