Skip to content Skip to sidebar Skip to footer

How Can I Add Paging On The Antd Select ? Because Getting Data From The Interface Is Huge. So I Want To Implement Paging

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}

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


Post a Comment for "How Can I Add Paging On The Antd Select ? Because Getting Data From The Interface Is Huge. So I Want To Implement Paging"