1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
| import React, { useState } from 'react'; import { Table } from 'antd'; import api from 'api/api'; const TableListPromise: React.FC<any> = (props) => { const [rows, setRows] = useState(10); const [total, setTotal] = useState(0); const [dataSource, setDataSource] = useState([]); const [isMore, setIsMore] = useState<boolean>(true); const [pageNo, setPageNo] = useState<number>(1); const getList = (page = 1) => { api.list({ page, rows }).then((res) => { const { data } = res.data; if (data) { const { total, rows } = data; setTotal(total); setDataSource(page == 1 ? rows : [...dataSource, ...rows]); } }); }; const onScrollCapture = () => { let tableEleNodes = document.querySelectorAll(`.tableRect .ant-table-body`)[0]; if ( Math.round(tableEleNodes?.scrollTop) + tableEleNodes?.clientHeight == tableEleNodes?.scrollHeight ) { if (total === dataSource.length) { setIsMore(false); return false; } console.log('-----pageNo+1-----', pageNo + 1); getList(pageNo + 1); setPageNo(pageNo + 1); } }; const columns = [ { title: '序号', dataIndex: 'rownum', key: 'rownum', width: '15%', render: (text, record, index) => index + 1, }, { title: '姓名', dataIndex: 'name', key: 'name', width: '20%', }, { title: '就诊医院', dataIndex: 'orgName', key: 'orgName', width: '35%', }, { title: '居住地址', dataIndex: 'addr', key: 'addr', width: '30%', }, ]; return ( <div className="tablecontainer" onScrollCapture={onScrollCapture}> <Table className={`tableRect`} columns={columns} dataSource={dataSource} pagination={false} scroll={{ y: 240 }} rowKey={() => Math.random()} /> {!isMore ? <div className="no-more">已经到底啦</div> : null} </div> ); }; export default TableListPromise;
|