Data and type casting
Data and type casting
DataTables 1.10.5. Editor 1.4 Using inline editing.
I'm familiar with what is written here data.
While I have things working for the most part, I seem to be fighting my data and probably doing some unnecessary type casting.
My question is therefore about best practices around how to best work with data types, particularly JavaScript numbers
and Date
objects. For example, I have a column that shows the row number and as rows are inserted or deleted, the row number is dynamically update. Sometimes the row number value, rather than being an number
is a string
.
Do I understand what is happening? My adjax
function, originally looked like this:
"ajax": function (method, url, data, success, error) {
var obj = {};
if (data.action === 'edit') {
success({row: data.data});
}
},
1. But I gather that since data.data
is a jason
object, the values of the object will always be strings. Correct? The server certainly isn't going to know what a JavaScript Data
object is. Or am I misinterpreting what I'm seeing and I have bugs somewhere in my program due to type casting my code might be doing?
To minimize type casting I've started to change my data handling to following this pattern:
"ajax": function (method, url, data, success, error) {
var obj = {};
if (data.action === 'edit') {
obj = { "idx" : parseInt(data.data.idx, 10),
"rowNum" : parseInt(data.data.rowNum, 10),
"myDate" : data.data.myDate,
"myColor" : data.data.myColor,
"nSalesDays" : data.data.nSalesDays,
"myEndDate" : data.data.myEndDate,
success(obj);
}
},
2. Am I on the right track? Or maybe the objects managed by DataTable's Data
array should all be fields that are string
type? Using this technique I would then have to type cast at the point in the code where performing date or integer math.
3. Here's a Render
function. In the if (type === 'display')
, the return, is the result only changing what is being displayed, or is the underlying value being impacted as well? Could use a little clarity as to what I maybe should and should not do in render
.
{"data": 'myEndDate', "width": "10%", "defaultContent": null, "orderable": false, "render": function (data, type, row, meta) {
if (typeof data === "string" && data !== '') {
data = dateToFormat(data);
}
// 'display', 'sort', 'filter', 'type'
if (type === 'display' && data !== null && data !== '') {
return toCustomDateString(data);
}
return data;
}},
Any pointers on what is considered best practices for working with data types would be appreciated.
Thank you.
This question has an accepted answers - jump to answer
Answers
No. JSON can contain numbers, strings, objects, arrays, null and boolean values. However, if you submit the data to the server, then by default Editor will use HTTP variables to represent the data. These are plain text and so any type may be lost. Most server-side frameworks will automatically attempt to detect the type and convert it (PHP for example does this).
Correct - JSON cannot represent complex objects like
Date
. For that you need to use a string representation of the date and then convert it back on the server-side.The other thing to be aware of is that when a value is read from an
<input>
field, it is read as a string. Again there is no type information. That would explain why you are currently usingparseInt
for yourrowNum
input.In this case, yes I would say so since you are not using a server-side database. I would point out however that the
obj
you are returning tosuccess
does not conform to what Editor expects. Specifically therow
parameter is missing.My suggestion would be that the callbacks / events you are using for Editor should ensure that DataTables always sees the same type for each data point. i.e. if
rowNum
is a number on load, it should always be a number.Could you clarify what you mean by "DataTable's
Data
array"? DataTables will just hold whatever information you give it for each row.The whole point of the
columns.render
option is that the underlying data is not modified in order to present formatted data to DataTables. The oldfnRender
method thatcolumns.render
replaced did write to the data source object and it was a compatibility and performance nightmare. It was dropped in 1.10 as it wouldn't have been possible to do much of what 1.10 does with it!What you have looks okay, but again I would suggest you are consistent with the data given to DataTables. If it is a
Date
object then it should always expect aDate
object and anything else is a bug. That might require type casting in an event handler as discussed above, to a large extent it depends upon what the date input type you are using does! Some will happily give you aDate
object, some ISO8601 always, etc.Regards,
Allan