Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataTables: Columns with dot (.) in header not displaying correctly

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"
        }
    ]
}
like image 457
Peter G. Avatar asked Nov 13 '15 11:11

Peter G.


People also ask

How do you fix a column in a DataTable?

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.

What is autoWidth in DataTable?

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.

What is FixedColumns in DataTables?

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.

How much data can DataTables handle?

The maximum number of rows that a DataTable can store is 16,777,216.


2 Answers

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/

like image 57
davidkonrad Avatar answered Nov 15 '22 07:11

davidkonrad


Try this:

"columns": [
    {
        "data": "Doc&#46;",
        "title": "Doc&#46;"
    },
    {
        "data": "Order no&#46;",
        "title": "Order no&#46;"
    }
],
"data": [
    {
        "Doc&#46;": "564251422",
        "Order no&#46;": "56421"
    },
    {
        "Doc&#46;": "546546545",
        "Order no&#46;": "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"
    }
]
like image 43
annoyingmouse Avatar answered Nov 15 '22 06:11

annoyingmouse