List列表

    最基础的列表展示,可承载文字、列表、图片、段落,常用于后台数据展示页面。

    简单列表

    列表拥有大、中、小三种尺寸。

    通过设置 为 large small 分别把按钮设为大、小尺寸。若不设置 size,则尺寸为中。

    可通过设置 headerfooter,来自定义列表头部和尾部。

    List 列表 - 图2

    基础列表。

    1. import { List, Avatar } from 'antd';
    2. const data = [
    3. {
    4. title: 'Ant Design Title 1',
    5. },
    6. {
    7. title: 'Ant Design Title 2',
    8. },
    9. {
    10. title: 'Ant Design Title 3',
    11. },
    12. {
    13. title: 'Ant Design Title 4',
    14. },
    15. ];
    16. ReactDOM.render(
    17. <List
    18. itemLayout="horizontal"
    19. dataSource={data}
    20. renderItem={item => (
    21. <List.Item>
    22. <List.Item.Meta
    23. avatar={<Avatar src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png" />}
    24. title={<a href="https://ant.design">{item.title}</a>}
    25. description="Ant Design, a design language for background applications, is refined by Ant UED Team"
    26. />
    27. </List.Item>
    28. )}
    29. />,
    30. mountNode,
    31. );

    加载更多

    可通过 loadMore 属性实现加载更多功能。

    1. import { List, Avatar, Button, Skeleton } from 'antd';
    2. import reqwest from 'reqwest';
    3. const count = 3;
    4. const fakeDataUrl = `https://randomuser.me/api/?results=${count}&inc=name,gender,email,nat&noinfo`;
    5. class LoadMoreList extends React.Component {
    6. state = {
    7. initLoading: true,
    8. loading: false,
    9. data: [],
    10. list: [],
    11. };
    12. componentDidMount() {
    13. this.getData(res => {
    14. this.setState({
    15. initLoading: false,
    16. data: res.results,
    17. list: res.results,
    18. });
    19. });
    20. }
    21. getData = callback => {
    22. reqwest({
    23. url: fakeDataUrl,
    24. type: 'json',
    25. method: 'get',
    26. contentType: 'application/json',
    27. success: res => {
    28. callback(res);
    29. },
    30. });
    31. };
    32. onLoadMore = () => {
    33. this.setState({
    34. loading: true,
    35. list: this.state.data.concat([...new Array(count)].map(() => ({ loading: true, name: {} }))),
    36. });
    37. this.getData(res => {
    38. const data = this.state.data.concat(res.results);
    39. this.setState(
    40. {
    41. data,
    42. list: data,
    43. loading: false,
    44. },
    45. () => {
    46. // Resetting window's offsetTop so as to display react-virtualized demo underfloor.
    47. // In real scene, you can using public method of react-virtualized:
    48. // https://stackoverflow.com/questions/46700726/how-to-use-public-method-updateposition-of-react-virtualized
    49. window.dispatchEvent(new Event('resize'));
    50. },
    51. );
    52. });
    53. };
    54. render() {
    55. const { initLoading, loading, list } = this.state;
    56. const loadMore =
    57. !initLoading && !loading ? (
    58. <div
    59. style={{
    60. textAlign: 'center',
    61. marginTop: 12,
    62. height: 32,
    63. lineHeight: '32px',
    64. }}
    65. >
    66. <Button onClick={this.onLoadMore}>loading more</Button>
    67. </div>
    68. ) : null;
    69. return (
    70. <List
    71. className="demo-loadmore-list"
    72. loading={initLoading}
    73. itemLayout="horizontal"
    74. loadMore={loadMore}
    75. dataSource={list}
    76. renderItem={item => (
    77. <List.Item actions={[<a>edit</a>, <a>more</a>]}>
    78. <List.Item.Meta
    79. avatar={
    80. <Avatar src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png" />
    81. }
    82. title={<a href="https://ant.design">{item.name.last}</a>}
    83. description="Ant Design, a design language for background applications, is refined by Ant UED Team"
    84. />
    85. <div>content</div>
    86. </Skeleton>
    87. </List.Item>
    88. )}
    89. />
    90. );
    91. }
    92. }
    93. ReactDOM.render(<LoadMoreList />, mountNode);

    通过设置 itemLayout 属性为 vertical 可实现竖排列表样式。

    1. import { List, Avatar, Icon } from 'antd';
    2. const listData = [];
    3. for (let i = 0; i < 23; i++) {
    4. listData.push({
    5. href: 'http://ant.design',
    6. title: `ant design part ${i}`,
    7. avatar: 'https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png',
    8. description:
    9. 'Ant Design, a design language for background applications, is refined by Ant UED Team.',
    10. content:
    11. 'We supply a series of design principles, practical patterns and high quality design resources (Sketch and Axure), to help people create their product prototypes beautifully and efficiently.',
    12. });
    13. }
    14. const IconText = ({ type, text }) => (
    15. <span>
    16. <Icon type={type} style={{ marginRight: 8 }} />
    17. {text}
    18. </span>
    19. );
    20. ReactDOM.render(
    21. <List
    22. itemLayout="vertical"
    23. size="large"
    24. pagination={{
    25. onChange: page => {
    26. console.log(page);
    27. },
    28. pageSize: 3,
    29. }}
    30. dataSource={listData}
    31. footer={
    32. <div>
    33. <b>ant design</b> footer part
    34. </div>
    35. }
    36. renderItem={item => (
    37. <List.Item
    38. key={item.title}
    39. actions={[
    40. <IconText type="star-o" text="156" />,
    41. <IconText type="like-o" text="156" />,
    42. <IconText type="message" text="2" />,
    43. ]}
    44. extra={
    45. <img
    46. width={272}
    47. alt="logo"
    48. src="https://gw.alipayobjects.com/zos/rmsportal/mqaQswcyDLcXyDKnZfES.png"
    49. />
    50. }
    51. >
    52. <List.Item.Meta
    53. avatar={<Avatar src={item.avatar} />}
    54. title={<a href={item.href}>{item.title}</a>}
    55. description={item.description}
    56. />
    57. {item.content}
    58. </List.Item>
    59. )}
    60. />,
    61. mountNode,
    62. );

    List 列表 - 图5

    栅格列表

    可以通过设置 Listgrid 属性来实现栅格列表,column 可设置期望显示的列数。

    1. import { List, Card } from 'antd';
    2. const data = [
    3. {
    4. title: 'Title 1',
    5. },
    6. {
    7. title: 'Title 2',
    8. },
    9. {
    10. title: 'Title 3',
    11. },
    12. {
    13. title: 'Title 4',
    14. },
    15. ];
    16. ReactDOM.render(
    17. <List
    18. grid={{ gutter: 16, column: 4 }}
    19. dataSource={data}
    20. renderItem={item => (
    21. <List.Item>
    22. <Card title={item.title}>Card content</Card>
    23. )}
    24. />,
    25. mountNode,
    26. );

    响应式的栅格列表。尺寸与 保持一致。

    List 列表 - 图7

    滚动加载

    结合 实现滚动自动加载列表。

    1. import { List, message, Avatar, Spin } from 'antd';
    2. import reqwest from 'reqwest';
    3. import InfiniteScroll from 'react-infinite-scroller';
    4. const fakeDataUrl = 'https://randomuser.me/api/?results=5&inc=name,gender,email,nat&noinfo';
    5. class InfiniteListExample extends React.Component {
    6. data: [],
    7. loading: false,
    8. hasMore: true,
    9. };
    10. componentDidMount() {
    11. this.fetchData(res => {
    12. this.setState({
    13. data: res.results,
    14. });
    15. });
    16. }
    17. fetchData = callback => {
    18. reqwest({
    19. url: fakeDataUrl,
    20. type: 'json',
    21. method: 'get',
    22. contentType: 'application/json',
    23. success: res => {
    24. callback(res);
    25. },
    26. });
    27. };
    28. handleInfiniteOnLoad = () => {
    29. let data = this.state.data;
    30. this.setState({
    31. loading: true,
    32. });
    33. if (data.length > 14) {
    34. message.warning('Infinite List loaded all');
    35. this.setState({
    36. hasMore: false,
    37. loading: false,
    38. });
    39. return;
    40. }
    41. this.fetchData(res => {
    42. data = data.concat(res.results);
    43. this.setState({
    44. data,
    45. loading: false,
    46. });
    47. });
    48. };
    49. render() {
    50. return (
    51. <div className="demo-infinite-container">
    52. <InfiniteScroll
    53. initialLoad={false}
    54. pageStart={0}
    55. loadMore={this.handleInfiniteOnLoad}
    56. hasMore={!this.state.loading && this.state.hasMore}
    57. useWindow={false}
    58. >
    59. <List
    60. dataSource={this.state.data}
    61. renderItem={item => (
    62. <List.Item key={item.id}>
    63. <List.Item.Meta
    64. avatar={
    65. <Avatar src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png" />
    66. }
    67. title={<a href="https://ant.design">{item.name.last}</a>}
    68. description={item.email}
    69. />
    70. <div>Content</div>
    71. </List.Item>
    72. )}
    73. >
    74. {this.state.loading && this.state.hasMore && (
    75. <div className="demo-loading-container">
    76. <Spin />
    77. </div>
    78. )}
    79. </List>
    80. </InfiniteScroll>
    81. </div>
    82. );
    83. }
    84. }
    85. ReactDOM.render(<InfiniteListExample />, mountNode);
    1. .demo-infinite-container {
    2. border: 1px solid #e8e8e8;
    3. border-radius: 4px;
    4. overflow: auto;
    5. padding: 8px 24px;
    6. height: 300px;
    7. }
    8. .demo-loading-container {
    9. position: absolute;
    10. bottom: 40px;
    11. width: 100%;
    12. text-align: center;
    13. }

    滚动加载无限长列表

    结合 实现滚动加载无限长列表,带有虚拟化(virtualization)功能,能够提高数据量大时候长列表的性能。

    virtualized 是在大数据列表中应用的一种技术,主要是为了减少不可见区域不必要的渲染从而提高性能,特别是数据量在成千上万条效果尤为明显。

    1. .demo-loading {
    2. position: absolute;
    3. bottom: -40px;
    4. left: 50%;
    5. }

    分页的配置项。