[PUI] Remember state of table sorting (#7702)

- Remember table ordering information
This commit is contained in:
Oliver 2024-07-22 13:27:16 +10:00 committed by GitHub
parent ffd55cf164
commit 01fe26e15f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 3 deletions

View File

@ -1,3 +1,4 @@
import { DataTableSortStatus } from 'mantine-datatable';
import { create } from 'zustand'; import { create } from 'zustand';
import { persist } from 'zustand/middleware'; import { persist } from 'zustand/middleware';
@ -28,6 +29,9 @@ interface LocalStateProps {
setTableColumnNames: ( setTableColumnNames: (
tableKey: string tableKey: string
) => (names: Record<string, string>) => void; ) => (names: Record<string, string>) => void;
tableSorting: Record<string, any>;
getTableSorting: (tableKey: string) => DataTableSortStatus;
setTableSorting: (tableKey: string) => (sorting: DataTableSortStatus) => void;
clearTableColumnNames: () => void; clearTableColumnNames: () => void;
detailDrawerStack: number; detailDrawerStack: number;
addDetailDrawer: (value: number | false) => void; addDetailDrawer: (value: number | false) => void;
@ -87,6 +91,19 @@ export const useLocalState = create<LocalStateProps>()(
clearTableColumnNames: () => { clearTableColumnNames: () => {
set({ tableColumnNames: {} }); set({ tableColumnNames: {} });
}, },
tableSorting: {},
getTableSorting: (tableKey) => {
return get().tableSorting[tableKey] || {};
},
setTableSorting: (tableKey) => (sorting) => {
// Update the table sorting for the given table
set({
tableSorting: {
...get().tableSorting,
[tableKey]: sorting
}
});
},
// detail drawers // detail drawers
detailDrawerStack: 0, detailDrawerStack: 0,
addDetailDrawer: (value) => { addDetailDrawer: (value) => {

View File

@ -10,8 +10,6 @@ import {
Stack, Stack,
Tooltip Tooltip
} from '@mantine/core'; } from '@mantine/core';
import { modals } from '@mantine/modals';
import { showNotification } from '@mantine/notifications';
import { import {
IconBarcode, IconBarcode,
IconFilter, IconFilter,
@ -157,7 +155,12 @@ export function InvenTreeTable<T = any>({
columns: TableColumn<T>[]; columns: TableColumn<T>[];
props: InvenTreeTableProps<T>; props: InvenTreeTableProps<T>;
}) { }) {
const { getTableColumnNames, setTableColumnNames } = useLocalState(); const {
getTableColumnNames,
setTableColumnNames,
getTableSorting,
setTableSorting
} = useLocalState();
const [fieldNames, setFieldNames] = useState<Record<string, string>>({}); const [fieldNames, setFieldNames] = useState<Record<string, string>>({});
const navigate = useNavigate(); const navigate = useNavigate();
@ -390,6 +393,15 @@ export function InvenTreeTable<T = any>({
direction: 'asc' direction: 'asc'
}); });
useEffect(() => {
const tableKey: string = tableState.tableKey.split('-')[0];
const sorting: DataTableSortStatus = getTableSorting(tableKey);
if (sorting) {
setSortStatus(sorting);
}
}, []);
// Return the ordering parameter // Return the ordering parameter
function getOrderingTerm() { function getOrderingTerm() {
let key = sortStatus.columnAccessor; let key = sortStatus.columnAccessor;
@ -413,6 +425,9 @@ export function InvenTreeTable<T = any>({
const handleSortStatusChange = (status: DataTableSortStatus) => { const handleSortStatusChange = (status: DataTableSortStatus) => {
tableState.setPage(1); tableState.setPage(1);
setSortStatus(status); setSortStatus(status);
const tableKey = tableState.tableKey.split('-')[0];
setTableSorting(tableKey)(status);
}; };
// Function to perform API query to fetch required data // Function to perform API query to fetch required data