Decimal with Comma in Table Editor Data
Decimal with Comma in Table Editor Data
Description of problem:
I have the problem that when editing a decimal number with inline editing the number gets displayed with a "." instead of "," as a decimal seperator (I live in germany where comma is used as the decimal seperator). When someone tries to change one number and doesnt correct the . to a , the program ignores the . and the number gets stored without a seperator (So the number gets multiplied by ten).
I have solved it for the initial table with another thread: https://datatables.net/forums/discussion/24626
Like this:
language: {
url: '/lang/de_de.json',
decimal: ',',
thousands: '.'
},
ajax: {
url: "/Gesellschafter/GesellschafterReportingApi"
,
dataSrc: function (json) {
if (json.data) {
for (var i = 0, ien = json.data.length; i < ien; i++) {
json.data[i].UmsatzGesellschafter.Umsatz = formatNumbers(json.data[i].UmsatzGesellschafter.Umsatz);
json.data[i].UmsatzGesellschafter.Menge = formatNumbers(json.data[i].UmsatzGesellschafter.Menge);
}
return json.data;
}
}
},
and the format function
function formatNumbers(data) {
if (data == null) return data;
var rounded = data.toFixed(2);
var str = rounded.toString();
var num = str.replace(".", ",");
return num;
}
So initially all the data gets correct and the point is replaced by a comma.
The Problem occurs when a number gets edited. After editing a row the data for that row gets reloaded and the old problem occurs.
Please somebody got any idea how to solve this, is there an event for data row update I could use?
Answers
I solved the problem with a getFormator on the server side like this:
With the formatting function being like this:
In case you need to expand your solution with various number formats:
My solution is bilingual. It needs to comply with English and German number formatting depending on the selected language.
I use get and set formatters on the server in order to avoid client side reformatting. To make sure the numbers are in the right format I use https://plentz.github.io/jquery-maskmoney/ to mask the user input fields accordingly.
Here are my two little helper functions that assign the right masks depending on the class that I add dynamically: