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
import React, { useState, useEffect } from 'react';
2015-12-09 21:49:32 +08:00
import { Table } from 'antd';
2021-12-03 23:54:19 +08:00
import qs from 'qs';
2022-05-19 09:46:26 +08:00
import type { FilterValue, SorterResult } from 'antd/lib/table/interface';
import type { ColumnsType, TablePaginationConfig } from 'antd/lib/table';
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;
};
}
interface Params {
pagination?: TablePaginationConfig;
sorter?: SorterResult< any > | SorterResult< any > [];
total?: number;
sortField?: string;
sortOrder?: string;
}
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-05-19 09:46:26 +08:00
const getRandomuserParams = (params: Params) => ({
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);
const [pagination, setPagination] = useState< TablePaginationConfig > ({
current: 1,
pageSize: 10,
});
2018-11-28 15:00:03 +08:00
2022-05-19 09:46:26 +08:00
const fetchData = (params: Params = {}) => {
setLoading(true);
2021-12-03 23:54:19 +08:00
fetch(`https://randomuser.me/api?${qs.stringify(getRandomuserParams(params))}`)
.then(res => res.json())
2022-05-19 09:46:26 +08:00
.then(({ results }) => {
setData(results);
setLoading(false);
setPagination({
...params.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(() => {
fetchData({ pagination });
}, []);
const handleTableChange = (
newPagination: TablePaginationConfig,
filters: Record< string , FilterValue > ,
sorter: SorterResult< DataType > ,
) => {
fetchData({
sortField: sorter.field as string,
sortOrder: sorter.order as string,
pagination: newPagination,
...filters,
});
};
return (
< Table
columns={columns}
rowKey={record => record.login.uuid}
dataSource={data}
pagination={pagination}
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
```