2017-10-11 10:29:45 +08:00
|
|
|
---
|
|
|
|
order: 2
|
|
|
|
title:
|
|
|
|
zh-CN: 加载更多
|
2017-10-13 11:40:49 +08:00
|
|
|
en-US: Load more
|
2017-10-11 10:29:45 +08:00
|
|
|
---
|
|
|
|
|
|
|
|
## zh-CN
|
|
|
|
|
|
|
|
可通过 `loadMore` 属性实现加载更多功能。
|
|
|
|
|
|
|
|
## en-US
|
|
|
|
|
2017-10-13 11:40:49 +08:00
|
|
|
Load more list with `loadMore` property.
|
2017-10-11 10:29:45 +08:00
|
|
|
|
|
|
|
````jsx
|
2018-08-22 23:34:36 +08:00
|
|
|
import { List, Avatar, Button, Skeleton } from 'antd';
|
2017-10-11 10:29:45 +08:00
|
|
|
|
|
|
|
import reqwest from 'reqwest';
|
|
|
|
|
2018-08-22 23:34:36 +08:00
|
|
|
const count = 3;
|
|
|
|
const fakeDataUrl = `https://randomuser.me/api/?results=${count}&inc=name,gender,email,nat&noinfo`;
|
2017-10-11 10:29:45 +08:00
|
|
|
|
|
|
|
class LoadMoreList extends React.Component {
|
|
|
|
state = {
|
2018-08-22 23:34:36 +08:00
|
|
|
initLoading: true,
|
|
|
|
loading: false,
|
2017-10-11 10:29:45 +08:00
|
|
|
data: [],
|
2018-08-22 23:34:36 +08:00
|
|
|
list: [],
|
2017-10-11 10:29:45 +08:00
|
|
|
}
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2017-10-11 10:29:45 +08:00
|
|
|
componentDidMount() {
|
|
|
|
this.getData((res) => {
|
|
|
|
this.setState({
|
2018-08-22 23:34:36 +08:00
|
|
|
initLoading: false,
|
2017-10-11 10:29:45 +08:00
|
|
|
data: res.results,
|
2018-08-22 23:34:36 +08:00
|
|
|
list: res.results,
|
2017-10-11 10:29:45 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2017-10-11 10:29:45 +08:00
|
|
|
getData = (callback) => {
|
|
|
|
reqwest({
|
|
|
|
url: fakeDataUrl,
|
|
|
|
type: 'json',
|
|
|
|
method: 'get',
|
|
|
|
contentType: 'application/json',
|
|
|
|
success: (res) => {
|
|
|
|
callback(res);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2017-10-11 10:29:45 +08:00
|
|
|
onLoadMore = () => {
|
|
|
|
this.setState({
|
2018-08-22 23:34:36 +08:00
|
|
|
loading: true,
|
|
|
|
list: this.state.data.concat([...new Array(count)].map(() => ({ loading: true, name: {} }))),
|
2017-10-11 10:29:45 +08:00
|
|
|
});
|
|
|
|
this.getData((res) => {
|
|
|
|
const data = this.state.data.concat(res.results);
|
|
|
|
this.setState({
|
|
|
|
data,
|
2018-08-22 23:34:36 +08:00
|
|
|
list: data,
|
|
|
|
loading: false,
|
2017-10-12 14:09:38 +08:00
|
|
|
}, () => {
|
|
|
|
// Resetting window's offsetTop so as to display react-virtualized demo underfloor.
|
2017-10-13 11:40:49 +08:00
|
|
|
// In real scene, you can using public method of react-virtualized:
|
|
|
|
// https://stackoverflow.com/questions/46700726/how-to-use-public-method-updateposition-of-react-virtualized
|
2017-10-12 14:09:38 +08:00
|
|
|
window.dispatchEvent(new Event('resize'));
|
2017-10-11 10:29:45 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2018-06-27 15:55:04 +08:00
|
|
|
|
2017-10-11 10:29:45 +08:00
|
|
|
render() {
|
2018-08-22 23:34:36 +08:00
|
|
|
const { initLoading, loading, list } = this.state;
|
|
|
|
const loadMore = !initLoading && !loading ? (
|
2017-10-18 17:16:14 +08:00
|
|
|
<div style={{ textAlign: 'center', marginTop: 12, height: 32, lineHeight: '32px' }}>
|
2018-08-22 23:34:36 +08:00
|
|
|
<Button onClick={this.onLoadMore}>loading more</Button>
|
2017-10-11 10:29:45 +08:00
|
|
|
</div>
|
|
|
|
) : null;
|
2018-08-22 23:34:36 +08:00
|
|
|
|
2017-10-11 10:29:45 +08:00
|
|
|
return (
|
|
|
|
<List
|
|
|
|
className="demo-loadmore-list"
|
2018-08-22 23:34:36 +08:00
|
|
|
loading={initLoading}
|
2017-10-11 10:29:45 +08:00
|
|
|
itemLayout="horizontal"
|
|
|
|
loadMore={loadMore}
|
2018-08-22 23:34:36 +08:00
|
|
|
dataSource={list}
|
2017-10-11 10:29:45 +08:00
|
|
|
renderItem={item => (
|
|
|
|
<List.Item actions={[<a>edit</a>, <a>more</a>]}>
|
2018-08-22 23:34:36 +08:00
|
|
|
<Skeleton avatar title={false} loading={item.loading} active>
|
|
|
|
<List.Item.Meta
|
|
|
|
avatar={<Avatar src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png" />}
|
|
|
|
title={<a href="https://ant.design">{item.name.last}</a>}
|
|
|
|
description="Ant Design, a design language for background applications, is refined by Ant UED Team"
|
|
|
|
/>
|
|
|
|
<div>content</div>
|
|
|
|
</Skeleton>
|
2017-10-11 10:29:45 +08:00
|
|
|
</List.Item>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ReactDOM.render(<LoadMoreList />, mountNode);
|
|
|
|
````
|
|
|
|
|
|
|
|
````css
|
|
|
|
.demo-loadmore-list {
|
2017-10-18 17:16:14 +08:00
|
|
|
min-height: 350px;
|
2017-10-11 10:29:45 +08:00
|
|
|
}
|
|
|
|
````
|