- fix for decimals in tables in appl form;

This commit is contained in:
Vitalii Kiiko
2025-07-04 10:33:22 +02:00
parent 34233465e9
commit f5bebc382e
7 changed files with 84 additions and 11 deletions

View File

@@ -410,12 +410,12 @@
.appPageSection__actions {
display: flex;
gap: 24px;
padding: 24px 0 0;
padding: 0;
flex-wrap: wrap;
}
.appPageSection__actions > *:first-child{
margin-bottom: 24px;
.appPageSection > .appPageSection__actions:first-child {
padding-bottom: 24px;
}
.appPageSection .appPageSection__actions:last-of-type {

View File

@@ -1,4 +1,5 @@
import { InputNumber } from 'primereact/inputnumber';
import parseLocaleNumber from '../../../../../../../helpers/parseLocaleNumber';
const NumericFormulaCell = ({ getValue, row: { index }, column: { id }, table }) => {
const initialValue = getValue();
@@ -19,7 +20,7 @@ const NumericFormulaCell = ({ getValue, row: { index }, column: { id }, table })
return (
<InputNumber
disabled={disabled}
value={initialValue}
value={parseLocaleNumber(initialValue)}
/*onValueChange={(e) => onChange(e.value)}*/
onFocus={onFocus}
onBlur={(e) => onBlur(e.target.value)}

View File

@@ -6,6 +6,7 @@ import { head, isEmpty, isNil, pathOr, sum } from 'ramda';
// components
import DefaultCell from './components/DefaultCell';
import LastRowCell from './components/LastRowCell';
import parseLocaleNumber from '../../../../../helpers/parseLocaleNumber';
const RenderTable = ({ tableValue = {}, columnsCfg, lastRowCfg, setTableValueFn, disabled }) => {
const rows = pathOr([], ['rows'], tableValue)
@@ -28,7 +29,7 @@ const RenderTable = ({ tableValue = {}, columnsCfg, lastRowCfg, setTableValueFn,
if (columnCfgData.meta.enableFormula) {
const getAllRowsValues = newRowsData.rows
.map(row => row[columnId])
.map(v => isEmpty(v) || isNil(v) ? 0 : v);
.map(v => parseLocaleNumber(v));
if (cellValue === 'sum') {
total = sum(getAllRowsValues);

View File

@@ -1,5 +1,6 @@
import { head, isEmpty, isNil, sum } from 'ramda';
import { head, isNil, sum } from 'ramda';
import getNumberFormatted from '../../../../../../../helpers/getNumberFormatted';
import parseLocaleNumber from '../../../../../../../helpers/parseLocaleNumber';
const LastRowCell = ({columnId, lastRowCfg, columnMeta = {}, getColumnDataFn}) => {
const cellData = head(lastRowCfg.filter(o => !isNil(o[columnId])));
@@ -7,7 +8,7 @@ const LastRowCell = ({columnId, lastRowCfg, columnMeta = {}, getColumnDataFn}) =
if (columnMeta.enableFormula) {
const getAllRowsValues = getColumnDataFn(columnId)
.map(v => isEmpty(v) || isNil(v) ? 0 : v);
.map(v => parseLocaleNumber(v));
if (cellValue === 'sum') {
cellValue = getNumberFormatted(sum(getAllRowsValues));

View File

@@ -1,4 +1,5 @@
import { InputNumber } from 'primereact/inputnumber';
import parseLocaleNumber from '../../../../../../../helpers/parseLocaleNumber';
const NumericFormulaCell = ({ getValue, row: { index }, column: { id }, table }) => {
const initialValue = getValue();
@@ -20,7 +21,7 @@ const NumericFormulaCell = ({ getValue, row: { index }, column: { id }, table })
return (
<InputNumber
disabled={disabled}
value={initialValue}
value={parseLocaleNumber(initialValue)}
/*onValueChange={(e) => onChange(e.value)}*/
onFocus={onFocus}
onBlur={(e) => onBlur(e.target.value)}

View File

@@ -0,0 +1,69 @@
export default function parseLocaleNumber(numberString) {
// Handle null, undefined, and empty values
if (numberString === null || numberString === undefined || numberString === '') {
return 0;
}
const str = String(numberString).trim();
if (str === '') {
return 0;
}
// Remove any currency symbols, letters, and other non-numeric characters
let cleaned = str.replace(/[^\d.,\-+]/g, '');
// Handle edge cases
if (cleaned === '' || cleaned === '-' || cleaned === '+') {
return 0;
}
// Count dots and commas
const dotCount = (cleaned.match(/\./g) || []).length;
const commaCount = (cleaned.match(/,/g) || []).length;
try {
// Determine format based on separators
if (dotCount === 0 && commaCount === 0) {
// Pure integer
const result = parseInt(cleaned, 10);
return isNaN(result) ? 0 : result;
}
else if (dotCount === 1 && commaCount === 0) {
// US format (123.45)
const result = parseFloat(cleaned);
return isNaN(result) ? 0 : result;
}
else if (dotCount === 0 && commaCount === 1) {
// Could be European (123,45) or US thousands (1,234)
const parts = cleaned.split(',');
if (parts.length === 2 && parts[1].length <= 2) {
// Likely decimal (123,45)
const result = parseFloat(cleaned.replace(',', '.'));
return isNaN(result) ? 0 : result;
} else {
// Likely thousands separator (1,234)
const result = parseFloat(cleaned.replace(',', ''));
return isNaN(result) ? 0 : result;
}
}
else {
// Mixed separators - use position to determine
const lastDot = cleaned.lastIndexOf('.');
const lastComma = cleaned.lastIndexOf(',');
if (lastDot > lastComma) {
// Dot is decimal separator (1,234.56)
cleaned = cleaned.replace(/,/g, '');
} else {
// Comma is decimal separator (1.234,56)
cleaned = cleaned.replace(/\./g, '').replace(',', '.');
}
const result = parseFloat(cleaned);
return isNaN(result) ? 0 : result;
}
} catch (error) {
return 0;
}
}