react antd checkbox實(shí)現(xiàn)全選、多選功能
背景
目前好像只有table組件有實(shí)現(xiàn)表格數(shù)據(jù)的全選功能,如果說對于list,card,collapse等其他組件來說,需要自己結(jié)合checkbox來手動實(shí)現(xiàn)全選功能。
Checkbox.Group有實(shí)現(xiàn)全選功能,但是對于需要遍歷出來的數(shù)據(jù)(接口動態(tài)返回),這種方式不太可行。
方案
考慮都使用checkbox組件來實(shí)現(xiàn),同時增加狀態(tài)管理。
1、增加一個【全選】checkbox,同時遍歷每一行數(shù)據(jù)的時候,為Panel組件的header屬性添加checkbox選擇框。
<Checkbox indeterminate={indeterminate} checked={checkAll} onChange={handleClickAllCheckbox} > 全選 </Checkbox> const renderPanelItem = () => { return caseList?.map((item: CaseItem) => { const key = item.replay_id; const isChecked = selectedRowKeys?.includes(item.task_record_id); // 判斷是否勾選 return ( <Panel header={ <> <Checkbox style={{ marginRight: 5 }} onChange={(e) => { e.stopPropagation(); handleClickItemCheckbox(isChecked, item.task_record_id); }} checked={isChecked} // 視圖 /> <CloseCircleTwoTone twoToneColor="#f5222d" style={{ marginRight: 5 }} /> <span>{item.task_case_name}</span> </> } key={key} extra={renderPanelExtra(item)} > // ... </Panel> ); }); };
2、增加需要的狀態(tài)
const [selectedRowKeys, setSelectedRowKeys] = useState<number[]>([]); // 勾選的每一行數(shù)據(jù)的id集合 const [indeterminate, setIndeterminate] = useState(false); // 【全選】checkbox const [checkAll, setCheckAll] = useState(false); // 【全選】checkbox
3、當(dāng)點(diǎn)擊【全選】checkbox的時候
- 全部勾選:把當(dāng)前頁的item.id和已有的selectedRowKeys一起存起來,注意id去重
- 取消全部勾選:
setSelectedRowKeys([])
- 同時改變checkAll的狀態(tài)
const handleClickAllCheckbox = () => { if (!checkAll) { setSelectedRowKeys( Array.from(new Set([...selectedRowKeys, ...(caseList?.map((item: CaseItem) => item.task_record_id) || [])])), ); } else { setSelectedRowKeys([]); } setCheckAll(!checkAll); setIndeterminate(false); // 全選/全不選的indeterminate為false,部分選上才為true };
4、當(dāng)點(diǎn)擊【每一行數(shù)據(jù)的checkbox】的時候
- 首選需要判斷該行數(shù)據(jù)是否已經(jīng)被勾選上:
const isChecked = selectedRowKeys?.includes(item.task_record_id);
- 如果已經(jīng)被勾選上了,說明需要取消勾選,則從selectedRowKeys去除掉該id;否則,為selectedRowKeys添加上該id。
- 考慮分頁的時候,selectedRowKeys需要存放 上一頁已勾選的id 和 當(dāng)前頁已勾選的id
- 然后再用 當(dāng)前頁已勾選的id列表 去判斷 checkAll 和 indeterminate【全選checkbox】的狀態(tài)值
// 獲取當(dāng)前頁已勾選的keys const getCurrentCheckedList = (allKeys: number[], allData: CaseItem[]) => { const current: number[] = []; allKeys?.forEach((checkedId) => { allData?.forEach((c: CaseItem) => { if (c.task_record_id === checkedId) { current.push(checkedId); } }); }); return current; }; // 點(diǎn)擊每一項(xiàng)數(shù)據(jù)前面checkbox時 const handleClickItemCheckbox = (isChecked: boolean, taskRecordId: number) => { let keys; if (isChecked) { keys = selectedRowKeys.filter((id) => id !== taskRecordId); } else { keys = [...selectedRowKeys, taskRecordId]; } setSelectedRowKeys(keys); // 更新全選checkbox的狀態(tài)值 const current = getCurrentCheckedList(keys, caseList); setCheckAll(current?.length === limit); // limit表示每一頁展示的數(shù)量,checkAll是否為true也只是針對當(dāng)前頁來說 setIndeterminate(!!current.length && current.length < limit); };
另外,在進(jìn)行翻頁請求數(shù)據(jù)的時候,需要對當(dāng)前頁的checkAll狀態(tài)值進(jìn)行判斷,并初始化
// 當(dāng)翻頁時,初始化【全選】選項(xiàng) const currentPageCheckedList = getCurrentCheckedList(selectedRowKeys, resp_list); // resp_list表示接口返回的數(shù)據(jù) setCheckAll(currentPageCheckedList?.length === limit); setIndeterminate(!!currentPageCheckedList.length && currentPageCheckedList.length < limit);
效果圖:
參考文檔:antd CheckBox實(shí)現(xiàn)全選、多選,antd checkbox全選
到此這篇關(guān)于react antd checkbox實(shí)現(xiàn)全選、多選功能的文章就介紹到這了,更多相關(guān)react antd checkbox全選 多選內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
React實(shí)現(xiàn)Vue的watch監(jiān)聽屬性方式
這篇文章主要介紹了React實(shí)現(xiàn)Vue的watch監(jiān)聽屬性方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01ReactNative實(shí)現(xiàn)的橫向滑動條效果
本文介紹了ReactNative實(shí)現(xiàn)的橫向滑動條效果,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),補(bǔ)充介紹了ReactNative基于寬度變化實(shí)現(xiàn)的動畫效果,感興趣的朋友跟隨小編一起看看吧2024-02-02react配合antd組件實(shí)現(xiàn)的管理系統(tǒng)示例代碼
這篇文章主要介紹了react配合antd組件實(shí)現(xiàn)的管理系統(tǒng)示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04詳解React Angular Vue三大前端技術(shù)
當(dāng)前世界中,技術(shù)發(fā)展非常迅速并且變化迅速,開發(fā)者需要更多的開發(fā)工具來解決不同的問題。本文就對于當(dāng)下主流的前端開發(fā)技術(shù)React、Vue、Angular這三個框架做個相對詳盡的探究,目的是為了解開這些前端技術(shù)的面紗,看看各自的廬山真面目。2021-05-05基于Node的React圖片上傳組件實(shí)現(xiàn)實(shí)例代碼
本篇文章主要介紹了基于Node的React圖片上傳組件實(shí)現(xiàn)實(shí)例代碼,非常具有實(shí)用價值,需要的朋友可以參考下2017-05-05React實(shí)現(xiàn)類似淘寶tab居中切換效果的示例代碼
這篇文章主要介紹了React實(shí)現(xiàn)類似淘寶tab居中切換效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06