ant-design/components/list/demo/loadmore.md

119 lines
2.9 KiB
Markdown
Raw Normal View History

---
order: 2
title:
zh-CN: 加载更多
en-US: Load more
---
## zh-CN
可通过 `loadMore` 属性实现加载更多功能。
## en-US
Load more list with `loadMore` property.
2019-05-07 14:57:32 +08:00
```jsx
import { List, Avatar, Button, Skeleton } from 'antd';
const count = 3;
const fakeDataUrl = `https://randomuser.me/api/?results=${count}&inc=name,gender,email,nat,picture&noinfo`;
class LoadMoreList extends React.Component {
state = {
initLoading: true,
loading: false,
data: [],
list: [],
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
componentDidMount() {
fetch(fakeDataUrl)
.then(res => res.json())
.then(res => {
this.setState({
initLoading: false,
data: res.results,
list: res.results,
});
});
}
2018-06-27 15:55:04 +08:00
onLoadMore = () => {
this.setState({
loading: true,
2021-10-29 15:56:41 +08:00
list: this.state.data.concat(
[...new Array(count)].map(() => ({ loading: true, name: {}, picture: {} })),
),
});
fetch(fakeDataUrl)
.then(res => res.json())
.then(res => {
const data = this.state.data.concat(res.results);
this.setState(
{
data,
list: data,
loading: false,
},
() => {
// Resetting window's offsetTop so as to display react-virtualized demo underfloor.
// 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
window.dispatchEvent(new Event('resize'));
},
);
});
2019-05-07 14:57:32 +08:00
};
2018-06-27 15:55:04 +08:00
render() {
const { initLoading, loading, list } = this.state;
2019-05-07 14:57:32 +08:00
const loadMore =
!initLoading && !loading ? (
<div
style={{
textAlign: 'center',
marginTop: 12,
height: 32,
lineHeight: '32px',
}}
>
<Button onClick={this.onLoadMore}>loading more</Button>
</div>
) : null;
return (
<List
className="demo-loadmore-list"
loading={initLoading}
itemLayout="horizontal"
loadMore={loadMore}
dataSource={list}
renderItem={item => (
<List.Item
actions={[<a key="list-loadmore-edit">edit</a>, <a key="list-loadmore-more">more</a>]}
>
<Skeleton avatar title={false} loading={item.loading} active>
<List.Item.Meta
avatar={<Avatar src={item.picture.large} />}
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>
</List.Item>
)}
/>
);
}
}
ReactDOM.render(<LoadMoreList />, mountNode);
2019-05-07 14:57:32 +08:00
```
2019-05-07 14:57:32 +08:00
```css
.demo-loadmore-list {
min-height: 350px;
}
2019-05-07 14:57:32 +08:00
```