The key to solving this - which nobody really addressed - is this: As stated in the question we are using DataTables in serverSide
mode. It has pagination and when you click on each page it makes a separate ajax request to load just the data for that page. So for example if you click page 2, 3, 4 it is making 3 separate ajax requests - one per page. Herein lies the problem - if you want to scroll to row 999 you have to answer the question "on what page does row 999 appear"?
The only way you can do this is to call a serverside script which will calculate the page number and return it to the js used by the application. The reason this is needed is because if, for example, you were on page 1 and row 999 happens to be on page 4, you do not have the rows for page 4 anywhere in the DOM; indeed you wouldn't have these at all until you click page 4 on the pagination.
So, the first thing to do is to make an ajax request to an endpoint that calculates this page number and returns an integer. How the page number is calculated depends on the application. @Sascha Gottfried has kind of hinted at this in their answer. In our application the rows are not sequential, i.e. you could have row_999
followed by row_1052
:
If they are sequential you could do something equivalent to ceil(999/250)
which would tell you that row 999 appears on page 4, when there are 250 records per page.
If they are NOT sequential (as is the case in my application) the endpoint must provide a means of calculating it. There is no set answer to how you do this. In our application it depends on multiple search/filter parameters which can result in rows being out of sequence. However all the script needs to do is return an integer, e.g. echo json_encode(['page' => 4]);
Once this number is calculated you have to trigger the equivalent click for "page 4" (or whatever page number you want) in DataTables. This can be done in jquery by targetting the anchors inside .dataTables_paginate
.
Then, you have to scroll down to the row - some information was provided by @ygorbunkov relating to this. Missing or not obvious is that you have to wait until the ajax request to populate the DataTable has completed before you attempt to scroll to the row - otherwise the data is not present to scroll to. This can be done by nesting a .done()
in the ajax request to the endpoint which gets the data for page 4.
Although this is an incomplete answer these are the principles that are required. In the end we actually abandoned doing this in our application because the complexity of trying to implementing it outweighed any benefit!