diff options
| author | Larvan2 <[email protected]> | 2024-06-23 21:26:53 +0800 |
|---|---|---|
| committer | Larvan2 <[email protected]> | 2024-06-23 21:27:20 +0800 |
| commit | d0ca56c54c806779ee55b287b53e6ff57bd4d206 (patch) | |
| tree | 5ca2329e71e69469a1c2fada362b19461ab2dfcb /src/components | |
| parent | b541d1823eccd2257562ebd61ddb076bbb648ca1 (diff) | |
feat: store sort state
Diffstat (limited to 'src/components')
| -rw-r--r-- | src/components/ConnectionTable.tsx | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/src/components/ConnectionTable.tsx b/src/components/ConnectionTable.tsx index d5c50a7..c667003 100644 --- a/src/components/ConnectionTable.tsx +++ b/src/components/ConnectionTable.tsx @@ -23,13 +23,15 @@ const sortById = { id: 'id', desc: true }; function Table({ data, columns, hiddenColumns, apiConfig }) { const [operationId, setOperationId] = useState(''); const [showModalDisconnect, setShowModalDisconnect] = useState(false); + + // 从本地存储加载排序状态 + const savedSortBy = JSON.parse(localStorage.getItem('tableSortBy')) || [sortById]; + const tableState = { - sortBy: [ - // maintain a more stable order - sortById, - ], + sortBy: savedSortBy, hiddenColumns, }; + const table = useTable( { columns, @@ -41,10 +43,12 @@ function Table({ data, columns, hiddenColumns, apiConfig }) { ); const { getTableProps, setHiddenColumns, headerGroups, rows, prepareRow } = table; + const state = table.state; useEffect(() => { setHiddenColumns(hiddenColumns); }, [setHiddenColumns, hiddenColumns]); + const { t, i18n } = useTranslation(); let locale: Locale; @@ -67,10 +71,7 @@ function Table({ data, columns, hiddenColumns, apiConfig }) { setShowModalDisconnect(true); }; - const renderCell = ( - cell: { column: { id: string }; row: { original: { id: string } }; value: number }, - locale: Locale - ) => { + const renderCell = (cell, locale) => { switch (cell.column.id) { case 'ctrl': return ( @@ -92,6 +93,11 @@ function Table({ data, columns, hiddenColumns, apiConfig }) { } }; + // 当排序状态改变时,将新状态保存到本地存储 + useEffect(() => { + localStorage.setItem('tableSortBy', JSON.stringify(state.sortBy)); + }, [state.sortBy]); + return ( <div style={{ marginTop: '5px' }}> <table {...getTableProps()} className={cx(s.table, 'connections-table')}> |
