vue+elementUI-el-table實(shí)現(xiàn)動(dòng)態(tài)顯示隱藏列方式
elementUI-el-table動(dòng)態(tài)顯示隱藏列
在實(shí)際工作場(chǎng)景中,我們?cè)谡故緮?shù)據(jù)時(shí),會(huì)遇到展示數(shù)據(jù)過(guò)多此時(shí)會(huì)將數(shù)據(jù)分頁(yè)展示,但是如果數(shù)據(jù)列展示過(guò)多,會(huì)造成每列數(shù)據(jù)相對(duì)比較擁擠,并且所有的列數(shù)據(jù)不一定都是必須展示的。
可以將其先隱藏,用戶(hù)需要時(shí)才將其顯示在列表之中。所以本文章結(jié)合vue+elementUI--中的el-table以及分頁(yè)實(shí)現(xiàn)表格列的動(dòng)態(tài)隱藏與顯示。
實(shí)現(xiàn)思路:將表格展示+分頁(yè)+顯示/隱藏列 組件化,其中利用slot動(dòng)態(tài)展示不同的表格數(shù)據(jù),父組件引用此組件傳遞相應(yīng)切必須的參數(shù),當(dāng)分頁(yè)或顯示/隱藏列動(dòng)態(tài)變化時(shí)將數(shù)據(jù)再返回父組件,父組件中的列根據(jù)回傳的數(shù)據(jù)利用v-if實(shí)現(xiàn)列的顯示與隱藏(此代碼為第一版!)
主要代碼如下
子組件(trendsTable.vue)主要代碼:
<!-- create by crystalSong 分頁(yè)+table+動(dòng)態(tài)設(shè)置表格列的隱藏與顯示 -->
<template>
? <div class='trends_container'>
? ? ? <div class="table_container">
? ? ? ? ? ? <el-table ref="trendsTable" :data="tableList" fit stripe style="width: 100%" border
? ? ? ? ? ? @selection-change="handleSelectionChange">
? ? ? ? ? ? ? ? <slot></slot>//此處用于列表靈活展示
? ? ? ? ? ? </el-table>
? ? ? </div>
? ? ? <div class="pagination_trends_wrap">
? ? ? ? ? ? <div class="trends_oprt_wrap">
//將所有列展示在此,可以點(diǎn)擊進(jìn)行隱藏與顯示
? ? ? ? ? ? ? ? <el-popover placement="top-start" width="280" trigger="click">
? ? ? ? ? ? ? ? ? ? <div class="trends_btn_wrap">
? ? ? ? ? ? ? ? ? ? ? ? <el-checkbox-group v-model="visibleList" size="small" @change="visibleListChange">
? ? ? ? ? ? ? ? ? ? ? ? ? ? <el-checkbox?
? ? ? ? ? ? ? ? ? ? ? ? ? ? v-for="(col, index) in columnList" :key="index"
? ? ? ? ? ? ? ? ? ? ? ? ? ? :label="col.label"
? ? ? ? ? ? ? ? ? ? ? ? ? ? border
? ? ? ? ? ? ? ? ? ? ? ? ? ? >{{col.value}}</el-checkbox>
? ? ? ? ? ? ? ? ? ? ? ? </el-checkbox-group>
? ? ? ? ? ? ? ? ? ? </div>
? ? ? ? ? ? ? ? ? ? <el-button slot="reference" size="small">隱藏/顯示列</el-button>
? ? ? ? ? ? ? ? </el-popover>
? ? ? ? ? ? </div>
? ? ? ? ? ? <div class="pagination_wrap" v-if="total > 0">
//分頁(yè)區(qū)域
? ? ? ? ? ? ? ? <template>
? ? ? ? ? ? ? ? ? ? <el-pagination style="text-align: right;" @size-change="handleSizeChange"
? ? ? ? ? ? ? ? ? ? @current-change="handleCurrentChange" :current-page.sync="currentPage"
? ? ? ? ? ? ? ? ? ? :page-sizes="[10,25,50,100]" :page-size.sync="currentSize"
? ? ? ? ? ? ? ? ? ? layout="total, sizes, prev, pager, next, jumper" :total="total" background>
? ? ? ? ? ? ? ? ? ? </el-pagination>
? ? ? ? ? ? ? ? </template>
? ? ? ? ? ? </div>
? ? ? </div>
? </div>
</template>
<script>
? export default {
? ? name: 'trendsTable',
? ? props: {
? ? ? ? tableList:{//列表數(shù)據(jù)
? ? ? ? ? ? type: Array,
? ? ? ? ? ? require: true,
? ? ? ? ? ? default: _ => []
? ? ? ? },
? ? ? ? hideColumnIndexs:{//需要隱藏的列的下標(biāo)即表格數(shù)據(jù)的序號(hào)從0開(kāi)始
? ? ? ? ? ? type: Array,
? ? ? ? ? ? default: _ => []
? ? ? ? },
? ? ? ? pageSize:{//每頁(yè)幾條數(shù)據(jù)
? ? ? ? ? ? type:Number,
? ? ? ? ? ? default:10,
? ? ? ? },
? ? ? ? pageNumber:{//當(dāng)前頁(yè)碼
? ? ? ? ? ? type:Number,
? ? ? ? ? ? default:1,
? ? ? ? },
? ? ? ? total:{//總數(shù)據(jù)條數(shù)
? ? ? ? ? ? required: true,
? ? ? ? ? ? type:Number,
? ? ? ? ? ? default:0,
? ? ? ? },
? ? },
? ? components: {},
? ? data() {
? ? ? ? return {
? ? ? ? ? ? visibleList:[],//顯示/隱藏列的選中下標(biāo)數(shù)據(jù)集合
? ? ? ? ? ? columnList:[],//表格所有列名稱(chēng)數(shù)據(jù)列表
? ? ? ? };
? ? },
? ? created() {
? ? ? ??
? ? },
? ? mounted() {
? ? ? ? let _this = this;
? ? ? ? var curHideList = [];
//頁(yè)面初始化:動(dòng)態(tài)獲取表格有用的所有列生成并放入復(fù)選列表并記錄初始隱藏列
? ? ? ? this.$refs.trendsTable.$children.forEach((obj,index) => {?
? ? ? ? ? ? if(obj.type){
? ? ? ? ? ? ? ? _this.columnList.push(
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? 'label':index,
? ? ? ? ? ? ? ? ? ? ? ? 'value':obj.type == 'selection' ? '選擇' : obj.label,
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? );
? ? ? ? ? ? ? ? // 記錄初始展示的列
? ? ? ? ? ? ? ? if (_this.hideColumnIndexs.indexOf(index) === -1) {
? ? ? ? ? ? ? ? ? ? _this.visibleList.push(index)
? ? ? ? ? ? ? ? ? ? curHideList[index] = false;
? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? curHideList.push(true);
? ? ? ? ? ? ? ? } ? ? ? ? ? ? ?
? ? ? ? ? ? }
? ? ? ? });
//此向父組件傳遞列是否隱藏的數(shù)組
? ? ? ? _this.$emit('getHideColist',curHideList);
? ? },
? ? methods: {
? ? ? ?visibleListChange(item){
? ? ? ? ? ? // console.log('change',item)
? ? ? ? ? ? var curHideList = [];
? ? ? ? ? ? this.columnList.forEach((obj,index) => {?
? ? ? ? ? ? ? ? curHideList[index] = false;
? ? ? ? ? ? ? ? // 更改顯示隱藏列
? ? ? ? ? ? ? ? if (item.indexOf(index) === -1) {
? ? ? ? ? ? ? ? ? ? curHideList[index] = true;
? ? ? ? ? ? ? ? }?
? ? ? ? ? ? });
? ? ? ? ? ? this.$emit('getHideColist',curHideList);
? ? ? ? },
? ? },
? ? computed: {
?
? ? },
? ? watch: {},
? }
</script>
<style lang='less' scoped>
? ? .trends_container{
? ? ? ? .pagination_trends_wrap{
? ? ? ? ? ? margin: 20px 0;
? ? ? ? ? ? position: relative;
? ? ? ? }
? ? ? ? .trends_oprt_wrap{
? ? ? ? ? ? display: inline-block;
? ? ? ? ? ? vertical-align: top;
? ? ? ? ? ? width: 100px;
? ? ? ? }
? ? ? ? .pagination_wrap{
? ? ? ? ? ? display: inline-block;
? ? ? ? ? ? vertical-align: top;
? ? ? ? ? ? width: calc(100% - 105px);
? ? ? ? ? ? margin: 0 !important;
? ? ? ? }
? ? }
</style>
<style lang="less">
? ? .trends_btn_wrap{
? ? ? ? .el-checkbox-group{
? ? ? ? ? ? label{
? ? ? ? ? ? ? ? margin: 0 !important;
? ? ? ? ? ? ? ? margin: 0 10px !important;
? ? ? ? ? ? ? ? margin-bottom: 15px !important;
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? .table_container .el-table .cell{
? ? ? ? text-overflow: initial !important;
? ? }
</style>引用此組件時(shí)主要代碼:
// 引入-table-組件 import TrendsTable from "@/components/trendsTable.vue";
主要代碼就是根據(jù)子組件返回的數(shù)組利用v-if進(jìn)行判斷
<trends-table :tableList="onrenewalTableList"
? ? ? ? ? ? ? ? ? ? :hideColumnIndexs='[]' ref="trendtable"
? ? ? ? ? ? ? ? ? ? :pageSize.sync="onrenewalForm.pageSize"
? ? ? ? ? ? ? ? ? ? :pageNumber.sync="onrenewalForm.pageNumber"
? ? ? ? ? ? ? ? ? ? :total="mbePageTotal"
? ? ? ? ? ? ? ? ? ? @getHideColist="getHideColist"
? ? ? ? ? ? ? ? ? ? @pagination = "paginationHadle"
? ? ? ? ? ? ? ? ? ? @selection-change="handleSelectionChange"
? ? ? ? ? ? ? ? ? ? >
? ? ? ? ? ? ? ? ? ? ? <el-table-column
? ? ? ? ? ? ? ? ? ? ? ?v-if="!columnHideList[0]"
? ? ? ? ? ? ? ? ? ? ? ? type="selection"
? ? ? ? ? ? ? ? ? ? ? ? width="55">
? ? ? ? ? ? ? ? ? ? ? </el-table-column>
? ? ? ? ? ? ? ? ? ? ? <el-table-column
? ? ? ? ? ? ? ? ? ? ? ? v-if="!columnHideList[1]"
? ? ? ? ? ? ? ? ? ? ? ? type="index"
? ? ? ? ? ? ? ? ? ? ? ? label="序號(hào)"
? ? ? ? ? ? ? ? ? ? ? ? width="50">
? ? ? ? ? ? ? ? ? ? ? ? <template slot-scope="scope">
? ? ? ? ? ? ? ? ? ? ? ? ? {{ (onrenewalForm.pageNumber - 1) * onrenewalForm.pageSize + (scope.$index + 1)}}
? ? ? ? ? ? ? ? ? ? ? ? </template>
? ? ? ? ? ? ? ? ? ? ? </el-table-column>
? ? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? ? ? ? <el-table-column prop="codeNo" label="序列號(hào)" v-if="!columnHideList[2]"> </el-table-column>
? ? ? ? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? ? <el-table-column prop="proName" label="產(chǎn)品" v-if="!columnHideList[3]"> </el-table-column>
? ? ? ? ? ? ? ? ? ? ? <el-table-column prop="proPrice" label="產(chǎn)品定價(jià)" width="100px" v-if="!columnHideList[4]">
? ? ? ? ? ? ? ? ? ? ? ? <template slot-scope="{row}">
? ? ? ? ? ? ? ? ? ? ? ? ? <span >{{row.proPrice / 100 | numFormat(2)}}</span>
? ? ? ? ? ? ? ? ? ? ? ? </template>
? ? ? ? ? ? ? ? ? ? ? </el-table-column>
? ? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? ? ? ? <el-table-column prop="activeTime" label="激活時(shí)間" v-if="!columnHideList[5]">
? ? ? ? ? ? ? ? ? ? ? ? <template slot-scope="{row}">
? ? ? ? ? ? ? ? ? ? ? ? ? <span >{{ row.activeTime | parseTime('{y}-{m}-27bxjkg {h}:{i}:{s}') }}</span>
? ? ? ? ? ? ? ? ? ? ? ? </template>
? ? ? ? ? ? ? ? ? ? ? </el-table-column>
? ? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? ? ? ??
? ? ? ? ? ? ? ? ? ? </trends-table>主要就是利用el-table組件此處就做了序號(hào),多選如果需要更多操作可以自定義擴(kuò)展。
相關(guān)截圖


總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- vue elementUi+sortable.js實(shí)現(xiàn)嵌套表格拖拽問(wèn)題
- vue+elementUi中的table實(shí)現(xiàn)跨頁(yè)多選功能(示例詳解)
- Vue+ElementUI踩坑之動(dòng)態(tài)顯示/隱藏表格的列el-table-column問(wèn)題
- Vue+EleMentUI實(shí)現(xiàn)el-table-colum表格select下拉框可編輯功能實(shí)例
- Vue?ElementUI在el-table中使用el-popover問(wèn)題
- Vue使用ElementUI動(dòng)態(tài)修改table單元格背景顏色或文本顏色
- vue?elementui二次封裝el-table帶插槽問(wèn)題
- Vue?ElementUI?table實(shí)現(xiàn)雙擊修改編輯某個(gè)內(nèi)容的方法
相關(guān)文章
解決Vue.js Devtools未檢測(cè)到Vue實(shí)例的問(wèn)題
在開(kāi)發(fā)Vue.js應(yīng)用時(shí),Vue.js Devtools是一個(gè)不可或缺的調(diào)試工具,然而,有時(shí)我們可能會(huì)遇到“Vue.js not detected”的提示,這意味著Vue.js Devtools未能成功識(shí)別和連接到我們的Vue應(yīng)用,本文將詳細(xì)解析這個(gè)問(wèn)題,并提供相應(yīng)的解決步驟與代碼示例,需要的朋友可以參考下2024-01-01
vue數(shù)組中不滿(mǎn)足條件跳出循環(huán)問(wèn)題
這篇文章主要介紹了vue數(shù)組中不滿(mǎn)足條件跳出循環(huán)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
vuex實(shí)現(xiàn)的簡(jiǎn)單購(gòu)物車(chē)功能示例
這篇文章主要介紹了vuex實(shí)現(xiàn)的簡(jiǎn)單購(gòu)物車(chē)功能,結(jié)合實(shí)例形式分析了vuex購(gòu)物車(chē)組件相關(guān)商品列表、購(gòu)物車(chē)創(chuàng)建、添加、刪除、清空等相關(guān)操作技巧,需要的朋友可以參考下2019-02-02
Element中el-input密碼輸入框?yàn)g覽器自動(dòng)填充賬號(hào)密碼問(wèn)題的解決方法
自己寫(xiě)了一個(gè)管理系統(tǒng),登錄成功之后,瀏覽器提示我保存賬號(hào)密碼,每次登錄時(shí)就會(huì)自動(dòng)回填記住的賬號(hào)密碼,方便用戶(hù)快速登錄,下面這篇文章主要給大家介紹了關(guān)于Element中el-input密碼輸入框?yàn)g覽器自動(dòng)填充賬號(hào)密碼問(wèn)題的解決方法,需要的朋友可以參考下2022-09-09
vue中使用vue-router切換頁(yè)面時(shí)滾動(dòng)條自動(dòng)滾動(dòng)到頂部的方法
這篇文章主要介紹了vue項(xiàng)目中在使用vue-router切換頁(yè)面的時(shí)候滾動(dòng)條自動(dòng)滾動(dòng)到頂部的實(shí)現(xiàn)方法,一般使用Window scrollTo() 方法實(shí)現(xiàn),本文給大家簡(jiǎn)單介紹了crollTop的使用,需要的朋友可以參考下2017-11-11
手寫(xiě)?Vue3?響應(yīng)式系統(tǒng)(核心就一個(gè)數(shù)據(jù)結(jié)構(gòu))
這篇文章主要介紹了手寫(xiě)?Vue3?響應(yīng)式系統(tǒng)(核心就一個(gè)數(shù)據(jù)結(jié)構(gòu)),響應(yīng)式就是被觀察的數(shù)據(jù)變化的時(shí)候做一系列聯(lián)動(dòng)處理。就像一個(gè)社會(huì)熱點(diǎn)事件,當(dāng)它有消息更新的時(shí)候,各方媒體都會(huì)跟進(jìn)做相關(guān)報(bào)道。這里社會(huì)熱點(diǎn)事件就是被觀察的目標(biāo)2022-06-06
vue2.0 + element UI 中 el-table 數(shù)據(jù)導(dǎo)出Excel的方法
下面小編就為大家分享一篇vue2.0 + element UI 中 el-table 數(shù)據(jù)導(dǎo)出Excel的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
關(guān)于vue.js彈窗組件的知識(shí)點(diǎn)總結(jié)
最近在開(kāi)發(fā)過(guò)程對(duì)對(duì)于組件化的開(kāi)發(fā)有一些感想,于是開(kāi)始記錄下這些。彈窗組件一直是 web 開(kāi)發(fā)中必備的,使用頻率相當(dāng)高,最常見(jiàn)的莫過(guò)于 alert,confirm和prompt這些,不同的組件庫(kù)對(duì)于彈窗的處理也是不一樣的,下面來(lái)一起看看吧。2016-09-09

