From 930c6a3277dbfc459034048c4f2a33e5083dd95f Mon Sep 17 00:00:00 2001 From: "Alinson S. Xavier" Date: Wed, 10 Sep 2025 10:04:24 -0500 Subject: [PATCH] web: Optimize table data updates --- web/src/components/Common/Forms/DataTable.tsx | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/web/src/components/Common/Forms/DataTable.tsx b/web/src/components/Common/Forms/DataTable.tsx index 8dfcec4..d476b9f 100644 --- a/web/src/components/Common/Forms/DataTable.tsx +++ b/web/src/components/Common/Forms/DataTable.tsx @@ -370,6 +370,7 @@ const DataTable = (props: DataTableProps) => { const tableRef = useRef(null); const [isTableBuilt, setTableBuilt] = useState(false); const [activeCell, setActiveCell] = useState(null); + const [currentTableData, setCurrentTableData] = useState([]); useEffect(() => { const onCellEdited = (cell: CellComponent) => { @@ -412,25 +413,36 @@ const DataTable = (props: DataTableProps) => { data: data, columns: columns, height: height, + index: "Name", }); tableRef.current.on("tableBuilt", () => { setTableBuilt(true); }); } + if (isTableBuilt) { const newHeight = height; const newColumns = columns; - const newData = data; + const newTableData = data; const oldRows = tableRef.current.getRows(); const activeRowPosition = activeCell?.getRow().getPosition() as number; const activeField = activeCell?.getField(); - if (activeCell) { - console.log(activeCell.getValue()); - } - // Update data - tableRef.current.replaceData(newData).then(() => {}); + if (newTableData.length === currentTableData.length) { + const updatedRows = newTableData.filter((_, i) => { + return ( + JSON.stringify(newTableData[i]) !== + JSON.stringify(currentTableData[i]) + ); + }); + if (updatedRows.length > 0) { + tableRef.current.updateData(updatedRows).then(() => {}); + } + } else { + tableRef.current.replaceData(newTableData).then(() => {}); + } + setCurrentTableData(newTableData); // Restore active cell selection if (activeCell) {