interface TableItem {
[key: string]: number;
}
function getTotalByKeys(tableList: TableItem[], keys: string[]): TableItem {
const total: TableItem = {};
for (const item of tableList) {
for (const key of keys) {
total[key] =
(total[key] || 0) + (typeof item[key] === "number" ? item[key] : 0);
}
}
return total;
}
const useTableTotal = (
value: any[],
totalName: string,
requiredTotalName: string[],
callback?: (arg0: any[], arg1: string[]) => any
): any => {
if (value.length === 0) {
return value;
}
let resultTotalByValue: any;
if (callback) {
return callback(value, requiredTotalName);
} else {
resultTotalByValue = getTotalByKeys(value, requiredTotalName);
value.push({
[totalName]: "合计",
...resultTotalByValue,
});
return value;
}
};
export default useTableTotal;
interface TableItem {
[key: string]: number;
}
function getTotalByKeys(tableList: TableItem[], keys: string[]): TableItem {
const total: TableItem = {};
for (const item of tableList) {
for (const key of keys) {
total[key] =
(total[key] || 0) + (typeof item[key] === "number" ? item[key] : 0);
}
}
return total;
}
const useTableTotal = (
value: any[],
totalName: string,
requiredTotalName: string[],
callback?: (arg0: any[], arg1: string[]) => any
): any => {
if (value.length === 0) {
return value;
}
let resultTotalByValue: any;
if (callback) {
return callback(value, requiredTotalName);
} else {
resultTotalByValue = getTotalByKeys(value, requiredTotalName);
value.push({
[totalName]: "合计",
...resultTotalByValue,
});
return value;
}
};
export default useTableTotal;