оптимизацмя таблицы
This commit is contained in:
parent
dc621a1223
commit
096a8b817f
@ -134,6 +134,11 @@ const RealtimeTable = ({ formType, formId, sheetName, direction, year }) => {
|
|||||||
return () => document.removeEventListener('mousedown', handleDocumentClick);
|
return () => document.removeEventListener('mousedown', handleDocumentClick);
|
||||||
}, [selectedColumnId]);
|
}, [selectedColumnId]);
|
||||||
|
|
||||||
|
const handleCellUpdateError = useCallback((rowId, columnId, error) => {
|
||||||
|
console.error(`Error updating cell ${rowId}_${columnId}:`, error);
|
||||||
|
toast.error('Ошибка обновления ячейки');
|
||||||
|
}, []);
|
||||||
|
|
||||||
const columns = useMemo(() => {
|
const columns = useMemo(() => {
|
||||||
if (!columnsConfig || !columnsConfig.columns) return [];
|
if (!columnsConfig || !columnsConfig.columns) return [];
|
||||||
try {
|
try {
|
||||||
@ -143,41 +148,58 @@ const RealtimeTable = ({ formType, formId, sheetName, direction, year }) => {
|
|||||||
columnsConfig,
|
columnsConfig,
|
||||||
onCellUpdate: handleUpdateCell,
|
onCellUpdate: handleUpdateCell,
|
||||||
onCellNumberClick: handleClickRowCell,
|
onCellNumberClick: handleClickRowCell,
|
||||||
onCellUpdateError: (rowId, columnId, error) => {
|
onCellUpdateError: handleCellUpdateError,
|
||||||
console.error(`Error updating cell ${rowId}_${columnId}:`, error);
|
|
||||||
toast.error('Ошибка обновления ячейки')
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error creating columns:', error);
|
console.error('Error creating columns:', error);
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
}, [columnsConfig, handleUpdateCell, handleClickRowCell]);
|
}, [columnsConfig, handleUpdateCell, handleClickRowCell, handleCellUpdateError]);
|
||||||
|
|
||||||
|
const selectedColumnIdRef = useRef(selectedColumnId);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
selectedColumnIdRef.current = selectedColumnId;
|
||||||
|
}, [selectedColumnId]);
|
||||||
|
|
||||||
const tableMeta = useMemo(() => ({
|
const tableMeta = useMemo(() => ({
|
||||||
updateCell: handleUpdateCell,
|
updateCell: handleUpdateCell,
|
||||||
selectedColumnId,
|
getSelectedColumnId: () => selectedColumnIdRef.current,
|
||||||
}), [handleUpdateCell, selectedColumnId]);
|
}), [handleUpdateCell]);
|
||||||
|
|
||||||
// Конфигурация таблицы
|
const ROW_VIRTUALIZER_OPTIONS = {
|
||||||
const tableConfig = useMemo(
|
|
||||||
() => ({
|
|
||||||
...BASE_TABLE_CONFIG,
|
|
||||||
columns,
|
|
||||||
data: data,
|
|
||||||
enableRowVirtualization: true,
|
|
||||||
enableColumnVirtualization: true,
|
|
||||||
rowVirtualizerInstanceRef: rowVirtualizerRef,
|
|
||||||
rowVirtualizerOptions: {
|
|
||||||
overscan: 30,
|
overscan: 30,
|
||||||
scrollPaddingStart: 0,
|
scrollPaddingStart: 0,
|
||||||
scrollPaddingEnd: 0,
|
scrollPaddingEnd: 0,
|
||||||
estimateSize: () => TABLE_ROW_HEIGHT,
|
estimateSize: () => TABLE_ROW_HEIGHT,
|
||||||
measureElement: (el) => el?.offsetHeight || TABLE_ROW_HEIGHT,
|
measureElement: (el) => el?.offsetHeight || TABLE_ROW_HEIGHT,
|
||||||
},
|
};
|
||||||
|
|
||||||
|
const createTableConfig = ({
|
||||||
|
columns,
|
||||||
|
data,
|
||||||
|
columnSizing,
|
||||||
|
columnPinning,
|
||||||
|
columnVisibility,
|
||||||
|
globalFilter,
|
||||||
|
showColumnFilters,
|
||||||
|
rowSelection,
|
||||||
|
containerRef,
|
||||||
|
tableMeta,
|
||||||
|
setColumnSizing,
|
||||||
|
setColumnPinning,
|
||||||
|
editingCell,
|
||||||
|
contextStartEditing,
|
||||||
|
}) => ({
|
||||||
|
...BASE_TABLE_CONFIG,
|
||||||
|
columns,
|
||||||
|
data,
|
||||||
|
enableRowVirtualization: true,
|
||||||
|
enableColumnVirtualization: true,
|
||||||
|
rowVirtualizerInstanceRef: rowVirtualizerRef,
|
||||||
|
rowVirtualizerOptions: ROW_VIRTUALIZER_OPTIONS,
|
||||||
onEditingCellChange: (cell) => {
|
onEditingCellChange: (cell) => {
|
||||||
if (cell) {
|
if (cell && !editingCell) {
|
||||||
if (editingCell) return;
|
|
||||||
contextStartEditing?.(cell.row, cell.column);
|
contextStartEditing?.(cell.row, cell.column);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -194,7 +216,6 @@ const RealtimeTable = ({ formType, formId, sheetName, direction, year }) => {
|
|||||||
}
|
}
|
||||||
const allCols = [...table.getLeftVisibleLeafColumns(), ...table.getCenterVisibleLeafColumns()]
|
const allCols = [...table.getLeftVisibleLeafColumns(), ...table.getCenterVisibleLeafColumns()]
|
||||||
return allCols[index]?.getSize() ?? 150;
|
return allCols[index]?.getSize() ?? 150;
|
||||||
|
|
||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
enableRowSelection: true,
|
enableRowSelection: true,
|
||||||
@ -248,23 +269,39 @@ const RealtimeTable = ({ formType, formId, sheetName, direction, year }) => {
|
|||||||
placeholder: 'Поиск...',
|
placeholder: 'Поиск...',
|
||||||
size: 'small',
|
size: 'small',
|
||||||
},
|
},
|
||||||
}),
|
});
|
||||||
[
|
|
||||||
|
// Конфигурация таблицы
|
||||||
|
const tableConfig = useMemo(() => createTableConfig({
|
||||||
columns,
|
columns,
|
||||||
data,
|
data,
|
||||||
setColumnSizing,
|
|
||||||
setColumnPinning,
|
|
||||||
columnSizing,
|
columnSizing,
|
||||||
columnPinning,
|
columnPinning,
|
||||||
columnVisibility,
|
columnVisibility,
|
||||||
globalFilter,
|
globalFilter,
|
||||||
showColumnFilters,
|
showColumnFilters,
|
||||||
|
rowSelection,
|
||||||
containerRef,
|
containerRef,
|
||||||
tableMeta,
|
tableMeta,
|
||||||
|
setColumnSizing,
|
||||||
|
setColumnPinning,
|
||||||
|
editingCell,
|
||||||
|
contextStartEditing,
|
||||||
|
}), [
|
||||||
|
columns,
|
||||||
|
data,
|
||||||
|
columnSizing,
|
||||||
|
columnPinning,
|
||||||
|
columnVisibility,
|
||||||
|
globalFilter,
|
||||||
|
showColumnFilters,
|
||||||
rowSelection,
|
rowSelection,
|
||||||
editingCell
|
containerRef,
|
||||||
],
|
tableMeta,
|
||||||
);
|
setColumnSizing,
|
||||||
|
setColumnPinning,
|
||||||
|
editingCell,
|
||||||
|
]);
|
||||||
|
|
||||||
const table = useMaterialReactTable(tableConfig);
|
const table = useMaterialReactTable(tableConfig);
|
||||||
|
|
||||||
@ -342,6 +379,17 @@ const RealtimeTable = ({ formType, formId, sheetName, direction, year }) => {
|
|||||||
setRowSelection({});
|
setRowSelection({});
|
||||||
}, [rowSelection, table])
|
}, [rowSelection, table])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
return () => {
|
||||||
|
setData([]);
|
||||||
|
setRowSelection({});
|
||||||
|
setEditingCell(null);
|
||||||
|
if (rowVirtualizerRef.current) {
|
||||||
|
rowVirtualizerRef.current = null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<SettingsPanel
|
<SettingsPanel
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user