DataTables seems to treat dots (.
) as a special character and doesn't display the columns, which have them in the header.
Is there any solution how to keep the dots and use some sort of escape character?
The error is: Requested unknown parameter 'Doc.' for row 0, column 0
My JSON DataTable initializer:
{
"columns": [
{
"data": "Doc.",
"title": "Doc."
},
{
"data": "Order no.",
"title": "Order no."
}
],
"data": [
{
"Doc.": "564251422",
"Order no.": "56421"
},
{
"Doc.": "546546545",
"Order no.": "98745"
}
]
}
FixedColumns allows columns to be fixed from both the left and the right hand sides of the table. Fixing right hand-side columns is done by using the fixedColumns. right initialisation parameter, which works just the same as fixedColumns. left does for the left side of the table.
The autoWidth option is used to specify whether the automatic calculation of the column width in the DataTable is enabled or not. This calculation takes a little time and may be disabled when the column widths are explicitly passed using the columns.
FixedColumns provides the option to freeze one or more columns to the left or right of a horizontally scrolling DataTable. This allows information in the fixed columns to remain visible even when scrolling through the data set. This can be particularly useful if you wish to show a large number of columns.
The maximum number of rows that a DataTable can store is 16,777,216.
When there is dots in data property names dataTables will look for nested data properties. Like if there was as Doc.<something>
property. When you have automated scripts - as you have - the server is distributing JSON not necessarily designed for dataTables, you can "sanitize" the data before passing it to dataTables.
I took the answer from yesterday and added two functions : sanitizeData
and sanitizeColumns
. sanitizeData
removes all dots and whitespaces from keys, sanitizeColumns
remove whitespace and dots from the columns.data
definitions :
$.getJSON('https://api.myjson.com/bins/4z5xd', function(json) {
//remove whitespace and dots from keys / attribute names
function sanitizeData(jsonArray) {
var newKey;
jsonArray.forEach(function(item) {
for (key in item) {
newKey = key.replace(/\s/g, '').replace(/\./g, '');
if (key != newKey) {
item[newKey]=item[key];
delete item[key];
}
}
})
return jsonArray;
}
//remove whitespace and dots from data : <propName> references
function sanitizeColumns(jsonArray) {
var dataProp;
jsonArray.forEach(function(item) {
dataProp = item['data'].replace(/\s/g, '').replace(/\./g, '');
item['data'] = dataProp;
})
return jsonArray;
}
json.data = sanitizeData(json.data);
json.columns = sanitizeColumns(json.columns);
$('#example').DataTable({
data : json.data,
columns : json.columns
})
});
demo -> http://jsfiddle.net/egpxdsq7/
Try this:
"columns": [
{
"data": "Doc.",
"title": "Doc."
},
{
"data": "Order no.",
"title": "Order no."
}
],
"data": [
{
"Doc.": "564251422",
"Order no.": "56421"
},
{
"Doc.": "546546545",
"Order no.": "98745"
}
]
Though this also works:
"columns": [
{
"data": "Doc.",
"title": "Doc."
},
{
"data": "Order no.",
"title": "Order no."
}
],
"data": [
{
"Doc": "564251422",
"Order no": "56421"
},
{
"Doc": "546546545",
"Order no": "98745"
}
]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With