当前位置:   article > 正文

antd select组件懒加载,后台搜索_antd select懒加载

antd select懒加载
  1. import React, { useState, useEffect } from 'react';
  2. import { Select } from 'antd';
  3. import axios from 'axios';
  4. export default function () {
  5. const limit = 10;
  6. const [options, setOptions] = useState([]);
  7. const [offset, setOffset] = useState(0);
  8. const [total, setTotal] = useState(0);
  9. const [searchValue, setSerachValue] = useState(null);
  10. function getData() {
  11. axios.get('./data.json').then(function (res) {
  12. let data = [...options];
  13. data = data.concat(res.data.rows);
  14. setOptions(data);
  15. setTotal(res.data.total);
  16. });
  17. }
  18. useEffect(()=>{
  19. getData();
  20. },[searchValue, offset]);
  21. return (
  22. <Select
  23. showSearch
  24. allowClear
  25. options={options}
  26. onDropdownVisibleChange={(open)=>{console.log(open); if (open) getData()}}
  27. onPopupScroll={(e)=>{
  28. const { target } = e;
  29. if (target.scrollTop + target.offsetHeight === target.scrollHeight) {
  30. if (total > offset + limit) setOffset(offset + limit);
  31. }
  32. }}
  33. onSearch={(newVlaue)=>{
  34. setSerachValue(newVlaue);
  35. setOffset(0);
  36. setOptions([]);
  37. }}
  38. />
  39. );
  40. }

data.json

  1. {
  2. "total": 61,
  3. "rows": [
  4. {"key": 1, "value": "选项1"},
  5. {"key": 2, "value": "选项2"},
  6. {"key": 3, "value": "选项3"},
  7. {"key": 4, "value": "选项4"},
  8. {"key": 5, "value": "选项5"},
  9. {"key": 6, "value": "选项6"},
  10. {"key": 7, "value": "选项7"},
  11. {"key": 8, "value": "选项8"},
  12. {"key": 9, "value": "选项9"},
  13. {"key": 10, "value": "选项10"}
  14. ]
  15. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/151652?site
推荐阅读
相关标签
  

闽ICP备14008679号