Refactor: Make Reset Column button reset columns order
This commit is contained in:
parent
254a2c423c
commit
83b837966d
2 changed files with 12 additions and 3 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
import "ag-grid-community/styles/ag-grid.css"; // Mandatory CSS required by the grid
|
import "ag-grid-community/styles/ag-grid.css"; // Mandatory CSS required by the grid
|
||||||
import "ag-grid-community/styles/ag-theme-quartz.css"; // Optional Theme applied to the grid
|
import "ag-grid-community/styles/ag-theme-quartz.css"; // Optional Theme applied to the grid
|
||||||
import { AgGridReact, AgGridReactProps } from "ag-grid-react";
|
import { AgGridReact, AgGridReactProps } from "ag-grid-react";
|
||||||
import { ElementRef, forwardRef, useRef } from "react";
|
import { ElementRef, forwardRef, useEffect, useRef } from "react";
|
||||||
import {
|
import {
|
||||||
DEFAULT_TABLE_ALERT_MSG,
|
DEFAULT_TABLE_ALERT_MSG,
|
||||||
DEFAULT_TABLE_ALERT_TITLE,
|
DEFAULT_TABLE_ALERT_TITLE,
|
||||||
|
|
@ -40,6 +40,7 @@ const TableComponent = forwardRef<
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const realRef = ref?.current ? ref : gridRef;
|
const realRef = ref?.current ? ref : gridRef;
|
||||||
const dark = useDarkStore((state) => state.dark);
|
const dark = useDarkStore((state) => state.dark);
|
||||||
|
const initialColumnDefs = useRef(props.columnDefs);
|
||||||
|
|
||||||
const makeLastColumnNonResizable = (columnDefs) => {
|
const makeLastColumnNonResizable = (columnDefs) => {
|
||||||
columnDefs.forEach((colDef, index) => {
|
columnDefs.forEach((colDef, index) => {
|
||||||
|
|
@ -55,6 +56,7 @@ const TableComponent = forwardRef<
|
||||||
...props.columnDefs,
|
...props.columnDefs,
|
||||||
]);
|
]);
|
||||||
params.api.setColumnDefs(updatedColumnDefs);
|
params.api.setColumnDefs(updatedColumnDefs);
|
||||||
|
initialColumnDefs.current = params.api.getColumnDefs();
|
||||||
if (props.onGridReady) props.onGridReady(params);
|
if (props.onGridReady) props.onGridReady(params);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -134,7 +136,7 @@ const TableComponent = forwardRef<
|
||||||
onGridReady={onGridReady}
|
onGridReady={onGridReady}
|
||||||
onColumnMoved={onColumnMoved}
|
onColumnMoved={onColumnMoved}
|
||||||
/>
|
/>
|
||||||
<ResetColumns resetGrid={() => resetGrid(realRef)} />
|
<ResetColumns resetGrid={() => resetGrid(realRef, initialColumnDefs)} />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,12 @@
|
||||||
export default function resetGrid(ref) {
|
export default function resetGrid(ref, initialColumnDefs) {
|
||||||
if (ref?.current && ref?.current.api) {
|
if (ref?.current && ref?.current.api) {
|
||||||
ref.current.api.resetColumnState();
|
ref.current.api.resetColumnState();
|
||||||
|
if (initialColumnDefs.current) {
|
||||||
|
const resetColumns = ref.current.api.applyColumnState({
|
||||||
|
state: initialColumnDefs.current,
|
||||||
|
applyOrder: true,
|
||||||
|
});
|
||||||
|
return resetColumns;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue