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
|
|
|
|
import { List, Avatar, Button, Spin } from 'antd';
|
|
|
|
|
|
|
|
import reqwest from 'reqwest';
|
|
|
|
|
|
|
|
const fakeDataUrl = 'https://randomuser.me/api/?results=5&inc=name,gender,email,nat&noinfo';
|
|
|
|
|
|
|
|
class LoadMoreList extends React.Component {
|
|
|
|
state = {
|
|
|
|
loading: true,
|
|
|
|
loadingMore: false,
|
|
|
|
showLoadingMore: true,
|
|
|
|
data: [],
|
|
|
|
}
|
|
|
|
componentDidMount() {
|
|
|
|
this.getData((res) => {
|
|
|
|
this.setState({
|
|
|
|
loading: false,
|
|
|
|
data: res.results,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
getData = (callback) => {
|
|
|
|
reqwest({
|
|
|
|
url: fakeDataUrl,
|
|
|
|
type: 'json',
|
|
|
|
method: 'get',
|
|
|
|
contentType: 'application/json',
|
|
|
|
success: (res) => {
|
|
|
|
callback(res);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
onLoadMore = () => {
|
|
|
|
this.setState({
|
|
|
|
loadingMore: true,
|
|
|
|
});
|
|
|
|
this.getData((res) => {
|
|
|
|
const data = this.state.data.concat(res.results);
|
|
|
|
this.setState({
|
|
|
|
data,
|
|
|
|
loadingMore: 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
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
render() {
|
|
|
|
const { loading, loadingMore, showLoadingMore, data } = this.state;
|
|
|
|
const loadMore = showLoadingMore ? (
|
2017-10-13 11:40:49 +08:00
|
|
|
<div style={{ textAlign: 'center', marginTop: 8, height: 32 }}>
|
2017-10-11 10:29:45 +08:00
|
|
|
{loadingMore && <Spin />}
|
|
|
|
{!loadingMore && <Button onClick={this.onLoadMore}>loading more</Button>}
|
|
|
|
</div>
|
|
|
|
) : null;
|
|
|
|
return (
|
|
|
|
<List
|
|
|
|
className="demo-loadmore-list"
|
|
|
|
loading={loading}
|
|
|
|
itemLayout="horizontal"
|
|
|
|
loadMore={loadMore}
|
|
|
|
dataSource={data}
|
|
|
|
renderItem={item => (
|
|
|
|
<List.Item actions={[<a>edit</a>, <a>more</a>]}>
|
|
|
|
<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>
|
|
|
|
</List.Item>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ReactDOM.render(<LoadMoreList />, mountNode);
|
|
|
|
````
|
|
|
|
|
|
|
|
````css
|
|
|
|
.demo-loadmore-list {
|
|
|
|
min-height: 350px;
|
|
|
|
}
|
|
|
|
````
|