使用蚂蚁设计(antd)的分页和卡片组件?

can*_*nin 1 javascript pinterest reactjs antd

是否可以将antd的分页组件与Card组件组合在一起,以获得类似于Pinterest的页面?

来自https://ant.design/components/pagination/的基本分页代码:

import { Pagination } from 'antd';

ReactDOM.render(<Pagination defaultCurrent={1} total={50} />, mountNode);
Run Code Online (Sandbox Code Playgroud)

来自https://ant.design/components/card/的基本卡代码:

import { Card } from 'antd';

ReactDOM.render(
  <Card
    title="Card title"
    extra={<a href="#">More</a>}
    style={{ width: 300 }}
  >
    <p>Card content</p>
    <p>Card content</p>
    <p>Card content</p>
  </Card>,
  mountNode
);
Run Code Online (Sandbox Code Playgroud)

与图像中的示例类似,如何将它们组合起来以遍历许多卡?例如,某些页面有9张卡片。

分页示例

小智 6

这是我在项目中使用的工作代码:

<List
  grid={{
   gutter: 16,
   xs: 1,
   sm: 2,
   md: 3,
   lg: 4,
   xl: 5,
   xxl: 6
  }}

  pagination={{
    showSizeChanger: true,
    pageSizeOptions: ["10", "50", "100", "1000"],
    position: "both"
  }}

  dataSource={dataSource}
  renderItem={fabric => (
    <List.Item>
      <Card
        bordered={false}
        key={key}
        title={"CARD TITLE}
        cover={
          <img
            alt={"ALT"}
            src={url}
          />
        }
      >
       {"BODY"}
      </Card>
</List.Item>
Run Code Online (Sandbox Code Playgroud)


Tri*_*ani 5

可以通过设置最小值和最大值并相应显示结果来完成。

const numEachPage = 4   // Use a constant here to keep track of number of cards per page

constructor(props) {
    super(props);
    this.state = {
      minValue: 0,
      maxValue: 1
    };
  }
Run Code Online (Sandbox Code Playgroud)

然后使用Array.slice()显示基于这些值的数据,如下所示:

render() {
    let data = [
      { title: "Card title1", value: "Card content1" },
      { title: "Card title2", value: "Card content2" },
      { title: "Card title3", value: "Card content3" },
      { title: "Card title4", value: "Card content4" },
      { title: "Card title5", value: "Card content5" }
    ];
    return (
      <div>
        {data &&
          data.length > 0 &&
          data.slice(this.state.minValue, this.state.maxValue).map(val => (
            <Card
              title={val.title}
              extra={<a href="#">More</a>}
              style={{ width: 300 }}
            >
              <p>{val.value}</p>
            </Card>
          ))}
        <Pagination
          defaultCurrent={1}
          defaultPageSize={numEachPage} //default size of page
          onChange={this.handleChange}
          total={3} //total number of card data available
        />
      </div>
    );
  }
Run Code Online (Sandbox Code Playgroud)

然后,您可以在handleChange方法中编写逻辑。

handleChange = value => {
    this.setState({
      minValue: (value - 1) * numEachPage,
      maxValue: value * numEachPage
    });
  };
Run Code Online (Sandbox Code Playgroud)

我创建了一个工作演示