Getting column names from AJAX source

Getting column names from AJAX source

TraXXTraXX Posts: 4Questions: 0Answers: 0
edited December 2010 in General
I had searched this forum for an answer for it without success.

My intention is getting aoColumns array (with bSortable, sType and sTitle values) from AJAX source.

This is my code:
[code]
function fnDataTablesPipeline ( sSource, aoData, fnCallback ) {
var iPipe = 5;

aoData.push({
"name":"colName",
"value":"COL1"
});

var bNeedServer = false;
var sEcho = fnGetKey(aoData, "sEcho");
var iRequestStart = fnGetKey(aoData, "iDisplayStart");
var iRequestLength = fnGetKey(aoData, "iDisplayLength");
var iRequestEnd = iRequestStart + iRequestLength;
oCache.iDisplayStart = iRequestStart;

/* outside pipeline? */
if ( oCache.iCacheLower < 0 || iRequestStart < oCache.iCacheLower || iRequestEnd > oCache.iCacheUpper )
{
bNeedServer = true;
}

/* sorting etc changed? */
if ( oCache.lastRequest && !bNeedServer )
{
for( var i=0, iLen=aoData.length ; i

Replies

  • allanallan Posts: 63,400Questions: 1Answers: 10,452 Site admin
    Currently DataTables only allows the data array to be given by the server-side process when considering it's built in Ajax get methods. It is possible however to wrap the initialisation call for DataTables up in your own Ajax call as shown here: http://datatables.net/forums/comments.php?DiscussionID=3442&page=1#Item_10

    It won't be perfect with server-side processing though as you'll need two calls to the server, one for aoColumns and one for aaData (the second is the one DataTables will make itself). It is possible to work around this I suspect, but it would involve tinkering with some of the internals of DataTables and could be rather messy.

    Regards,
    Allan
  • TraXXTraXX Posts: 4Questions: 0Answers: 0
    I had read this discussion before posting here.
    My problem doing that is that I need some variables in my PHP script sent by datatables like $_POST['sSearch'], $_POST['bSearchableX'], $_POST['iSortingCols']... (my script is very similar to server_processing.php from datatables examples).

    When I do first AJAX call, these variables aren't sent and I retrieve errors from PHP script.
  • TraXXTraXX Posts: 4Questions: 0Answers: 0
    I have tried to change a column title from $.ajax callback of my fnDataTablesPipeline function with fnSettings:

    [code]
    $.ajax({
    "dataType": "json",
    "type": "POST",
    "url": sSource,
    "data": aoData,
    "success": function (json) {
    /* Callback processing */
    oCache.lastJson = jQuery.extend(true, {}, json);

    if ( oCache.iCacheLower != oCache.iDisplayStart )
    {
    json.aaData.splice( 0, oCache.iDisplayStart-oCache.iCacheLower );
    }
    json.aaData.splice( oCache.iDisplayLength, json.aaData.length );

    gTable.fnSettings().aoColumns[1].sTitle = 'Datatables Title 1';

    fnCallback(json);
    }
    });
    [/code]

    If then, I write "console.log(gTable.fnSettings().aoColumns[1].sTitle)", firebug shows me the correct title (Datatables Title 1), but the column title in datatable remains empty.
    I have tried to redraw the table with "gTable.fnDraw(false)" with no results.

    It would be a good solution for me because, in this callback, I can access the json data returned by the server and this data contains the aoColumns array that I like use in my table.
  • allanallan Posts: 63,400Questions: 1Answers: 10,452 Site admin
    Hi TraXX,

    Thanks for the clarification - I think I understand what you are getting at now. So the column titles are the key thing for you here, and it looks like you've got about 99% of it already done :-). The one this which is yet to be done, as you point out, is to write the title to the DOM. Javascript (at least 'mass consumption' Javascript) doesn't provide getters and setters yet, so DataTables can't write to the DOM for you when you do the setting of sTitle. However, it's a fairly simple line to add it in:

    [code]
    gTable.fnSettings().aoColumns[1].nTh.innerHTML = 'Datatables Title 1';
    [/code]
    Regards,
    Allan
  • TraXXTraXX Posts: 4Questions: 0Answers: 0
    edited December 2010
    Not only the title. I want to load to aoColumns some more attributes (sWidth, bSearchable, sType, etc) from json array.

    How can I change these other?
  • allanallan Posts: 63,400Questions: 1Answers: 10,452 Site admin
    Hi TraXX,

    It's not generally possible to modify DataTables settings during run time - the sTitle option above makes us of the internal structure, so it is possible, but you need to know what to set and how. The way that I would typically recommend getting the full aoColumns object is shown in the thread I liked to - something like this (a bit simplified):

    [code]
    $(document).ready(function() {
    $.ajax( {
    "dataType": 'text',
    "type": "GET",
    "url": "dtJSON.txt",
    "success": function (dataStr) {
    var data = eval( '('+dataStr+')' );
    $('#example').dataTable({
    "aaData": data.aaData,
    "aoColumns": data.aoColumns,
    });
    }
    } );
    });
    [/code]
    Allan
This discussion has been closed.