feat: add formatFileSize

This commit is contained in:
xingyu4j
2025-10-17 14:38:52 +08:00
parent 4f7b7c0e59
commit 09afe93a7a
6 changed files with 36 additions and 27 deletions

View File

@@ -350,6 +350,17 @@ setupVbenVxeTable({
return `${erpNumberFormatter(fenToYuan(cellValue), digits)}`;
},
});
vxeUI.formats.add('formatFileSize', {
tableCellFormatMethod({ cellValue }, digits = 2) {
if (!cellValue) return '0 B';
const unitArr = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const index = Math.floor(Math.log(cellValue) / Math.log(1024));
const size = cellValue / 1024 ** index;
const formattedSize = size.toFixed(digits);
return `${formattedSize} ${unitArr[index]}`;
},
});
},
useVbenForm,
});

View File

@@ -76,15 +76,7 @@ export function useGridColumns(): VxeTableGridOptions['columns'] {
field: 'size',
title: '文件大小',
minWidth: 80,
formatter: ({ cellValue }) => {
// TODO @xingyu【优先级中】要不要搞到一个方法里
if (!cellValue) return '0 B';
const unitArr = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const index = Math.floor(Math.log(cellValue) / Math.log(1024));
const size = cellValue / 1024 ** index;
const formattedSize = size.toFixed(2);
return `${formattedSize} ${unitArr[index]}`;
},
formatter: 'formatFileSize',
},
{
field: 'type',

View File

@@ -340,6 +340,17 @@ setupVbenVxeTable({
return `${erpNumberFormatter(fenToYuan(cellValue), digits)}`;
},
});
vxeUI.formats.add('formatFileSize', {
tableCellFormatMethod({ cellValue }, digits = 2) {
if (!cellValue) return '0 B';
const unitArr = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const index = Math.floor(Math.log(cellValue) / Math.log(1024));
const size = cellValue / 1024 ** index;
const formattedSize = size.toFixed(digits);
return `${formattedSize} ${unitArr[index]}`;
},
});
},
useVbenForm,
});

View File

@@ -76,15 +76,7 @@ export function useGridColumns(): VxeTableGridOptions['columns'] {
field: 'size',
title: '文件大小',
minWidth: 80,
formatter: ({ cellValue }) => {
// TODO @xingyu【优先级中】要不要搞到一个方法里
if (!cellValue) return '0 B';
const unitArr = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const index = Math.floor(Math.log(cellValue) / Math.log(1024));
const size = cellValue / 1024 ** index;
const formattedSize = size.toFixed(2);
return `${formattedSize} ${unitArr[index]}`;
},
formatter: 'formatFileSize',
},
{
field: 'type',

View File

@@ -202,6 +202,17 @@ setupVbenVxeTable({
return `${erpNumberFormatter(fenToYuan(cellValue), digits)}`;
},
});
vxeUI.formats.add('formatFileSize', {
tableCellFormatMethod({ cellValue }, digits = 2) {
if (!cellValue) return '0 B';
const unitArr = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const index = Math.floor(Math.log(cellValue) / Math.log(1024));
const size = cellValue / 1024 ** index;
const formattedSize = size.toFixed(digits);
return `${formattedSize} ${unitArr[index]}`;
},
});
},
useVbenForm,
});

View File

@@ -76,15 +76,7 @@ export function useGridColumns(): VxeTableGridOptions['columns'] {
field: 'size',
title: '文件大小',
minWidth: 80,
formatter: ({ cellValue }) => {
// TODO @xingyu【优先级中】要不要搞到一个方法里
if (!cellValue) return '0 B';
const unitArr = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const index = Math.floor(Math.log(cellValue) / Math.log(1024));
const size = cellValue / 1024 ** index;
const formattedSize = size.toFixed(2);
return `${formattedSize} ${unitArr[index]}`;
},
formatter: 'formatFileSize',
},
{
field: 'type',