I have a question about custom formatters.
What I try to achieve is a currencyFormatter just for the amount with Locale sent by the server, when locale is not define or supported fall back to British English. Something like this:
function currencyFmatter(cellvalue, options, rowdata) {
return new Intl.NumberFormat([locale, "en-GB"], {minimumFractionDigits: 2, maximumFractionDigits: 2}).format(cellvalue);
}
My problem is how to pass my variable locale to the formatter, I’m pretty sure it has to be a way to do it but right now I don’t see it.
Thanks
It's an interesting question! There are many ways to implement your requirements.
1) you can extend your input data returned from the server with additional information which specify the locale of data. For example you can returns
"de-DE:10.000,04"
instead of"10.000,04"
which represent1000.04
formatted in German locale (where,
will be used as the decimal separator and.
used as the thousands separator). It allows you to usecellvalue.split(":")
to get array["de-DE", "10.000,04"]
with the locale of the number and the number itselfAlternatively you can place the information about locale of the number in separate field (for example
numLocale
) of the input data and use something likerowdata.numLocale
(orrowdata[12]
depend on the input format of the JSON data) to access the locale.2) It could be that all the data returned from the server will be in the same format. In the case it would be not the best way to prepend all numbers with the same prefix
"de-DE:"
. What you can do for example is to extend the data returned from the server with additional field. For example you can useYou can access the custom
localOfNumbers
field inside ofbeforeProcessing
callback. It's very practical callback. It allows you to pre-process the data returned from the server before the data will be processed by jqGrid. I recommend you to read the answer and this one for more code example. What you can do for example is to savelocalOfNumbers
value in some new option of jqGrid (see the answer for more details). Let us you want to have an optiongridLocale
for the goal. Then you can do something like the following:To access the new
gridLocale
option you can use3) You can consider to save the information about the locale as column property instead of usage one common
gridLocale
grid option. To do this you can define the column incolModel
like belowOne can set the property of
formatoptions.colLocale
inside ofbeforeProcessing
too. You can useand
I'm sure that one can suggest even more ways to implement your requirements.