Auto Column Headers?

Auto Column Headers?

orangeorange Posts: 20Questions: 0Answers: 0
edited February 2010 in General
I have DataTables currently pulling data from a JSON file and it's working great.

I'm wondering if the column headers can be generated from the JSON data as opposed to defined in the js.

What example or code samples should I be looking at to make this happen?

thank you.

Replies

  • allanallan Posts: 63,161Questions: 1Answers: 10,406 Site admin
    What you need to do in this case is create the XHR yourself and then give DataTables the object that is returned ( something like this http://datatables.net/examples/data_sources/js_array.html ). This way you can have the full JS object in the JSON file, and you just pass it to DataTables for the initialisation:

    [code]
    $.getJSON( 'source', null, function(json) {
    $('whatever').dataTable( json );
    } );
    [/code]
    Regards,
    Allan
  • orangeorange Posts: 20Questions: 0Answers: 0
    Update:

    This worked. Thank you for the insight.
  • dpaustindpaustin Posts: 3Questions: 0Answers: 0
    What would the json look like if we wanted to set up multiple headers rows with colspan?
    Thanks.
  • orangeorange Posts: 20Questions: 0Answers: 0
    edited May 2010
    I used something like this (not sure if this is what your looking for):

    [code]
    {
    "aoColumns": [
    {
    "sTitle": "Rendering Engine"
    },
    {
    "sTitle": "Browser"
    },
    {
    "sTitle": "Platform(s)"
    },
    {
    "sTitle": "Engine version"
    },
    {
    "sTitle": "CSS grade"
    }
    ],
    "aaData": [
    [
    "Trident",
    "Internet Explorer 4.0",
    "Win 95+",
    "4",
    "X"
    ],
    [
    "Trident",
    "Internet Explorer 5.0",
    "Win 95+",
    "5",
    "C"
    ],
    [
    "Trident",
    "Internet Explorer 5.5",
    "Win 95+",
    "5.5",
    "A"
    ],
    [
    "Trident",
    "Internet Explorer 6",
    "Win 98+",
    "6",
    "A"
    ],
    [
    "Trident",
    "Internet Explorer 7",
    "Win XP SP2+",
    "7",
    "A"
    ],
    [
    "Trident",
    "AOL browser (AOL desktop)",
    "Win XP",
    "6",
    "A"
    ],
    [
    "Gecko",
    "Firefox 1.0",
    "Win 98+ / OSX.2+",
    "1.7",
    "A"
    ]
    ]
    }
    [/code]
This discussion has been closed.