vue Antd(table结合Checkbox)实现可选择动态表头

最近做了一个小需求,个人记录一下,实现效果大概如下!

在这里插入图片描述
在这里插入图片描述

不多唠了直接贴码

<template>
  <div class="box">
    <h2>vue Antd(table结合Checkbox)实现可选择表头</h2>
     <h2>-</h2>
      <h2>-</h2>
       <h2>-</h2>
    <div class="inpBox">
      <a-popover placement="topLeft">
        <template slot="content">
          <a-checkbox-group v-model="checkedList" @change="changeList">
            <a-checkbox
              v-for="(item, index) in list"
              :key="index"
              :value="item"
              >{{ item }}</a-checkbox
            >
          </a-checkbox-group>
        </template>
        <a-icon
          type="appstore"
          style="fontsize: 24px; border: 1px solid #ccc; padding: 5px 5px"
        />
      </a-popover>
    </div>
    <a-table :columns="filterColumns" :data-source="data">
      <a slot="name" slot-scope="text">{{ text }}</a>
    </a-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 表头选项
      list: ["表头1", "表头2", "表头3", "表头4", "表头5", "表头6"],
      // 表头已选项(需要和【可配置表头信息】filterColumns数量保持一致,如不一致会出现展示问题。很重要)
      checkedList: ["表头1", "表头2"],
      //全部表头信息
      columns: [
        {
          title: "表头1",
          dataIndex: "name",
          key: "name",
          scopedSlots: { customRender: "name" },
        },
        {
          title: "表头2",
          dataIndex: "age",
          key: "age",
          width: 80,
        },
        {
          title: "表头3",
          dataIndex: "addressA",
          key: "addressA",
          ellipsis: true,
        },
        {
          title: "表头4",
          dataIndex: "addressB",
          key: "addressB",
          ellipsis: true,
        },
        {
          title: "表头5",
          dataIndex: "addressC",
          key: "addressC",
          ellipsis: true,
        },
        {
          title: "表头6",
          dataIndex: "address",
          key: "address 3",
          ellipsis: true,
        },
      ],
      //可配置表头信息(需要和【表头已选项】checkedList数量保持一致,如不一致会出现展示问题。很重要)
      filterColumns: [
        {
          title: "表头1",
          dataIndex: "name",
          key: "name",
          scopedSlots: { customRender: "name" },
        },
        {
          title: "表头2",
          dataIndex: "age",
          key: "age",
          width: 80,
        },
      ],
      //   data
      data: [
        {
          key: "1",
          name: "John Brown",
          age: 32,
          address: "New York No. 1 Lake Park, New York No. 1 Lake Park",
          tags: ["nice", "developer"],
        },
        {
          key: "2",
          name: "Jim Green",
          age: 42,
          address: "London No. 2 Lake Park, London No. 2 Lake Park",
          tags: ["loser"],
        },
        {
          key: "3",
          name: "Joe Black",
          age: 32,
          address: "Sidney No. 1 Lake Park, Sidney No. 1 Lake Park",
          tags: ["cool", "teacher"],
        },
      ],
    };
  },
  methods: {
    // 联动
    changeList() {
      this.filterColumns = this.columns.filter((item, index) => {
        //   主要通过checkedList里的已选项返回对应的表头
        return this.checkedList.includes(item.title);
      });
    },
  },
};
</script>

<style>
.box {
  width: 100%;
}
.inpBox {
  width: 800px;
  display: flex;
  flex-direction: row;
  margin: 0 auto;
  align-items: center;
}
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值