fnReloadAjax in 1.10.0-dev

fnReloadAjax in 1.10.0-dev

tstartstar Posts: 36Questions: 0Answers: 0
edited August 2013 in General
I need to reload the new data into a table via Ajax request.
[code]oTable.fnReloadAjax(null, function () {
alert("Ok.");
});[/code]
This code work fine in 1.9.4, but in 1.10.0-dev i get exception "Object does not support property or method "_fnServerParams"" in fnReloadAjax.js in line 35 "this.oApi._fnServerParams( oSettings, aData );". I could not find a solution to this problem. Maybe in the new version this feature is already built in?

Replies

  • allanallan Posts: 63,512Questions: 1Answers: 10,472 Site admin
    DataTables 1.10 has this ability built into the new API. You would use:

    [code]
    var table = $('#example').DataTable(); // NOTE the capital D - this gets an API instance rather than jQuery

    table.ajax.reload();
    [/code]

    The API file for the Ajax options in DataTables 1.10 is here: https://github.com/DataTables/DataTablesSrc/blob/1_10_wip/js/api/api.ajax.js

    Allan
  • tstartstar Posts: 36Questions: 0Answers: 0
    Thank you for your immediate response! But I can not check it, because functions fnGetPosition, fnGetData now throw exception. They are absent in the new API?
  • tstartstar Posts: 36Questions: 0Answers: 0
    I get it. I do not have to initialize the table with the new API. I have to do is just to call up reloading data. it does not re-create the table again. Do I understand correctly?
  • allanallan Posts: 63,512Questions: 1Answers: 10,472 Site admin
    Correct. The old API is still present (as a shim layer to the new one) - just use a lower case `d` when creating the instance and you get the same as 1.9-. There is also a new method (api()) which will give you the api instance - i.e. `$('#example').dataTable().api()` is the same as `$('#example').DataTable();` .

    The new API provides everything of the old, and more so you shouldn't need the old API at all. Of course it isn't documented yet, so you might want the old API... For data - you can simply use `table.row( selector ).data()` for example, rather than fnGetData. Overall the new API should be much easier to use.

    Allan
  • tstartstar Posts: 36Questions: 0Answers: 0
    Can I use this construstion in my code?
    [code]var oTable = $('#test').dataTable();
    oTable.DataTable().ajax.reload();[/code]
    It works for me, but would not that be a problem?

    And thanks again for your help in my understanding of the new version.
  • tstartstar Posts: 36Questions: 0Answers: 0
    Allan! Now everything has fallen into place in my brain. A thousand thanks! And sorry for my clumsy English.
  • tstartstar Posts: 36Questions: 0Answers: 0
    Yes, indeed! Everything works as you said. The only fnReloadAjax had a fnCallback at the end of update. The new API is no such fnCallback in ajax.reload(). But I have found a replacement :

    [code]"fnServerData": function (sSource, aoData, fnCallback) {
    $.ajax({
    success: function (json) {
    fnCallback(json);
    alert("Ok");
    },
    error: function (jqXHR, textStatus, errorThrown) {
    alert("Error: " + jqXHR.responseText);
    }
    });
    }[/code]
  • allanallan Posts: 63,512Questions: 1Answers: 10,472 Site admin
    > oTable.DataTable().ajax.reload();

    Yes that will work - but you'd be better using:

    [code]
    oTable.api().ajax.reload();
    [/code]

    I think. It will just save a few clock cycles.

    Regarding the callback - that's a good point. You could do:

    [code]
    table
    .one( 'xhr', function () { ... } )
    .ajax.reload();
    [/code]

    where the `xhr` event handler is the callback.

    I'll look at adding this in explicitly.

    Allan
  • tstartstar Posts: 36Questions: 0Answers: 0
    If callback will be implemented in the .ajax.reload(), it will be fine. I do not understand what is [quote].one( 'xhr', function () { ... } )[/quote]? I have not found anything in the DataTables source. Although, I have not tried to run your code, maybe it will work. :)
  • allanallan Posts: 63,512Questions: 1Answers: 10,472 Site admin
    Hopefully it will work :-). `one` is a jQuery method which DataTables will "proxy" to jQuery - http://api.jquery.com/one/ . Its just an event handler which is run only once. The `xhr` event occurs when the Ajax from the source is loaded: http://datatables.net/docs/DataTables/1.9.4/#xhr

    Allan
  • tstartstar Posts: 36Questions: 0Answers: 0
    Holy shit! I am lamer :) Thank you.
This discussion has been closed.