dynamically creating thead columns using ajax source

dynamically creating thead columns using ajax source

wachesonwacheson Posts: 5Questions: 2Answers: 0

I noticed on the installation page, the following:
"Note that DataTables can actually generate the thead and tbody for you, along with all of the rows and cells of the table, if you are using Ajax sourced data,...".
I've looked through the online examples, though, and haven't found a suitable example? Would it be possible to post one, that would demonstrate this?

To use for an example, my code that Im working with is very similar to:
/examples/server_side/object_data.html

Here's what Im trying to accomplish:
I want to be able to dynamically provide the thead (columns.title) with my ajax request data received.
My hope is that I'd like to add an additional array/object like 'thead_array', for example, to provide the thead column titles.

I will probably be doing a customized ajax request, so Im assuming I'll have to do something like the following?
"ajax": function (data, callback, settings) {

callback(

);

}

In datatables 1.9.x, i do a custom ajax request like the following:
,'fnServerData': function( sUrl, aoData, fnCallback, oSettings ) {
oSettings.jqXHR = $.ajax( {
'url': sUrl
,'type': 'POST'
,'timeout': 10000
,'data': aoData
,'success': fnCallback
,'dataType': 'jsonp'
,'cache': false
});
}
So, Im going to assume that I've got to do something like that here as well.
But once I've done the ajax request, how do I get my 'thead_array' and use that for my columns titles?

I do have a way to build the html dynamically and send it back from another ajax call, but I think itd make more sense with datables able to do that part for me as well.

Thanks.

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin
    Answer ✓

    Hi,

    The note in the documentation, while technically correct, is a bit misleading - I'll alter what it says.

    In short, DataTables can create the header elements for you, but it cannot do it with information from the Ajax sourced data that DataTables obtains. If you want to create the columns from the Ajax information, you need to make the Ajax call yourself and then use the columns and columns.title options to create the columns with the required titles.

    It is a bit of an annoying limitation this - the reason for it is that JSON cannot convey all of the information that can be used in the columns option - specifically functions. I'm planning in a future release of DataTables to allow this, but it will still be that only a subset of the functionality will be available.

    Regards,
    Allan

This discussion has been closed.