Custom JSON Format

Custom JSON Format

anais1477anais1477 Posts: 4Questions: 1Answers: 0

Hello,

I know this question has been asked many times. So I tried what was suggered on other threads but it still doesn't work for me.
This is my custom JSON :

{
    "data": [{
        "abc": {
            "key": "abc",
            "title1": "",
            "title2": "",
            "title3": "",
            "title4": "",
            "title5": "",
            "title6": "myvalue",
            "title7": "",
            "title8": "",
            "title9": "",
            "title10": "",
            "title11": "hello",
            "title12": 1
        },
        "def": {
            "key": "def",
            "title1": "",
            "title2": "",
            "title3": "goodbye",
            "title4": "",
            "title5": "",
            "title6": "myvalue",
            "title7": "",
            "title8": "",
            "title9": "",
            "title10": "",
            "title11": "hello",
            "title12": 1
        }
    }]
}

I tried using ajax.dataSrc option :

$(document).ready(function() {
    $('#example').DataTable( {      
        "ajax": {
                "url": "staff.php",
                "dataType": "json",
                "contentType": "application/json; charset=utf-8" 
                },      
            "dataSrc": "data"    
    } );

But I get the following error :

DataTables warning: table id=example - Requested unknown parameter '0' for row 0, column 0. For more information about this error, please see http://datatables.net/tn/4

Then I tried to explicitely write columns :

$(document).ready(function() {
    $('#example').DataTable( {
        
        "ajax": {
                "url": "staff.php",
                "dataType": "json",
                "contentType": "application/json; charset=utf-8"
 
                },
        "aoColumns": [
        { "mData": "key" },
        { "mData": "title1" },
        { "mData": "title2" },
        { "mData": "title3" },
        { "mData": "title4" },
        { "mData": "title5" },
        { "mData": "title6" },
        { "mData": "title7" },
        { "mData": "title8" },
        { "mData": "title9" },
        { "mData": "title10" },
        { "mData": "title11" },
        { "mData": "title12" }
    ]
    } );
} );

And I get the same error.

I can I configure datatable to read this json format ?

Answers

  • anais1477anais1477 Posts: 4Questions: 1Answers: 0

    Up

  • allanallan Posts: 61,743Questions: 1Answers: 10,111 Site admin

    Use data: 'abc.key' and data: 'def.key' etc.

    This assumes that the data array contains a single object for each row.

    If in fact the data array contains only a single object which itself has multiple keys, each representing a row, that is not something DataTables currently supports. DataTables requires an array where each element inside it (which can be an array or an object) represents a row.

    Allan

  • anais1477anais1477 Posts: 4Questions: 1Answers: 0

    Yes there is a single object for each row, but I don't know where to put data: 'abc.key' ??
    Because there are hundreds of keys like abc or def

  • allanallan Posts: 61,743Questions: 1Answers: 10,111 Site admin

    You come back to this point then:

    This assumes that the data array contains a single object for each row.

    Is that true or not? Its ambiguous from the above example.

    Allan

  • anais1477anais1477 Posts: 4Questions: 1Answers: 0

    Yes for example the object "abc" correspond to one row of the datatable

This discussion has been closed.