vue+element table表格實(shí)現(xiàn)動(dòng)態(tài)列篩選的示例代碼
需求:在用列表展示數(shù)據(jù)時(shí),出現(xiàn)了很多項(xiàng)信息需要展示導(dǎo)致表格橫向特別長(zhǎng),展示就不夠明晰,用戶使用起來(lái)可能會(huì)覺(jué)得抓不住自己的重點(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ì)記錄勾選情況,本來(lái)想加一個(gè)全選的選擇框,最后沒(méi)實(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源碼學(xué)習(xí)之關(guān)于對(duì)Array的數(shù)據(jù)偵聽(tīng)實(shí)現(xiàn)
這篇文章主要介紹了Vue源碼學(xué)習(xí)之關(guān)于對(duì)Array的數(shù)據(jù)偵聽(tīng)實(shí)現(xiàn),Vue使用了一個(gè)方式來(lái)實(shí)現(xiàn)Array類型的監(jiān)測(cè)就是攔截器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04vue前端開(kāi)發(fā)輔助函數(shù)狀態(tài)管理詳解示例
vue的應(yīng)用狀態(tài)管理提供了mapState、mapGetters、mapMutations、mapActions四個(gè)輔助函數(shù),所謂的輔助函數(shù)分別對(duì)State、Getters、Mutations、Actions在完成狀態(tài)的使用進(jìn)行簡(jiǎn)化2021-10-10對(duì)vuex中store和$store的區(qū)別說(shuō)明
這篇文章主要介紹了對(duì)vuex中store和$store的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07vue.js實(shí)例todoList項(xiàng)目
本篇文章主要介紹了vue.js實(shí)例todoList項(xiàng)目,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07