DataTables callbacks and infinite requests

DataTables callbacks and infinite requests

blondeheronblondeheron Posts: 4Questions: 0Answers: 0
edited November 2012 in DataTables 1.9
Hi all,
I'm experiencing the same problem described in this old discussion: http://datatables.net/forums/discussion/2259/fnserverdata-makes-infinite-http-request-when-we-have-the-callback-function/p1.

I'm using (as also described in the old discussion):
- server side processing
- sAjaxSource
- fnServerData
- $.ajax() calls in fnServerData, calling REST services
- callback function.

DataTables version is 1.9.4.

Replies

  • blondeheronblondeheron Posts: 4Questions: 0Answers: 0
    my debug code is: aloxew (http://debug.datatables.net/aloxew)
  • allanallan Posts: 63,394Questions: 1Answers: 10,450 Site admin
    What does `buildTableDataAsync(data, fnCallback);` do? it doesn't actually build the table again does it? Otherwise, every request is going to rebuild the table - hence an infinite loop.

    Allan
  • blondeheronblondeheron Posts: 4Questions: 0Answers: 0
    Thanks for your quick response!

    [code]buildTableDataAsync(data, fnCallback);[/code] is:

    [code]
    function buildTableDataAsync(json, callback) {
    var credentials = json.aaData;
    var buildPromises = [];
    $.each(credentials, function(i, entry) {
    buildPromises.push(buildRow(entry));
    });
    $.when.apply(null, buildPromises).always(function() {
    aTable.fnClearTable();
    aTable.fnAddData(rows);
    if (callback) callback(json);
    });
    }
    [/code]

    In my intention:
    1) the table loads partial table data via REST JSON service.
    2) the buildTableDataAsync function makes two ajax calls (other rest services) per table row, composes the whole dataset and updates the table.
    Unfortunately, using [code]fnClearTable[/code] or [code]fnAddData[/code] creates the infinite loop. So I'm doing something wrong.

    I read using methods like [code]fnClearTable[/code] or [code]fnAddData[/code] using server-side processing could be bad, but this seemed to be the only viable way. I hope there's another solution...
  • allanallan Posts: 63,394Questions: 1Answers: 10,450 Site admin
    > aTable.fnAddData(rows);

    Will do a redraw. As will fnClearTable. Hence the problem.

    I'd suggest you manipulate the data as needed and then use the provide callback method.

    Allan
  • blondeheronblondeheron Posts: 4Questions: 0Answers: 0
    So, a redraw triggers fnServerData and my first request (for partial data)?

    Manipulating the data, in my table, involves calling buildTableDataAsync. Maybe have I to manipulate the data before invoking datatables?
  • allanallan Posts: 63,394Questions: 1Answers: 10,450 Site admin
    > So, a redraw triggers fnServerData and my first request (for partial data)?

    It does if you have server-side processing enabled since there is no data 'held' at the client. It's really just a dumb viewer and event manger. Every draw, with server-side processing enabled, must call the server.

    > Manipulating the data, in my table, involves calling buildTableDataAsync. Maybe have I to manipulate the data before invoking data tables?

    If you are using server-side processing you need to manipulate it on every load. What is the manipulation you are going. You can bash the JSON around anyway you want and pass it into the callback in fnServerData :-)

    Allan
This discussion has been closed.