如何在 Antd 的 Select 上添加分页?因为从接口获取数据是巨大的。所以我想实现分页。但文档api不支持。
import { Select} from 'antd';
const Option = Select.Option;
let provinceData = [];
render:
<Select>
{provinceData.map(province => <Option>{province}</Option>)}
</Select>
从接口获取的省份数据可能会很大,所以我希望在antd select上实现分页。当我滑动到“选择”底部时,它会重新加载新数据。
如果您有大量数据,请避免在下拉列表中显示。但您仍然喜欢实现您想要的内容,这是带有分页的下拉菜单的原始形式。
执行:代码沙盒链接 https://codesandbox.io/s/qk64rw92pq
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"));
我希望这会有所帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)