当前位置:   article > 正文

AntDesign Pro 后端实现分页,前端表格展示数据及分页_antd中tabs使用useeffect请求数据展示页面

antd中tabs使用useeffect请求数据展示页面

1.引入Ant Design Table相关组件:

  1. import React, {useEffect, useState} from 'react';
  2. import {Button, message, Table} from "antd";
  3. import {ColumnsType} from "antd/es/table";

2.定义表格的列

  1. const columns: ColumnsType<API.User> = [
  2. {
  3. title: 'Id',
  4. dataIndex: 'id',
  5. key: 'id',
  6. },
  7. {
  8. title: '账号',
  9. dataIndex: 'userAccount',
  10. key: 'userAccount',
  11. },
  12. {
  13. title: '昵称',
  14. dataIndex: 'userName',
  15. key: 'userName',
  16. },
  17. {
  18. title: '头像',
  19. key: 'userAvatar',
  20. dataIndex: 'userAvatar',
  21. },
  22. {
  23. title: '角色',
  24. key: 'userRole',
  25. dataIndex: 'userRole',
  26. }, {
  27. title: '创建时间',
  28. key: 'createTime',
  29. dataIndex: 'createTime',
  30. },
  31. {
  32. title: '操作',
  33. key: 'action',
  34. render: (txt, record, index) =>{
  35. return (
  36. <div>
  37. <Button type="primary" size="small">编辑</Button>&nbsp;
  38. <Button type="primary" size="small">删除</Button>
  39. </div>
  40. )
  41. }
  42. },
  43. ];

3.从后端获取数据进行渲染,Table及分页相关参数可以参考https://ant-design.antgroup.com/components/table-cn

  1. const Manage: React.FC = () => {
  2. const initSearchParams = {
  3. page: 1,
  4. pageSize: 10,
  5. };
  6. const [searchParams, setSearchParams] = useState<API.UserQueryRequest>({...initSearchParams});
  7. const [userList, setUserList] = useState<API.User[]>();
  8. const [total, setTotal] = useState<number>(0);
  9. const loadData = async (page: any, pageSize: any) => {
  10. try {
  11. searchParams.pageSize = pageSize ?? 10;
  12. searchParams.current = page ?? 1;
  13. const res = await listUserByPageUsingPost(searchParams);
  14. if (res.data) {
  15. setUserList(res.data.records ?? []);
  16. setTotal(res.data.total ?? 0);
  17. } else {
  18. message.error("获取用户列表失败");
  19. }
  20. } catch (e: any) {
  21. message.error("获取用户列表失败," + e.message);
  22. }
  23. };
  24. useEffect(() => {
  25. loadData();
  26. }, [searchParams]);
  27. return (
  28. <Table
  29. rowKey={"id"}
  30. pagination={{
  31. total: total,
  32. pageSizeOptions: [10, 20, 50, 100],
  33. onChange: loadData,
  34. showTotal: total => `共${total}条记录 `,
  35. defaultPageSize: 10,
  36. defaultCurrent: 1,
  37. position: ['bottomRight'],
  38. size: 'small',
  39. showSizeChanger: true,
  40. showQuickJumper: true,
  41. }}
  42. style={{marginTop: 20}}
  43. columns={columns}
  44. dataSource={userList}/>
  45. );
  46. };

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/140457
推荐阅读
  

闽ICP备14008679号