Initialize a datatable without knowing the columns

0

How can I initialize a datatable from a json array that I do not know how many columns it has?

json = '[["DAT","XXXXXX","000000000","MA_","","21-07-2015","00:04:43","0","354345","94131","Datos","0.00000000"],  ["DAT","XXXXXX","000000000","MA_","","21-07-2015","00:04:43","0","354345","94131","Datos","0.00000000"]]';

json = JSON.parse(json)
var table = $('#example').DataTable({
    data:json ,
    columns: [
        { title: 'dato' },
        { title: 'dato' },
        { title: 'dato' },
        { title: 'dato' },
        { title: 'dato' },
        { title: 'dato' },
        { title: 'dato' },
        { title: 'dato' }, 
    ]
}); 

What I'm looking for is declaring the datatable without having to use the columns option since I do not know how many columns will be passed to it

Example

    
asked by Takyo 27.02.2017 в 18:19
source

1 answer

0

'columnDefs' is used for that purpose. See the reference in datatables.net

json = JSON.parse(json)
var table = $('#example').DataTable({
    data:json,
    "columnDefs": [ {
        "targets": -1,
        "data": null,
        "defaultContent": "dato"
    } ]
}); 
    
answered by 27.02.2017 в 21:35