使用Element ui Table組件動(dòng)態(tài)控制列的顯示隱藏方式
Element ui Table組件動(dòng)態(tài)控制列的顯示隱藏
最近遇到一個(gè)需求,要求可以動(dòng)態(tài)控制 table 列表中的列的顯示與隱藏,并且將選中的列進(jìn)行存儲(chǔ),下次進(jìn)入頁(yè)面仍展示上次勾選的列。
經(jīng)過(guò)查閱資料,實(shí)現(xiàn)了這個(gè)功能,創(chuàng)建一個(gè)Table.vue文件
組件封裝代碼
如下所示:
<template>
<div>
<el-table
ref="dataTable"
:data="tableData"
header-cell-class-name="headerCell"
cell-class-name="contentCell"
fit
border
lazy
size="mini"
>
<!-- 索引列 -->
<el-table-column type="index" width="50" />
<!-- 描述和操作列 -->
<template v-for="(item, index) in tableHead">
<el-table-column
v-if="tableHead[index].isShow"
:key="index"
:prop="item.columnName"
:label="item.columnTitle"
align="center"
>
<template slot-scope="scope">
<div>{{scope.row[item.columnName]}}</div>
</template>
</el-table-column>
</template>
<!-- 切換顯示隱藏列 -->
<el-table-column width="40">
<template slot="header" slot-scope="scope">
<a href="javascript:;" rel="external nofollow" >
<el-popover :ref="`popover-${scope.$index}`" width="150" placement="left" trigger="click">
<el-checkbox-group v-model="columnSelecteds">
<el-checkbox
v-for="item in tableHead"
v-show="item.columnTitle"
:key="item.columnTitle"
:label="item.columnTitle"
/>
</el-checkbox-group>
<i slot="reference" class="el-icon-s-grid" />
</el-popover>
</a>
</template>
</el-table-column>
</el-table>
</div>
</template>
<script>
export default {
props: {
tableData: {
type: Array,
required: true,
default: () => []
},
tableHead: {
type: Array,
required: true,
default: () => []
}
},
data() {
return {
columnSelecteds: [] // 已選擇的項(xiàng)
}
},
watch: {
/* @title 監(jiān)聽列顯示隱藏**/
columnSelecteds(newArrayVal) {
// 計(jì)算為被選中的列標(biāo)題數(shù)組
var nonSelecteds = this.tableHead
.filter((item) => newArrayVal.indexOf(item.columnTitle) === -1)
.map((item) => item.columnTitle)
// 根據(jù)計(jì)算結(jié)果進(jìn)行表格重繪
this.tableHead.filter((item) => {
const isNonSelected = nonSelecteds.indexOf(item.columnTitle) !== -1
if (isNonSelected) {
// 隱藏未選中的列
item.isShow = false
this.$nextTick(() => {
this.$refs.dataTable.doLayout()
})
} else {
// 顯示已選中的列
item.isShow = true
this.$nextTick(() => {
this.$refs.dataTable.doLayout()
})
}
})
localStorage.setItem('columnSelecteds', JSON.stringify(newArrayVal))
}
},
created() {
// 初始化要顯示的列
if (JSON.parse(localStorage.getItem('columnSelecteds')) && JSON.parse(localStorage.getItem('columnSelecteds')).length) {
this.columnSelecteds = JSON.parse(localStorage.getItem('columnSelecteds'))
} else {
this.tableHead.forEach((item) => {
if (item.isShow) {
this.columnSelecteds.push(item.columnTitle)
}
})
localStorage.setItem('columnSelecteds', JSON.stringify(this.columnSelecteds))
}
}
}
</script>
<style lang="scss" scoped>
::v-deep .headerCell {
padding: 5px 0;
background: #f5f7fa;
color: #606266;
}
::v-deep .contentCell {
padding: 5px 0;
text-align: center;
}
::v-deep .el-table .cell{
white-space: nowrap;
}
.el-checkbox:last-of-type{
margin-right: 30px;
}
</style>組件用法
如下所示:
新建一個(gè)index.vue,將Table組件引入使用:
<template>
<div>
<Table
:table-data="tableData"
:table-head="tableHead"
/>
</div>
</template>
<script>
import Table from './components/Table'
export default {
components: {
Table,
},
data() {
return {
// 表頭數(shù)據(jù)
tableHead: [{
'columnName': 'name',
'columnTitle': '姓名',
'isShow': true
}, {
'columnName': 'date',
'columnTitle': '日期',
'isShow': false
}, {
'columnName': 'address',
'columnTitle': '地址',
'isShow': true
}],
// 表格數(shù)據(jù)
tableData: [{
date: '2016-05-02',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1518 弄'
}, {
date: '2016-05-04',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1517 弄'
}, {
date: '2016-05-01',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1519 弄'
}, {
date: '2016-05-03',
name: '王小虎',
address: '上海市普陀區(qū)金沙江路 1516 弄'
}]
}
}
}
</script>效果如下圖
默認(rèn)只展示姓名和地址列

緩存勾選的列

可以將日期列選中,在列表中進(jìn)行展示

緩存勾選的列

總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- 如何解決Element-ui的el-table固定列后出現(xiàn)的表格錯(cuò)位問(wèn)題
- ElementUI實(shí)現(xiàn)el-table行列合并的操作步驟
- elementui的table根據(jù)是否符合需求合并列的實(shí)現(xiàn)代碼
- Element-UI?el-table對(duì)循環(huán)產(chǎn)生的空白列賦默認(rèn)值方式
- ElementUI實(shí)現(xiàn)el-table列寬自適應(yīng)的代碼詳解
- vue2+elementui的el-table固定列會(huì)遮住橫向滾動(dòng)條及錯(cuò)位問(wèn)題解決方案
相關(guān)文章
解決vux 中popup 組件Mask 遮罩在最上層的問(wèn)題
這篇文章主要介紹了解決vux 中popup 組件Mask 遮罩在最上層的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11
Vue引入highCharts實(shí)現(xiàn)數(shù)據(jù)可視化
這篇文章主要為大家詳細(xì)介紹了Vue引入highCharts實(shí)現(xiàn)數(shù)據(jù)可視化,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
vue arco.design錨點(diǎn)Anchor使用方式
這篇文章主要介紹了vue arco.design錨點(diǎn)Anchor使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-04-04
關(guān)于el-form表單驗(yàn)證中的validator與validate使用時(shí)的問(wèn)題
這篇文章主要介紹了關(guān)于el-form表單驗(yàn)證中的validator與validate使用時(shí)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
vue3輸入無(wú)效路由跳轉(zhuǎn)到指定error頁(yè)面問(wèn)題
這篇文章主要介紹了vue3輸入無(wú)效路由跳轉(zhuǎn)到指定error頁(yè)面問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
Vue純前端實(shí)現(xiàn)導(dǎo)出Excel并修改樣式
這篇文章主要為大家詳細(xì)介紹了Vue如何利用xlsx-style庫(kù)實(shí)現(xiàn)導(dǎo)出Excel并修改樣式的功能,文中的示例代碼講解詳細(xì),有需要的可以參考下2024-01-01
antd vue表格可編輯單元格以及求和實(shí)現(xiàn)方式
這篇文章主要介紹了antd vue表格可編輯單元格以及求和實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
解決vue-router路由攔截造成死循環(huán)問(wèn)題
這篇文章主要介紹了解決vue-router路由攔截造成死循環(huán)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08

