How to specify data type of DOM sourced data?
How to specify data type of DOM sourced data?
If I use the DOM as a data source, the DataTable treats all values as string
types.
How can I tell the DataTable to parse a column as number, boolean or something else?
Test case: https://plnkr.co/edit/YO9YGrQgZ6cEtIyZduiP?p=preview
This test case adds three rows via the DOM and a fourth row via the row.add()
API. Pay close attention to the data types that I get from the createdRow
callback.
tldr if the DOM contains <td>false</td>
then how can I initialize a DataTable that internally uses the boolean false
instead of the string "false"
?
Answers
Apparently it is possible to mutate the data before the first draw.
I'd still like to know if there is a better way. Can it be done through configuration? Or with a (custom) plugin?
If you are reading from the DOM, then yes, they are always read as strings, although sorting will correctly detect numbers. There currently is no defined way to cast the type while reading from the DOM. The only way is a workaround as you have done.
Allan
Followup question: would you mind adding a callback to the API for deserializing DOM data?
I should qualify my statement above - its actually possible already if you use
columns.data
as a function - it isn't as trivial as I was thinking above, but it can be done, you just need to do the deserialisation. You need to be a little careful incase it is called multiple times of course, but a type check would handle that.Allan
I don't think I can use a function. I need to also use the
ajax
option for async CRUD operations. If I setcolumns.data
to a function that expects initial data to bestring
then I can't re-use that function for ajax CRUD operations, can I?Initial data sourced from the DOM
AJAX refresh (eg. after using Editor to change the Completed status)
I can't imagine what a
columns.data
function for this scenario would look like.I don't see why not, although I don't fully understand that point and how it relates to CRUD.
I've just put a little example together here: http://live.datatables.net/nipicuqo/1/edit .
Allan
Let's walk through my problem from the very beginning.
My setup begins with a pre-rendered HTML table.
By default, DataTables parses each row as an array of
string
.You showed me how to add parsing behavior by attaching functions to
columns.data
...but the resulting
row
object is still an array (of mixed types).The point where I get stuck is when I need to replace the data in the table with a copy from a web server.
Suppose that I have configured
ajax
to get data from a custom backendThe backend returns this data:
The response contains an array of objects. Those objects are passed to my function which assumes that
row
is an array, but hererow
is an object.Repro: http://live.datatables.net/faxecere/2/edit (push the Reload button and look at the console output)
It gets even worse when I try to implement Editor, which has another AJAX option that needs to be configured.
Right, instead of writing to an index value like I had above (I didn't know you were using objects), you'd write to an object parameter:
Allan
I should say, this is why I don't recommend using
columns.data
as a function very often. It can be confusing as ****.Allan
Thanks, that works. The pieces of the puzzle are starting to fall into place now.
-removed-
Allan, is it possible that parsing fields to numbers doesn't work for ID columns? (DT_RowId or custom RowId)
Am I doing it wrong or is there something baked into DataTable/Editor that forces the value of
rowId
to be a string?Yes - because it is read from the DOM it is always a string. There isn't a way to change that at the moment I'm afraid.
Allan