Datatable and data-attribute HTML5 instead of JS init way
Datatable and data-attribute HTML5 instead of JS init way
I read a lot the documentation today and I have the feeling that something is missing.
We can initialize the datatable using javascript, but there is no way to help us to initialize using HTML5 data-attribute
.
Don't you think that allow to use things like <tr data-datatable-type="date-euro">€</tr>
would improve a lot the use of datatable?
I mean, the javascript way is the most important, of course, but sometimes it's just much faster to configure the table using html5 than JS. The JS should be able to override the html5 attribute if provided.
In this specific example, it would also avoid to stronly link the column number with the javascript, not like here:
"columnDefs": [
{ "type": "numeric-comma", targets: 3 }
]
I really think that provide such configuration way would improve a lot the use of the datatable and void to use JS when we could do it with HTML5.
What do you think about that? Many API use this system nowoday, that will become a standard way to configure plugins like jQuery.datatable.
Answers
I've been planning on writing a plug-in for DataTables that would provide this functionality for a long time, and just never had the change to do it. This question do come up a few times a year and it is something I very much want to do - its just a question of getting the time to do it as there are so many other things that are currently taking priority!
Regards,
Allan
Okay, do you have any headline/doc about how you thought you would have implemented that? Any idea how much time is required? I'm wondering about doing it myself. (PR)
The approach I'd take myself is simply to have a mapping object of data attributes to DataTables properties and then loop over the object, searching the DOM for the required parameters and building an initialisation object. There are all sorts of things you could do such as being clever with columns (this is the default sort column), etc, so it could be the work of a few hours, or it could be days of work :-)
Allan
I just discovered that it already "exists", not to type column but some implementation is already done.
https://datatables.net/manual/orthogonal-data
https://datatables.net/examples/advanced_init/html5-data-attributes.html
That's for orthogonal data and not initialisation parameters.
Allan
I wrote this plugin do to it -
http://jsfiddle.net/x6ddqqe7/1/
Supports setup on document ready or when table element has .initiaTable() called on it.
Thanks for sharing your solution with us!
You might also be interested in checking out this Github thread which discusses something similar. I'm still considering this for DataTables core (haven't had time to properly look into it yet!).
Allan