How can I add paging on the antd select ? Because getting data from the interface is huge. So I want to implement paging

南笙酒味 提交于 2021-02-11 13:23:31

问题


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 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.


回答1:


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.

Implementation: CodeSandBox Link

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"));

i hope this would help.



来源:https://stackoverflow.com/questions/55613169/how-can-i-add-paging-on-the-antd-select-because-getting-data-from-the-interfac

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!