秃头的欧皇 2021-07-04 03:09 采纳率: 16.7%
浏览 125
已采纳

求问ant vue表格怎么实现多级表头循环

img

  • 写回答

1条回答 默认 最新

  • 关注

    参考:

    <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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有偿求qftp工具。能连接,下载文件,发送代码,windows环境,最好qt6 要qt creator写的
  • ¥70 刚刚看到一个人的网站居然是通过cname访问的
  • ¥15 Attributeerror:super object has no attribute '__sklearn_tags__'_'
  • ¥15 逆置单链表输出不完整
  • ¥15 宇视vms-B200-A16@R启动不了,如下图所示,在软件工具搜不到,如何解决?(操作系统-linux)
  • ¥500 寻找一名电子工程师完成pcb主板设计(拒绝AI生成式答案)
  • ¥15 关于#mysql#的问题:UNION ALL(相关搜索:sql语句)
  • ¥15 matlab二位可视化能否针对不同数值范围分开分级?
  • ¥15 已经创建了模拟器但是不能用来运行app 怎么办😭自己搞两天了
  • ¥15 关于#极限编程#的问题,请各位专家解答!