antd a-table表格添加序号和分页总数——基础积累

antd a-table表格添加序号和分页总数——基础积累

效果

在这里插入图片描述

1.a-table组件的使用
<a-table
  :rowKey="(r, i) => i"
  :columns="columns"
  :scroll="{ y: screenHeight, x: 1400 }"
  :dataSource="messageList"
  bordered
  :pagination="pagination"
  @change="handleTableChange"
  :loading="loading"
>
  <div slot="action" slot-scope="record">
    <a
      href="javascript:;"
      @click="$refs.createModal.openModal(record, true)"
      >详情</a
    >
  </div>
</a-table>

看下组件中的属性:

1. rowkey :表格中每一行的唯一值,是一个动态绑定的数据,可以取每行中的id,也可以直接使用每行的索引值i
2. columns :表格中的列,具体要展示的列
3. scroll :表格的横纵向是否出现滚动条,如果超过指定的宽度或高度,就需要出现滚动条
4. dataSource :表格的数据,是一个数组
5. bordered :表格边框,bordered表示表格要有边框
6. pagination :分页数据
7. change :改变表格数据时触发,比如切换页码与页容量时。
8. loading :表格数据加载时的加载状态loading展示
2.a-table组件添加索引
const columns = [
  {
    title: "序号",
    width: 70,
    customRender: (text, record, index) => `${index + 1}`,
  },
]
3.a-table添加表格总数
pagination: {
  pageIndex: 1,
  pageSize: 10,
  total: 0,
  showTotal: (total) => `共 ${total} 条数据`, // 展示总共有几条数据
},

获取表格数据后,设置total总数。

this.$message.success("数据获取成功");
this.messageList = (res.data && res.data.items) || [];
const pagination = { ...this.pagination };
pagination.total =
  (res.data && res.data.items && res.data.total) || 0;
this.pagination = pagination;

改变表格数据后,也就是handleTableChange函数:

handleTableChange(pagination, filters, sorter) {
// 或者直接让pageIndex等于pagination就可以了,不要下面代码
  const pager = { ...this.pagination };
  pager.pageIndex = pagination.current;
  this.pagination = pager;
  this.getList();//获取表格数据的接口
},

最完整的pagination分页配置

const pagination = {
	showSizeChanger:true,
	showQuickJumper:true,
	total:pagination.totalData,//数据总数
	pageSize:10,
	current:1,
	showTotal:((total)=>{
		return `共${total}条`
	})
}

转自:https://blog.csdn.net/yehaocheng520/article/details/122854893

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 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` 属性计算出当前行的序号,并返回该序号。这样就可以在表格中展示序号了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值