vue+element table表格實(shí)現(xiàn)動態(tài)列篩選的示例代碼
需求:在用列表展示數(shù)據(jù)時(shí),出現(xiàn)了很多項(xiàng)信息需要展示導(dǎo)致表格橫向特別長,展示就不夠明晰,用戶使用起來可能會覺得抓不住自己的重點(diǎn)。
設(shè)想實(shí)現(xiàn):用戶手動選擇表格的列隱藏還是展示,并且記錄用戶選擇的狀態(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="工號" 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部分
//列表動態(tài)隱藏 colData: [ { title: "工號", 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)了,并且在刷新頁面等會記錄勾選情況,本來想加一個(gè)全選的選擇框,最后沒實(shí)現(xiàn),先這樣用吧。但是肯定有更好的方法,以后優(yōu)化了再更新~
到此這篇關(guān)于vue+element table表格實(shí)現(xiàn)動態(tài)列篩選的示例代碼的文章就介紹到這了,更多相關(guān)element table表格動態(tài)列篩選內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue源碼學(xué)習(xí)之關(guān)于對Array的數(shù)據(jù)偵聽實(shí)現(xiàn)
這篇文章主要介紹了Vue源碼學(xué)習(xí)之關(guān)于對Array的數(shù)據(jù)偵聽實(shí)現(xiàn),Vue使用了一個(gè)方式來實(shí)現(xiàn)Array類型的監(jiān)測就是攔截器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04vue前端開發(fā)輔助函數(shù)狀態(tài)管理詳解示例
vue的應(yīng)用狀態(tài)管理提供了mapState、mapGetters、mapMutations、mapActions四個(gè)輔助函數(shù),所謂的輔助函數(shù)分別對State、Getters、Mutations、Actions在完成狀態(tài)的使用進(jìn)行簡化2021-10-10vue.js實(shí)例todoList項(xiàng)目
本篇文章主要介紹了vue.js實(shí)例todoList項(xiàng)目,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07