如何在antd select上添加分页?因为从接口获取数据是巨大的.所以我想实现分页 [英] How can I add paging on the antd select ? Because getting data from the interface is huge. So I want to implement paging

查看:90
本文介绍了如何在antd select上添加分页?因为从接口获取数据是巨大的.所以我想实现分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在"Antd选择"上添加分页?因为从接口获取数据是巨大的.所以我想实现分页.但是文档api不支持它.

How can I add paging on the Select of Antd ? Because getting data from the interface is huge. So I want to implement paging. But the document api don't support it.

import { Select} from 'antd';
const Option = Select.Option;

let provinceData = []; 


render:

 <Select>
      {provinceData.map(province => <Option>{province}</Option>)}
 </Select>

provinceData从接口获取的数据可能很大,因此我希望在antd select上实现分页.当我滑动到选择"底部时,它将重新加载新数据.

provinceData that getting from the interface can be huge,so I expect to implement paging on antd select. When I slide to the Select bottom, it reload the new data.

推荐答案

如果您有大量数据,请避免在下拉列表中显示.但是您仍然想要实现所需的功能,这是带有分页的下拉菜单的原始形式.

If you have huge data, avoid showing in dropdown. But still you like to implement what you want, here is the crude form of dropdown with pagination.

实施: CodeSandBox链接

import React from "react";
import ReactDOM from "react-dom";
import "antd/dist/antd.css";
import "./index.css";
import { Select, Icon, Divider, Pagination, Button } from "antd";
import faker from "faker";

const Option = Select.Option;
let names = [];
const count = 100;
const pageSize = 5;
for (let i = 0; i < count; i++) {
  names.push(faker.name.firstName());
}

const getNames = pageNumber => {
  let toSendNames = [];
  for (let i = (pageNumber - 1) * pageSize; i < pageNumber * pageSize; i++) {
    toSendNames.push(names[i]);
  }
  console.log(toSendNames);
  return toSendNames;
};

class Test extends React.Component {
  state = {
    isOpen: false,
    currentPage: 1
  };

  paginationRef = React.createRef();

  render = () => {
    return (
      <div>
        <Select
          style={{ width: 250 }}
          onFocus={() => {
            this.setState({ isOpen: true });
          }}
          open={this.state.isOpen}
          dropdownRender={menu => (
            <div>
              {menu}
              <Divider style={{ margin: "4px 0" }} />
              <div style={{ padding: "8px", textAlign: "center" }}>
                <Pagination
                  simple
                  current={this.state.currentPage}
                  total={count}
                  onChange={pageIndex => {
                    this.setState({
                      currentPage: pageIndex
                    });
                  }}
                />
                <br />
                <Button
                  type="ghost"
                  style={{ width: "100%", borderColor: "red" }}
                  size="small"
                  onClick={() =>
                    this.setState({
                      isOpen: false
                    })
                  }
                >
                  Close
                </Button>
              </div>
            </div>
          )}
        >
          {getNames(this.state.currentPage).map(item => {
            return <Option value={item}>{item}</Option>;
          })}
        </Select>
      </div>
    );
  };
}

ReactDOM.render(<Test />, document.getElementById("container"));

我希望这会有所帮助.

这篇关于如何在antd select上添加分页?因为从接口获取数据是巨大的.所以我想实现分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆