Editor to dynamically load field/column header/footer names
Editor to dynamically load field/column header/footer names
Datatable/Editor has come a long way towards facilitating internationalisation. You can use the i18n option to change the language of the interface (see https://datatables.net/plug-ins/i18n/) and editor buttons too. However, the problem is also "how to change the language of the fields themselves and the column header and footers". The issue is because HTML is too "static". Datatables/Editor pulls in data dynamically from a database but not the "metadata" i.e. field names.
HTML column headers and footers need to manually be aligned to those defined in the JQuery. If there are not enough columns defined in the HTML there will be an error. I was thinking that there may be a better approach. I would like to suggest that the system be extended in the following regard to automatically align metadata (field and column names). This would have he additional benefit of enabling fully internaionalisation by the handling of field names and column names as DATA themselves (i.e. Metadata).
I suggest the Datatable/Editor be enhanced to:-
1) populate column headers and footers dynamically from metadata (field/column names) stored in a database or other table.
2) HTML to contain only symbolic references like "$col1hr, $col2hdr..etc" instead of actual literals. These are replaced at execution time by datatables/editor using the actual names populated inside the tables.
3) fields names also to be symbolic too like "$fld1, $fld2 ..etc" which are replaced at execution time by datatables/editor
A check to ensure the column count in the HTML equals the columns available in the Jquery. An error message to be issued if there is a mismatch. This would ensure that the correct number of HTML columns have been defined to match exactly the number of colmns defined in the JQuery.
Field names are already available in one language in the JQuery but there should be some method of enabling an array of these by language:-
field..............FR...............ES............DE....
_____________________________
house............maison....... casa ....etc
car.................voiture.........coche ..etc
etc
This needs to be populated by the user of course. Perhaps a database table or datatable.
I realise this is a non trivial piece of work but it would greatly enhance the capabilities of datatables and editor the increasingly international world...
This is shown in the internationalization example given.
https://datatables.net/examples/advanced_init/language_file.html
... where the field names and column headers (Name Position Office Age etc) remain in the original language when the buttons and control text changes.
What do you think?
Replies
Thanks for your input on this. i18n is an important (and difficult) topic in any software. Input is always welcome!
This is planned for the next major version of DataTables. It may or may not make it into Editor (it makes thinks more complicated, but it is something I would like to see). The reason it isn't available yet is that JSON does not allow the full range of options that you can have in Javascript - namely functions. However, this comes up so often that I think those who want it will be willing to (or simply have to) accept that compromise.
That's a template engine's job and not an area I want to take DataTables into. There are several excellent template engines already such as Mustache.
You can already use
data-*
attributes to effectively do that. It uses the attributes rather than the inner html, but the outcome should be the same.Allan
Thanks for the quick response. Glad to hear (point 1) is coming soon to datatables and that you believe it important for Editor too. In m opinion it is although I admit it will complicate things. No gain without pain.
I will check out template engines mentioned but to be blunt I think more "middleware" is never usually the best solution as is obviously also increases complexity.
I took a look at the data attributes. So does that mean I can "inherit" data names from the "name:" specified in the JS file?
I took a look at the data-* attributes but could not see where it would pull in the field name from the column definition? Is there an example?
"Coming soon" might be a bit optimistic. But it is planned.
Not as I understand that phrase. It means your HTML can define the options for the table - i.e. it can tell DataTables it wants to get the data from a particular property in the source objects.
Allan
Ah. Not what I meant. I want to pull in the actual field name from the JS file through to the HTML screen itself directly. I looked at data-* but could not see where it does that?
It would be better as it removes a layer of abstraction...
It is an extra hassle to keep the HTML inline with the JSON.
I see what you mean. There is no option to get the field name from the HTML built into Editor at this time. Perfectly possible to do with a line or two of jQuery though - just get the data properties you've assigned for the labels and built the
fields
array based on that.Allan
Sorry what I want is the exact opposite - to get a field name from Editor to show on the HTML instead of that one.
Isn't that exactly what the
fields.label
option does? You define it in Javascript and it shows up in the HTML.Allan
Ah, great. I will check it out.
Sorry, I am of course using the field.label option all over the place - what I was hoping to do is to be able to pick one of a number of "field labels" depending on a language variable. Is this possible inside Editor or will I need to use a resource-file option?
I was kind of hoping I could select field labels in different languages rather than a fixed hard coded one (which i do now) picking up an English field name.
Can you not simply do something like
label: i18n[ myLanguage ].myFieldName
in that case? Just do exactly as you say - pick the language out of an object as you require.Allan
Allan. Thanks I will give it a try. As you suggested I could use Mustache or another similar templating (/resource file) tool. I was trying to keep my design as simple as possible and so am looking at as many options as possible to see which seems best.
Allan. I have a working example of this which I posted;-
https://datatables.net/forums/discussion/34432/example-of-using-mustache-with-datatables-to-change-column-header-languages#latest
Super - thanks for updating this post!
Allan
See my other post on how to dynamically translate field names into other languages automatically.
https://datatables.net/forums/discussion/34517/translate-field-names-into-other-languages-dynamically?new=1