You can save the collapsed rows index in localStorage or database according to your needs.
You need to write logic in afterMouseDownOnCell
and check if user clicked on parentRow
. or you can bind the logic to ( +/- ) button of handsontable.
const afterMouseDownOnCell = (event: any, coords: CellCoords) => {
if (hotRef !== null && hotRef.current) {
let nestedRowPlug = hotRef.current.hotInstance!.getPlugin(
"nestedRows"
) as any;
let clickedVal = hotRef.current.hotInstance!.getDataAtCell(coords.row, 0);
let isGroup =
(nestedRowPlug.dataManager.data as []).findIndex(
(d: any, indx) => d.id === clickedVal
) >= 0;
if (!isGroup) {
return;
}
let indexOfClickedRow = (
nestedRowPlug.dataManager.cache.rows as []
).findIndex((d: any, indx) => d.id === clickedVal);
let isCollapsed = (
nestedRowPlug.collapsingUI.collapsedRows as number[]
).includes(indexOfClickedRow);
let localStorageCollapsedRows: number[] = JSON.parse(
localStorage.getItem(
`gridCollapsedRows`
) || "[]"
);
let collapsedRowsToUpdate = localStorageCollapsedRows.filter(
(rIndex) => rIndex !== indexOfClickedRow
);
if (isCollapsed) {
nestedRowPlug.collapsingUI.expandChildren(indexOfClickedRow);
} else {
nestedRowPlug.collapsingUI.collapseChildren(indexOfClickedRow);
collapsedRowsToUpdate = [...collapsedRowsToUpdate, indexOfClickedRow];
}
localStorage.setItem(
`gridCollapsedRows`,
JSON.stringify(collapsedRowsToUpdate)
);
setTimeout(() => {
hotRef.current?.hotInstance?.render();
}, 0);
}
};
After this you can get those values and update grid whenever its loaded or settings are updated. For me I used these settings only when grid is loaded first time.
let localStorageCollapsedRows: number[] = JSON.parse(
localStorage.getItem(
`gridCollapsedRows`
) || "[]"
);
localStorageCollapsedRows.forEach((rIndex) => {
if (
nestedRowPlug &&
nestedRowPlug.isPluginsReady &&
nestedRowPlug.dataManager &&
nestedRowPlug.dataManager.cache &&
nestedRowPlug.dataManager.cache.rows[rIndex] &&
nestedRowPlug.dataManager.cache.rows[rIndex].hasOwnProperty(
"__children"
)
)
nestedRowPlug.collapsingUI.collapseChildren(rIndex);
});
hotRef.current?.hotInstance?.render();