2016-03-31 09:40:55 +08:00
---
2019-11-15 14:35:25 +08:00
order: 10
2016-08-15 07:54:01 +08:00
title:
en-US: Ajax
zh-CN: 远程加载数据
2016-03-31 09:40:55 +08:00
---
2015-07-09 20:29:26 +08:00
2016-08-15 07:54:01 +08:00
## zh-CN
2015-12-09 21:49:32 +08:00
这个例子通过简单的 ajax 读取方式,演示了如何从服务端读取并展现数据,具有筛选、排序等功能以及页面 loading 效果。开发者可以自行接入其他数据处理方式。
另外,本例也展示了筛选排序功能如何交给服务端实现,列不需要指定具体的 `onFilter` 和 `sorter` 函数,而是在把筛选和排序的参数发到服务端来处理。
2020-06-28 22:41:59 +08:00
当使用 `rowSelection` 时,请设置 `rowSelection.preserveSelectedRowKeys` 属性以保留 `key` 。
2016-05-27 12:39:15 +08:00
**注意,此示例使用 [模拟接口 ](https://randomuser.me ),展示数据可能不准确,请打开网络面板查看请求。**
2015-07-14 20:04:14 +08:00
2020-11-27 18:29:21 +08:00
> 🛎️ 想要 3 分钟实现?试试 [ProTable](https://procomponents.ant.design/components/table)!
2016-08-15 08:07:03 +08:00
## en-US
2018-07-29 00:58:10 +08:00
This example shows how to fetch and present data from a remote server, and how to implement filtering and sorting in server side by sending related parameters to server.
2016-08-15 08:07:03 +08:00
2020-06-28 22:41:59 +08:00
Setting `rowSelection.preserveSelectedRowKeys` to keep the `key` when enable selection.
2016-08-15 08:07:03 +08:00
**Note, this example use [Mock API ](https://randomuser.me ) that you can look up in Network Console.**
2022-05-19 09:46:26 +08:00
```tsx
2015-12-09 21:49:32 +08:00
import { Table } from 'antd';
2022-07-04 22:09:54 +08:00
import type { ColumnsType, TablePaginationConfig } from 'antd/es/table';
import type { FilterValue, SorterResult } from 'antd/es/table/interface';
2021-12-03 23:54:19 +08:00
import qs from 'qs';
2022-05-23 14:37:16 +08:00
import React, { useEffect, useState } from 'react';
2015-08-05 10:58:21 +08:00
2022-05-19 09:46:26 +08:00
interface DataType {
name: {
first: string;
last: string;
};
gender: string;
email: string;
login: {
uuid: string;
};
}
2022-09-06 22:57:46 +08:00
interface TableParams {
2022-05-19 09:46:26 +08:00
pagination?: TablePaginationConfig;
sortField?: string;
sortOrder?: string;
2022-09-06 22:57:46 +08:00
filters?: Record< string , FilterValue > ;
2022-05-19 09:46:26 +08:00
}
const columns: ColumnsType< DataType > = [
2019-05-07 14:57:32 +08:00
{
title: 'Name',
dataIndex: 'name',
sorter: true,
render: name => `${name.first} ${name.last}` ,
width: '20%',
},
{
title: 'Gender',
dataIndex: 'gender',
2020-01-06 11:13:39 +08:00
filters: [
{ text: 'Male', value: 'male' },
{ text: 'Female', value: 'female' },
],
2019-05-07 14:57:32 +08:00
width: '20%',
},
{
title: 'Email',
dataIndex: 'email',
},
];
2015-07-12 17:10:06 +08:00
2022-09-06 22:57:46 +08:00
const getRandomuserParams = (params: TableParams) => ({
2022-05-19 09:46:26 +08:00
results: params.pagination?.pageSize,
page: params.pagination?.current,
2020-12-09 17:12:32 +08:00
...params,
});
2020-04-24 17:16:44 +08:00
2022-05-19 09:46:26 +08:00
const App: React.FC = () => {
const [data, setData] = useState();
const [loading, setLoading] = useState(false);
2022-09-06 22:57:46 +08:00
const [tableParams, setTableParams] = useState< TableParams > ({
pagination: {
current: 1,
pageSize: 10,
},
2022-05-19 09:46:26 +08:00
});
2018-11-28 15:00:03 +08:00
2022-09-06 22:57:46 +08:00
const fetchData = () => {
2022-05-19 09:46:26 +08:00
setLoading(true);
2022-09-06 22:57:46 +08:00
fetch(`https://randomuser.me/api?${qs.stringify(getRandomuserParams(tableParams))}`)
2021-12-03 23:54:19 +08:00
.then(res => res.json())
2022-05-19 09:46:26 +08:00
.then(({ results }) => {
setData(results);
setLoading(false);
2022-09-06 22:57:46 +08:00
setTableParams({
...tableParams,
pagination: {
...tableParams.pagination,
total: 200,
// 200 is mock data, you should read it from server
// total: data.totalCount,
},
2021-12-03 23:54:19 +08:00
});
2016-05-27 12:39:15 +08:00
});
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
2022-05-19 09:46:26 +08:00
useEffect(() => {
2022-09-06 22:57:46 +08:00
fetchData();
}, [JSON.stringify(tableParams)]);
2022-05-19 09:46:26 +08:00
const handleTableChange = (
2022-09-06 22:57:46 +08:00
pagination: TablePaginationConfig,
2022-05-19 09:46:26 +08:00
filters: Record< string , FilterValue > ,
sorter: SorterResult< DataType > ,
) => {
2022-09-06 22:57:46 +08:00
setTableParams({
pagination,
filters,
...sorter,
2022-05-19 09:46:26 +08:00
});
};
return (
< Table
columns={columns}
rowKey={record => record.login.uuid}
dataSource={data}
2022-09-06 22:57:46 +08:00
pagination={tableParams.pagination}
2022-05-19 09:46:26 +08:00
loading={loading}
onChange={handleTableChange}
/>
);
};
2015-07-14 15:35:17 +08:00
2022-04-15 16:20:56 +08:00
export default App;
2019-05-07 14:57:32 +08:00
```