antd a-table表格添加序号

自我学习备注

效果图如下:

 实现方法: 

const columns = [ { title: "序号", width: 70, customRender: (text, record, index) => `${index + 1}`, }, ]

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要在 antd vue3 的 table 组件的 `a-table-column` 中展示序号,可以使用 `rowKey` 和 `customRender` 属性结合起来实现。具体方法如下: 1. 在 `a-table` 组件中设置 `rowKey` 属性为一个函数,返回值为每一行数据的唯一标识,例如 `record.id`。 2. 在第一列的 `a-table-column` 中设置 `customRender` 属性为一个函数,该函数接收两个参数:当前单元格的值和当前行的数据对象。在该函数中,通过 `a-table` 组件的 `dataSource` 和 `rowKey` 属性计算当前行的序号,并返回该序号。 示例代码如下: ``` <template> <a-table :columns="columns" :dataSource="data" :rowKey="record => record.id"> <!-- 第一列展示序号 --> <a-table-column title="#" dataIndex="index" customRender="(_, record) => record.index" /> <!-- 其他列 --> <a-table-column title="姓名" dataIndex="name" /> <a-table-column title="年龄" dataIndex="age" /> </a-table> </template> <script> export default { data() { return { data: [ { id: 1, name: '张三', age: 18 }, { id: 2, name: '李四', age: 20 }, { id: 3, name: '王五', age: 22 }, ], columns: [ { title: '#', dataIndex: 'index', customRender: (_, record) => { const index = this.data.findIndex(item => item.id === record.id) + 1; return index; }, }, { title: '姓名', dataIndex: 'name', }, { title: '年龄', dataIndex: 'age', }, ], }; }, }; </script> ``` 在上面的代码中,我们首先在 `a-table` 组件中设置了 `rowKey` 属性为一个函数,返回值为每一行数据的 `id` 属性。然后,在第一列的 `a-table-column` 中设置了 `customRender` 属性为一个函数,该函数根据当前行的 `id` 属性计算出当前行的序号,并返回该序号。这样就可以在表格中展示序号了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值