zy-react-library/components/Table/index.js

74 lines
2.0 KiB
JavaScript

import TablePro from "@cqsjjb/jjb-react-admin-component/Table";
import { getIndexColumn } from "../../utils/index";
import dayjs from 'dayjs';
import "./index.less";
const CLEANUP_INTERVAL_DAYS = 10; // 清理间隔天数
const LAST_CLEANUP_KEY = 'tableLocalStorageLastCleanup'; // 最后清理时间存储key
// 清理本地存储中特定后缀的key
function cleanupTableLocalStorage() {
const now = dayjs();
const lastCleanupStr = localStorage.getItem(LAST_CLEANUP_KEY);
const lastCleanup = lastCleanupStr ? dayjs(lastCleanupStr) : null;
// 如果没有上次清理时间或距离上次清理已超过10天
if (!lastCleanup || now.diff(lastCleanup, 'day') >= CLEANUP_INTERVAL_DAYS) {
// 查找并清理符合条件的key
const keysToRemove = [];
for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i);
if (key && (key.endsWith('#columnState') ||
key.endsWith('#size') ||
key.endsWith('#resizable'))) {
keysToRemove.push(key);
}
}
// 删除匹配的key
keysToRemove.forEach(key => {
localStorage.removeItem(key);
});
// 更新最后清理时间
localStorage.setItem(LAST_CLEANUP_KEY, now.toISOString());
}
}
function Table(props) {
const {
columns = [],
showIndexColumn = true,
ellipsis = true,
align = "center",
indexColumnFixed = "left",
rowKey = "id",
...restProps
} = props;
// 组件初始化时执行清理
cleanupTableLocalStorage();
function settingColumns() {
showIndexColumn && columns.unshift({ ...getIndexColumn(props.pagination), fixed: indexColumnFixed });
const setAlign = column => ({
align,
ellipsis,
...column,
...(column.children ? { children: column.children.map(setAlign) } : {}),
});
return columns.map(setAlign);
}
return (
<div className="table-layout card-layout">
<TablePro rowKey={rowKey} columns={settingColumns()} bordered size="small" {...restProps} />
</div>
);
}
Table.displayName = "Table";
export default Table;