const [current , setCurrent] = useState(1);
const [cacheSelectedId, setCacheSelectedId ] = useState([])
const [cacheSelected, setCacheSelected] = useState([])
cosnt [submitData, setSubmitData] = useState([])
// 将选择的数据组合成后端获取的数据
function getSelectedData(){
if(cacheSelected.length){
cosnt result = cacheSelected.reduce((acu, cur)=>acu.concat(cur.data), [])
setSubmitData(result)
}
}
//表格分页改变函数
function handleChangePagination(pageNum, pageSize){
setCurrent(pageNum);
if(cacheSelected.length && cacheSelected.some(itemA=>itemA.page === pageNum){
const currentPageCacheKeys = cacheSelected.filter(itemB=> itemB.page === pageNum)[0]['data'];
// 此刻的fd_id与rowSelection的rowKey需要一致
const reuslt = currentPageCacheKeys .map(itemC =>itemC.fd_id);
setCacheSelectedId(result)
}else{
setCacheSelectedId([])
}
const rowSelection = {
selectedRowKeys: cacheSelectedId,
onChange: (selectedRowKeys,selectedRows) =>{
setCacheSelectedId(selectedRowKeys)
//将每页选中的数据存起来
if(cacheSelected.length && cacheSelected.some(item=> item.page === current)){
const result = cacheSelected.map(itemP=>{
if(itemP.page === current){
return {
page: current,
data: selectedRows
}
}else{
return itemP
}
})
setCacheSelected(result)
}else{
const reuslt = cacheSeleted;
result.push({page: current, data: selectedRows});
setCacheSelected(result)
}
}
}
<Table
rowSelection={{
type: 'checkbox',
...rowSelection
}}
rowKey={(row)=> row.fd_id}
/>
<Pagination
current = { current }
onChange = { handleChangePagination }
/>