Go to last page after ajax.reload with new page created

Go to last page after ajax.reload with new page created

fiofiottefiofiotte Posts: 9Questions: 3Answers: 1

Hi,

Let's say that you have a DataTable initialized with a length menu view set at 5 with 5 records in it.
So all of them are visible and you only have one page on your DataTable.

Now, if I try to create one more record, it add a new page to my DataTable.

But why when I try to do :

.on( 'postCreate', function (e, json) {
              $("#example").DataTable().ajax.reload( null, false );
              $("#example").DataTable().page('last').draw('page');
          } )

The DataTable isn't going to the last page !!!

But, if I already have two pages on my DataTable (more than 5 records) and I add one more record, the DataTable is going to the last page !!

It's only when a new page is created with the creation of the new record. Like if the second line of code was executed before the end of the reload once and thinking the Table has only one page.

I tried to put a timeout function or set the second line inside the callback function of the first one but no chance.

C.

Answers

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    Like if the second line of code was executed before the end of the reload once and thinking the Table has only one page.

    That will be what is happening - the ajax is async, so the page command would be executed before the new data has loaded.

    However, there should really be no need to execute the ajax.reload() method with Editor - the data returned from the server should contain everything needed.

    You should be able to simply use:

    .on( 'postCreate', function (e, json) {
                  $("#example").DataTable().page('last').draw('page');
              } )
    

    If that doesn't work for you, can you link to the page showing the issue please.

    Allan

  • fiofiottefiofiotte Posts: 9Questions: 3Answers: 1

    Hi Allan,

    after our discuss about PHP and PostCreate

    I needed to set the ajax.reload() function to be able to set the last added record to the end of the DataTable because of the postCreate function in the PHP file.

    But the problem isn't there, you can easily try the experience with whatever DataTable you want. Take a DataTable that have just one page, set the page() function on postCreate to go to the last page and then add a new record (that will also add a new page). The record will be added but the DataTable will still be at the first page.

    C.

  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin

    That is correct - the DataTable will remain on the current page rather than jumping to the position of the new row. Setting the page to last would not, I think be the correct thing to do here since your user might not want to go to the last page (the row might be inserted on the first page for example).

    I'll try to produce an example this week (although it might be next as this one is exceptionally busy) showing how to jump to the newly created row.

    Allan

  • fiofiottefiofiotte Posts: 9Questions: 3Answers: 1

    Ok.

    Thanks Allan.

    C.

This discussion has been closed.