Call initComplete on table.ajax.reload()

Call initComplete on table.ajax.reload()

jnajna Posts: 19Questions: 5Answers: 0

Due to special layout on my webpage I need to get the "recordsTotal" value in a function when I do a table.ajax.reload()
When I do my initial datatable init I have an initCompletefunction where I can get the value in "settings._iRecordsTotal"

But initComplete is not called on ajax.reload. Is there any way to pass the "settings._iRecordsTotal" value when you do a ajax.reload?

I have tried to do a callback function but can 't get i to work: this.$datatable.DataTable().ajax.reload( function(){ this.totalHitsRender(); alert('COME ON') });
If I could only call my totalHItsRender() function like that....

Answers

  • kthorngrenkthorngren Posts: 21,309Questions: 26Answers: 4,947
    edited November 2018

    initComplete is only called once, after the Datatable is initialized. Here is an example of using a callback with ajax.reload(). The callback uses page.info() to display info about the page. You can get recordsTotal from that API.

    http://live.datatables.net/tofosoto/1/edit

    Kevin

  • jnajna Posts: 19Questions: 5Answers: 0

    Hello again
    The example you provide only make one initial request so I can't see if your example is working. I have a feeling i does not work.
    I have tried 2 ways into your API:
    this.$datatable.DataTable().ajax.reload(this.totalHitsRender(this.$datatable.DataTable().page.info().recordsTotal))
    And
    this.$datatable.DataTable().ajax.reload(this.totalHitsRender(this.$datatable.DataTable().ajax.json().recordsTotal))

    They do the same thing. The result is the PREVIOUS reload, meaning the pageInfo is always 1 step behind. It is not synced with the data that the DataTable is showing.

    Does ther exist any examples where the callback from ajax.reload() is working?

  • kthorngrenkthorngren Posts: 21,309Questions: 26Answers: 4,947

    Ok, I updated my example to show the callback is working:
    http://live.datatables.net/tofosoto/6/edit

    Change the page length then click the reload button. The Page Length field should be updated with the page.info().length value of the current ajax.reload() request.

    Not sure why yours isn't working. Please provide a link to your page or a test case so we can help debug.
    https://datatables.net/manual/tech-notes/10#How-to-provide-a-test-case

    Kevin

  • jnajna Posts: 19Questions: 5Answers: 0

    Ok, thanks a lot. But i still don't see any changes when calling the reload request. The sum is the same no matter what the result is.
    Maybe there is a difference when it is serverside?

    Here is a link to my working example. if you open the networks tab and the the preview tab. You will see the new json everytime you do a search. The totalresults value on the page is always one step behind. That goes for both my attempts:

    this.$datatable.DataTable().ajax.reload(this.totalHitsRender(this.$datatable.DataTable().page.info().recordsTotal))
    And
    this.$datatable.DataTable().ajax.reload(this.totalHitsRender(this.$datatable.DataTable().ajax.json().recordsTotal))

    https://sundhedsstyrelsen-test-342250-cm.azurewebsites.net/Nyheder

  • jnajna Posts: 19Questions: 5Answers: 0

    Ok, I have fixed it by making my own json call. Is it possible to see a working example of DataTable().ajax.reload with a callback function. As I see it it does the callback but it does not work with the reloaded ajax....

  • kthorngrenkthorngren Posts: 21,309Questions: 26Answers: 4,947

    I put a breakpoint in your totalHitsRender() function and a watch for this.$datatable.DataTable().page.info().recordsTotal. The watch value seems to follow the value displayed on the page (Din søgning gav 1436 resultater). It looks like the value displayed on the page is based on the second ajax call you have created.

    Maybe you can put back the original code for us to look at.

    The ajax.reload() callback passes the JSON received to the callback. Maybe you can try something like this and just get the length of the data in the JSON instead of sending a second request:

    Modify the callback be removing the ():

    this.$datatable.DataTable().ajax.reload(this.totalHitsRender);

    Add the json parameter to the function and get the length of the data:
    function totalHitsRender(json) {

    I'm not familiar with your environment so I may be off track with my suggestions.

    Kevin

  • jnajna Posts: 19Questions: 5Answers: 0

    First of all it is great with any kind of suggestions :-)
    I couldn't get it to work but your post led me to read more about the json api and i ended up using this.$datatable.DataTable().on( 'xhr', () =>{//Code}
    This does the job. So thanx a lot :-)

This discussion has been closed.