Vue控制表格列的显示隐藏

table控制列显示隐藏的功能

《Vue控制表格列的显示隐藏》

当选中时 table列显示,未选中时隐藏该列
首先造轮子 全局拿来用

// 这是子组件
<template>
  <div style="text-align: right;">
    <el-popover placement="right" title="列筛选" trigger="click" >
      <el-checkbox-group v-model="checkList" @change="filter" style="max-width:1500px;">
        <el-checkbox
          v-for="(item, index) in tableList"
          :key="index"
          :label="item.value"
        ></el-checkbox>
      </el-checkbox-group>
      <el-button type="button" size="small" slot="reference"
        ><i class="el-icon-arrow-down el-icon-menu"></i
      ></el-button>
    </el-popover>
  </div>
</template>

<script>
// import AddOrUpdate from "./advert-add-or-update";

export default { 
  data() { 
    return { 
        tableList:[],
        checkList:[],
        result:[],
        list:[],
    };
  },
  props:{ 
      datas:{ 

    }
  },
  created(){ 
    // console.log(this.datas)
      this.tableList = this.datas
  },
 mounted(){ 
    this.tableList.forEach((item,index)=>{ 
      this.checkList.push(item.value)
    })
  },
  methods:{ 
    filter(val) { 
      this.tableList.forEach((item,index)=>{ 
        this.list.push(item.value)
      })
      this.result = this.list.filter(number => !val.includes(number))
      // console.log('result',result)
      if(this.result.length>=1){ 
        this.result.forEach((item,index)=>{ 
          this.tableList.forEach((items,indexs)=>{ 
            if(items.value == item){ 
              items.isTrue = false
            }
          })
        })
      }
      // console.log('val',val)
      val.forEach((item,index)=>{ 
        // console.log(item)
        this.tableList.forEach((items,indexs)=>{ 
            if(items.value == item){ 
              items.isTrue = true
            }
          })
      })
    },
  },
};
</script>
<style lang="scss" scoped>
</style>

// 这是父组件 
//!!!!!!!!!!!!!!!!引入<省略> 
// 调用
<screen :datas='tableList'></screen>
//例如这是你的table
<el-table :data="dataList" border row-key="categoryId" style="width: 100%;" v-loading="dataListLoading">
      <el-table-column prop="id" header-align="center" align="center" label="ID" width="80" v-if="tableList[0].isTrue"></el-table-column>
      <el-table-column prop="status" header-align="center" align="center" label="状态" width="100" v-if="tableList[1].isTrue"></el-table-column>
      <el-table-column prop="prodName" header-align="center" align="center" label="产品名" width="200" v-if="tableList[2].isTrue"></el-table-column>
</el-table>

//在需要进行切换显示的列后面加上 v-if="tableList[*].isTrue" 这个

然后js部分 在return 中加入这些

//下面tableList中对象的数量要和table中加入v-if的数量一致
// 因为用不到label 就直接删掉了
tableList:[
        { 
          value: 'ID',
          isTrue:true,
        },
        { 
          value: '状态',
          isTrue:true,
        },
        { 
          value: '产品名',
          isTrue:true,
        },
      ],
      checkList:[],
      result:[],
      list:[],
// components中别忘了注册
  components: { 
    screen
  },

加油 !

    原文作者:With Me
    原文地址: https://blog.csdn.net/weixin_44056936/article/details/109472840
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞