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

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 Fluent udf 编写问题
  • ¥15 求合并两个字节流VB6代码
  • ¥15 Pyqt 如何正确的关掉Qthread,并且释放其中的锁?
  • ¥30 网站服务器通过node.js部署了一个项目!前端访问失败
  • ¥15 WPS访问权限不足怎么解决
  • ¥15 java幂等控制问题
  • ¥15 海湾GST-DJ-N500
  • ¥15 氧化掩蔽层与注入条件关系
  • ¥15 Django DRF 如何反序列化得到Python对象类型数据
  • ¥15 多数据源与Hystrix的冲突