秋葵好好吃 2022-10-20 10:20 采纳率: 33.3%
浏览 325
已结题

vue结合antd表格怎么展示一个嵌套多层的数组呢


dataSource: [{ key1: '第一列数据1', key2: [{ key3: '第二列数据1' }, { key3: '第二列数据2' }] }, { key1: '第一列数据2', key2: [{ key3: '第二列数据1' }, { key3: '第二列数据2' }] }]

上面这个嵌套了好几层的数组, 要怎么实现下图这个效果呢? 不能用嵌套子表格(也就是expandedRowRender), 必须是下图这种一模一样的效果
#vue #antd

img

  • 写回答

2条回答 默认 最新

  • 秋葵好好吃 2022-10-26 09:57
    关注

    最终是这样解决了
    要对数据做扁平化处理...

    <template>
      <div>
        <a-table :columns="columns" :data-source="newDataSource" bordered></a-table>
        <button @click="flat">
          点我
        </button>
      </div>
    </template>
    
    <script>
    export default {
      data() {
        return {
          dataSource: [
            {
              key1: "第一列数据1",
              key2: [{ key3: "第二列数据1" }, { key3: "第二列数据2" }],
            },
            {
              key1: "第一列数据2",
              key2: [{ key3: "第二列数据1" }, { key3: "第二列数据2" }],
            },
          ],
          columns: [
            {
              title: "第一列",
              dataIndex: "key1",
              customRender: (text) => {
                return { 
                    children:text.record.key1,
                    attrs: { rowSpan: text.record.titleSpan } 
                };
              },
            },
            {
              title: "第二列",
              dataIndex: "key3",
            },
          ],
          newDataSource: [],
        };
      },
    
      methods: {
        flat() {
          let arr = [];
          this.dataSource.map((arrItem) => {
            let type = true;
            const length2 = arrItem.key2.length; //key2数组的长度
            arrItem.key2.map((key2Item) => {
              arr = [
                ...arr,
                {
                  key1: arrItem.key1,
                  key3: key2Item.key3,
                  titleSpan: Number(`${type ? length2 : 0}`),
                  // twoSpan:index === 0 ? length2 : 0,
                },
              ];
              type = false;
              return arr;
            });
          });
          this.newDataSource = arr;
          console.log(this.newDataSource);
        },
      },
    };
    </script>
    
    <style lang="less" scoped></style>
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 11月17日
  • 已采纳回答 11月9日
  • 创建了问题 10月20日

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵