当前位置:   article > 正文

Vue控制表格列的显示隐藏_vue隐藏table的列

vue隐藏table的列
table控制列显示隐藏的功能

在这里插入图片描述

当选中时 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>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
// 这是父组件 
//!!!!!!!!!!!!!!!!引入<省略>   
// 调用
<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" 这个
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

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

//下面tableList中对象的数量要和table中加入v-if的数量一致
// 因为用不到label 就直接删掉了
tableList:[
        {
          value: 'ID',
          isTrue:true,
        },
        {
          value: '状态',
          isTrue:true,
        },
        {
          value: '产品名',
          isTrue:true,
        },
      ],
      checkList:[],
      result:[],
      list:[],
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
// components中别忘了注册
  components: {
    screen
  },
  • 1
  • 2
  • 3
  • 4

加油 !

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/831596
推荐阅读
相关标签