antd-vue之table合并单元格

<template>
  <a-table :columns="columns" :data-source="data" bordered></a-table>
</template>
<script>
const renderContent = (value) => {
  const obj = {
    children: value,
    attrs: {},
  };
  return obj;
};

const data = [
  {
    key: "1",
    name: "John Brown",
    age: 32,
    tel: "0571-22098900",
    phone: 18889898989,
    address: "New York No. 1 Lake Park",
  },
  {
    key: "2",
    name: "John Brown",
    tel: "0571-220983d33",
    phone: 18889898888,
    age: 42,
    address: "New York No. 1 Lake Park",
  },
  {
    key: "3",
    name: "John Brown",
    age: 32,
    tel: "0575-2209890d9",
    phone: 18900010002,
    address: "Sidney No. 1 Lake Park",
  },
  {
    key: "4",
    name: "JimcRed",
    age: 18,
    tel: "0575-22090dd9",
    phone: 189010002,
    address: "London No. Lake Park",
  },
  {
    key: "5",
    name: "Jim Red",
    age: 11,
    tel: "0575d-2098909",
    phone: 18900010002,
    address: "Dublin No. 2 Lake Park",
  },
  {
    key: "6",
    name: "Jim Red",
    age: 11,
    tel: "0575d-2209909",
    phone: 18900010002,
    address: "Dublin No. 2 Lake Park",
  },
  {
    key: "7",
    name: "Jim Red",
    age: 11,
    tel: "0575d-2208909",
    phone: 18900010002,
    address: "Dublin No. 2 Lake Park",
  },
];

export default {
  data() {
    return {
      data,
      columns: [],
    };
  },
  created() {
    this.columns = [
      {
        title: "名字",
        dataIndex: "name",
        key: "name",
        //customRender这个函数返回要合并的数据
        customRender(_, row) {
          return {
            children: row.name,
            attrs: {
              rowSpan: row.nameRowSpan,
            },
          };
        },
      },
      {
        title: "地址",
        dataIndex: "age",
        key: "age",
        customRender(_, row) {
          return {
            children: row.age,
            attrs: {
              rowSpan: row.ageRowSpan,
            },
          };
        },
      },
      {
        title: "年龄",
        dataIndex: "address",
        key: "address",
        customRender: renderContent,
      },
    ];
    this.rowSpan("name");
    this.rowSpan("age");
  },
  methods: {
    // 合并单元格,这里我抽出了一个函数,调用的时候只需要将dataIndex作为参数传入即可
    rowSpan(key) {
      let arr = this.data
        .reduce((result, item) => {
          if (result.indexOf(item[key]) < 0) {
            result.push(item[key]);
          }
          return result;
        }, [])
        .reduce((result, keys) => {
          const children = this.data.filter((item) => item[key] === keys);
          result = result.concat(
            children.map((item, index) => ({
              ...item,
              [`${key}RowSpan`]: index === 0 ? children.length : 0,
            }))
          );
          return result;
        }, []);
      this.data = arr;
    },
  },
};
</script>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

xiaoshengjinbu

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

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

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

打赏作者

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

抵扣说明:

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

余额充值