setFormatter() and getFormatter() based on field-type or name
setFormatter() and getFormatter() based on field-type or name
I have several datetime fields in different Editor instances where I would like to use the fields.getFormatter
and fields.setFormatter
to be applied the same way, like displaying the German date format but saving the normal format.
Same goes for a field with the name "amount", which is used in different Editor instances, where I would like to display it with comma as decimal seperator, but saving it the usual way with decimal point.
I tried using a mask for this, but that screwed up my whole data.
Is it possible to achieve this by targeting either by field type or field name?
Just a suggestion: It would be great to have rendering options in Editor the same way as in DataTables or adding number and date formats to the translation json to be included. I know the decimal and thousands seperators are included in the translation file, so why don't those apply to Editor as well?
Answers
You mean something like
columnDefs
but for Editor fields? Honestly, I'd never thought of that before! That is a nice idea - it isn't something that is possible in Editor at the moment though.The reason the language options for the decimal and thousands separators don't apply in Editor is that it requires interaction with the server-side interaction in that it will need to deformat the numbers to store it in the computer representation. This is certain an area of HTML input that irks me! Something like the mask plug-in is about as good as it gets at the moment as far as I am aware, but as you have seen, it can be really fragile!
Allan
I had exactly the same challenge a while ago. My solution is:
- server side get and set formatters for all dates and number formats depending on the user language (German or English). This implicitly works for the data table and Editor without requiring any client side rendering.
- Client side masking of Editor number fields depending on the user language using a jquery plugin. This makes sure that only the expected format for the respective user language is passed to the server.
@rf1234 what do you use to manage the formatters? do you include the formatting inside the ajax calls for getting and sending the data? The numbers isn't so bad as long as I explain to my client that they need to enter numbers with point instead of comma.
@allan yes, something similar like the columnDefs - set and get formatter for each field individually is just crazy redundant if you want to do the same thing for several fields.
I work a lot with Advanced Custom Fields plugin in WordPress and they have the option for fields to display a custom format although the data is saved in regular format which works best for the database. I don't think it's any different than the get and set formatting: turn the value from the DB into something nice for displaying and when sending it back to the DB just turn it back.
I use Editor client and server side and use the respective get and set formatters for Editor. In my case the server side language is PHP but this is also available for the other supported languages.
Here are the links:
https://editor.datatables.net/manual/php/formatters
And a code example:
https://datatables.net/forums/discussion/comment/174042/#Comment_174042
My client side masking solution is really primitive and was developed right after I had restarted coding a couple of years ago but it should show you the basics of what you can do. I set the classes to the fields dynamically in the Editor initialization.
@rf1234 Thanks for sharing! That's really interesting
Feeling rather stupid regarding the dates in Editor now it's been there all along, of course you still have to include this in every field you need it to, but two small lines are doing it quite well:
Details can be found in
datetime
, Moment.js needs to be included obviously@allan actually that would be the easiest solution: adding displayFormat and wireFormat to other fields as well - like with numbers: you could save the number with several decimal characters in the database, but show it differently in the Editor field - alas, there is no 'number' field But this would definitely be perfect to format a decimal or thousands number the way you would within the datatables and there could also be a prefix or suffix (outside the input) for a number field so you can indicate if it's % or €/$ or pieces or whatever - seriously, why isn't there a 'number' field?
To get a number input you can use:
Allan
This article on numeric inputs is also interesting.
Allan
Thank you for the article link @allan ! I didn't know that, that's interesting. I couldn't find anything on number fields, so thank you also for the hint about number inputs.