【antd + vue】a-table 树形数据展示 - 嵌套子表格

5 篇文章 0 订阅

一、目标样式

父表格嵌套子表格,子表格默认折叠,点击父表格左侧加号可以展开父表格显示对应的子表格,展示每行数据更详细的信息

目标效果

二、问题样式

(一)问题说明

父表格展开后,子表格展示成功,但是子表格的数据以2种方式各渲染了一次,即:同一个子表格出现两次

问题展示

(二)问题原因

childrenColumnName 的默认值是 children ,所以当数据中的子数据是 children ,展开时会默认加载到子表格中,就会出现子表格重复渲染两次的问题

三、解决办法

将树形结构数据中的 children 改成其它名字,如:改成 childrens

四、代码示例:

1、 html部分

<a-table
    :columns="acList.columns"
    :data-source="acList.dataSource"
    class="tables"
    :pagination="false"
    :rowKey="(record) => record.id"
    :loading="acList.loading"
    >
    // 嵌套子表格
    <template #expandedRowRender="{ record }">
      <a-table
        :columns="acList.columns2"
        :data-source="record.childrens"
        :pagination="false"
        :rowKey="(record) => record.id"
      >
      </a-table>
    </template>
</a-table>

嵌套表格相关参数

嵌套表格可用参数及说明

2、 js部分

vue3定义相关数据


//表格所需数据
    const acList = reactive({
    // 父表格和子表格所需数据
      dataSource: [
          {
            "id": 5,
            "batch": "1",
            "state": 1,
            "stateName": "已完成",
            "childrens": [
                {
                    "id": 11,
                    "roomNum": 1,
                    "state": 1,
                    "stateName": "已完成"
                },
            ]
        },
      ],
      // 父级表格列名
      columns: [
        {
          title: "批次号",
          dataIndex: "batch",
          key: "batch",
        },
        {
          title: "收集状态",
          key: "stateName",
          align: "center",
          slots: {
            customRender: "stateName",
          },
        },
      ],
      // 子表格列名
      columns2: [
        {
          title: "考场号",
          dataIndex: "roomNum",
          key: "roomNum",
          width: 760,
        },
        {
          title: "收集状态",
          dataIndex: "stateName",
          key: "stateName",
          align: "center",
          slots: {
            customRender: "stateName",
          },
        },
      ],
      // 加载状态
      loading: false,
    });

五、其他可能出现的问题

子表格与父表格表头列没有对齐,如果样式要求如上图目标样式那样,则需要调整子表格列宽使其与父表格相应对齐,如上面代码列宽width设置

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
vue3 antdvue ,要对 a-table 的行进行合计,可以利用 a-table 的 footer 属性和 scoped slot 的方式来实现。 为了得到每一行需要合计的值,需要先将数据进行处理。 首先,需要在 a-table 设置 footer 属性为 true,这样就会生成表格的 footer 部分。 然后,可以利用 scoped slot 来自定义 footer 行的内容。在 slot-scope ,可以获取到表格的所有数据,包括每一行数据。可以遍历每一行的数据,计算需要合计的值,并将结果显示在 footer 。 下面是实现的代码示例: ``` <template> <a-table :columns="columns" :data-source="data" :footer="true"> <template v-slot:footer> <a-table-tfoot> <a-table-cell v-for="col in columns" :key="col.key"> <template v-if="col.key === 'total'"> {{ getFooterTotal(col.key) }} </template> <template v-else> {{ col.title }} </template> </a-table-cell> </a-table-tfoot> </template> </a-table> </template> <script> export default { data() { return { columns: [ { title: 'Name', dataIndex: 'name', key: 'name' }, { title: 'Age', dataIndex: 'age', key: 'age' }, { title: 'Address', dataIndex: 'address', key: 'address' }, { title: 'Total', key: 'total' }, ], data: [ { name: 'John', age: 28, address: 'New York', total: 100 }, { name: 'Jane', age: 32, address: 'London', total: 200 }, { name: 'Bob', age: 42, address: 'Paris', total: 300 }, ], }; }, methods: { getFooterTotal(key) { let total = 0; this.data.forEach((item) => { total += item[key]; }); return total; }, }, }; </script> ``` 在这个示例,我们定义了一个 a-table ,包含了四列数据:Name、Age、Address 和 Total。其,Total 列需要对其下面的每一行进行合计。 我们通过设置 footer 属性为 true,确保能够生成表格的 footer 部分。接着,我们可以利用 scoped slot 来自定义 footer 的内容。 在 footer 模板,我们首先定义了一个 a-table-tfoot 元素,表示 footer 要显示的行。 然后,我们遍历每一列数据,如果是 Total 列,就调用 getFooterTotal 方法来计算合计值。这个方法遍历每一行数据,将 Total 列的值累加到 total 。 最后,我们将结果返回给 footer 模板,以显示在表格的 footer

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值