动态显示与隐藏表格的列

一、整个表格(表头加数据)动态变化:

1、效果:

点击表头,弹出选择框:

2、代码:

<template>
  <div>
    <!--<div v-show="true" class="menu1" style="width:10%;float:left">
      <el-checkbox-group v-model="colOptions">
        <el-checkbox v-for="item in colSelect" :key="item" :label="item" />
      </el-checkbox-group>
    </div>

    <div style="width:70%">
       <table highlight-current-row @header-contextmenu="contextmenu" style="width: 70%;float:right">
        <tr>
          <td
            v-for="(item,index) in tableHeader"
            v-if="colData[index].istrue"
            width="180"
          >{{item.label}}</td>
        </tr>
        <tr v-for="(item,index) in tableData">
            <td>{{item.id}}</td>
        </tr>
    </table>-->

    <el-table @header-contextmenu="contextmenu" :data="tableData" style="width: 100%">
     <el-table-column
       v-for="(item,index) in tableHeader"
       v-if="colData[index].istrue"
       :key="item.key"
       show-overflow-tooltip
       :label="item.label"
       :prop="item.key"
     >
     </el-table-column>
    </el-table>
    <!--右键弹出的菜单内容-->
    <!--动态计算菜单出现的位置-->
    <div
      v-show="menuVisible"
      :style="{top:top+ &quot;px&quot;,left:left+ &quot;px&quot;}"
      class="menu1"
    >
      <el-checkbox-group v-model="colOptions">
        <el-checkbox v-for="item in colSelect" :key="item" :label="item" />
      </el-checkbox-group>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //所有的列
      tableHeader: [
        { label: "ID", key: "id" },
        { label: "编号", key: "num" },
        { label: "类型", key: "type" },
        { label: "价格", key: "price" },
        { label: "名称", key: "name" }
      ],
      menuVisible: false, //右键菜单的显示与隐藏
      top: 0, //右键菜单的位置
      left: 0,
      colOptions: ["id", "num", "type", "price", "name"], //多选框的选择项
      colSelect: ["id", "num", "type", "price", "name"], //多选框已选择的内容,即表格中显示的列
      tableData: [{ id: 1, num: "111", type: "aaa", price: 10, name: "vv" }],
      // istrue属性存放列的状态
      colData: [
        { title: "id", istrue: true },
        { title: "num", istrue: true },
        { title: "type", istrue: true },
        { title: "price", istrue: true },
        { title: "name", istrue: true }
      ]
    };
  },
  watch: {
    colOptions(newVal, oldVal) {
      if (newVal) {
        //如果有值发生变化,即多选框的已选项变化
        var arr = this.colSelect.filter(i => newVal.indexOf(i) < 0); // 未选中
        this.colData.filter(i => {
          if (arr.indexOf(i.title) !== -1) {
            i.istrue = false;
          } else {
            i.istrue = true;
          }
        });
      }
    }
  },
  methods: {
    contextmenu(row, event) {
      //先把菜单关闭,目的是第二次或者第n次右键鼠标的时候 它默认的是true
      this.menuVisible = false;
      // 显示菜单
      this.menuVisible = true;
      window.event.returnValue = false; //阻止浏览器自带的右键菜单弹出
      //给整个document绑定click监听事件, 左键单击任何位置执行foo方法
      document.addEventListener("click", this.foo);
      //event对应的是鼠标事件,找到鼠标点击位置的坐标,给菜单定位
      this.top = event.clientY;
      this.left = event.clientX;
    },
    foo() {
      this.menuVisible = false; //关闭菜单栏
      document.removeEventListener("click", this.foo); //解绑click监听,很重要,具体原因可以看另外一篇博文
    }
  }
};
</script>

<style>
.menu1 {
  position: fixed;
  height: auto;
  width: 231px;
  border-radius: 3px;
  border: 1px solid #999999;
  background-color: #f4f4f4;
  padding: 10px;
  z-index: 1000;
}
.el-checkbox {
  display: block;
  height: 20px;
  line-height: 20px;
  padding: 0 5px;
  margin-right: 0;
  font-size: 12px;
  border: 1px solid transparent;
}
.el-checkbox:hover {
  border-radius: 3px;
  border: 1px solid #999999;
}
</style>

二、

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

w_t_y_y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值