UNIVPLMDataIntegration/Web/src/hooks/useVxeTableOptionsHook.ts

93 lines
3.0 KiB
TypeScript
Raw Normal View History

2024-07-09 09:47:33 +08:00
import { reactive } from 'vue';
2024-10-10 10:24:47 +08:00
import { dayjs } from 'element-plus';
2024-07-09 09:47:33 +08:00
import { useThemeConfig } from '/@/stores/themeConfig';
import { merge } from 'lodash-es';
2024-10-10 10:24:47 +08:00
import { VxeGridProps, VxeGridPropTypes, VxeComponentSizeType } from 'vxe-table';
import { VxeTablePropTypes } from 'vxe-pc-ui/types/components/table';
2024-07-09 09:47:33 +08:00
// 根据主题配置获取组件大小
2024-07-10 12:59:32 +08:00
const vxeSize: VxeComponentSizeType = useThemeConfig().themeConfig.globalComponentSize == 'small' ? 'mini' : useThemeConfig().themeConfig.globalComponentSize == 'default' ? 'small' : 'medium';
2024-07-09 09:47:33 +08:00
/**
* @param {String} id ;
* @param {String} id name:表格名称;
* @param {VxeGridPropTypes.Columns<any>} columns ;
2024-10-10 10:24:47 +08:00
* @param {Boolean} showFooter ;
* @param {any} footerData ;
* @param {VxeTablePropTypes.FooterMethod<any>} footerMethod ;
2024-07-09 09:47:33 +08:00
*/
interface iVxeOption {
id?: string;
name?: string;
columns: VxeGridPropTypes.Columns<any>;
2024-10-10 10:24:47 +08:00
showFooter?: boolean;
footerData?: any;
footerMethod?: VxeTablePropTypes.FooterMethod<any>;
2024-07-09 09:47:33 +08:00
}
/**
* Vxe表格参数化Hook
* @param {iVxeOption} opt
2024-07-30 11:35:58 +08:00
* @param {VxeGridProps<T>} extras
2024-07-09 09:47:33 +08:00
* @returns
*/
export const useVxeTable = <T>(opt: iVxeOption, extras?: VxeGridProps<T>) => {
// 创建tableId,表格id固定才可以记录调整列宽再次刷新仍有效。
opt.id = opt.id ? opt.id : String(new Date().getTime());
const options = reactive<VxeGridProps>({
id: opt.id,
height: 'auto',
autoResize: true,
size: vxeSize,
loading: false,
align: 'center', // 自动监听父元素的变化去重新计算表格(对于父元素可能存在动态变化、显示隐藏的容器中、列宽异常等场景中的可能会用到)
// data: [] as Array<T>,
columns: opt.columns,
2024-10-10 10:24:47 +08:00
showFooter: opt.showFooter,
footerData: opt.footerData,
footerMethod: opt.footerMethod,
2024-07-09 09:47:33 +08:00
toolbarConfig: {
size: vxeSize,
slots: { buttons: 'toolbar_buttons', tools: 'toolbar_tools' },
refresh: {
code: 'query',
},
export: true,
print: true,
zoom: true,
custom: true,
},
checkboxConfig: { range: true },
sortConfig: { trigger: 'cell', remote: true },
exportConfig: {
remote: false, // 设置使用服务端导出
filename: `${opt.name}导出_${dayjs().format('YYMMDDHHmmss')}`,
},
pagerConfig: {
enabled: true,
size: vxeSize,
pageSize: 20,
},
2024-07-09 09:47:33 +08:00
printConfig: { sheetName: '' },
proxyConfig: {
enabled: true,
autoLoad: false,
sort: true,
props: {
list: 'data.result', // 不分页时
result: 'data.result.items', // 分页时
total: 'data.result.total',
message: 'data.message',
},
},
customConfig: {
storage: {
// 是否启用 localStorage 本地保存,会将列操作状态保留在本地(需要有 id
visible: true, // 启用显示/隐藏列状态
resizable: true, // 启用列宽状态
},
},
});
return extras ? merge(options, extras) : options;
};