Scroll to specific row using server-side
Scroll to specific row using server-side
I just converted a datatable implementation to server side (since I will have much more data in the future). In my non-server side implementation, I am able to scroll to a specific row using row().scrollTo(). I have read here that scrollTo() does not work for server side, and that I should instead use scroller.toPosition() - from your documentation::
Please note that it is not always possible to select all rows in the table using the row() selector method - specifically when server-side processing is enabled, only the rows that are loaded into the client-side can be selected. If you need to jump to a specific display index, use the scroller.toPosition() method.
I tried using row().index() to retrieve the index of the row I would like to scroll to to feed to scroller.toPosition(). If the row is within the first set of rows returned from the server, this works fine. If the row is not in the first set of rows from the server, the result of this function is "undefined".
So, basically, I would like to scroll to a specific row when the datatable is opened. How do I accomplish this using server-side?
Answers
I found a possible solution on the server side, but it is very inefficient. I perform an ajax call to calculate the index by iterating over the query set until it matches the row's ID - then I return that index. scroller.ToPosition(index) then scrolls to the correct row.
One of the reasons I converted to server side is because I may have hundreds of thousands or millions of rows at some point, so the solution I came up with probably will kind of defeat the purpose. It seems that row().index() would be an ideal solution if it would returned the actual index number of a row outside of the currently loaded rows. Am I using this function incorrectly for server side? Is there another client-side function that would work in this situation?
I am having a similar problem adding a class (for highlighting) to the row outside the loaded rows as well.
$(table.row(index).node()).addClass('highlight-row');
This works great if the row is in the currently loaded rows. If not, no highlighting happens. Is there also a similar client-side solution to that as I am not sure how to accomplish this on the server side?
Thank you
I found a possible solution on the server side, but it is very inefficient. I perform an ajax call to calculate the index by iterating over the query set until it matches the row's ID - then I return that index. scroller.ToPosition(index) then scrolls to the correct row.
One of the reasons I converted to server side is because I may have hundreds of thousands or millions of rows at some point, so the solution I came up with probably will kind of defeat the purpose. It seems that row().index() would be an ideal solution if it would returned the actual index number of a row outside of the currently loaded rows. Am I using this function incorrectly for server side? Is there another client-side function that would work in this situation?
I am having a similar problem adding a class (for highlighting) to the row outside the loaded rows as well.
$(table.row(index).node()).addClass('highlight-row');
This works great if the row is in the currently loaded rows. If not, no highlighting happens. Is there also a similar client-side solution to that as I am not sure how to accomplish this on the server side?
It appears that there is also a similar problem with datatable keys. In the client side, I am able to focus on a specific cell in a row. In the server-side version, the focus does not seem to work unless the focus is in the first few rows of the table. I am using the technique of scroller.toPostion(index) to actually scroll to the row I would like to focus on, but the same code that works fine focusing on the client side does not place focus when server side is active:
table.cell($("#id_rowx").find('td:first')).focus();
Is there another technique I should be using or something I should be doing prior to trying to focus on a cell or highlight a row?
Thank you
Not sure where you are using that statement but you can look at using
rowCallback
ordrawCallback
to add the class to the client side rows after each draw.Would guess the
td
with the id ofid_rowx
is not in the client when this statement is executed.Sounds like this is the only method you can use with server side processing and
-pi scroller.toPosition()
. See Allans answer in this thread and also this thread. Unless you have a way to algorithmically calculate the row index based on the row's ID in the client you will need to rely on the server to perform the calculation.Kevin
Thank you Kevin for the response, much appreciated.
So, I am running the scroller.toPosition to that row first before calling the focus. Do I need to perform some sort of "wait till this loads" after calling toPosition and before .focus()?
Use
initComplete
fo this.Kevin
Hi Kevin,
Thank you again. Unfortunately I am not trying to focus immediately upon opening the datatable. While the datatable is open, the user can click a button that opens a modal form allowing the user to add a new record. When the record is saved to the DB, the modal closes, I do a table.ajax.reload(), then run an ajax call to get the new row's index. IN the ajax "success" function, I run the toPosition, and in the ajax "complete", I try to do the focus. (Same for highlighting the row - I have not tried your other suggestion there yet)
Sounds like you will want to use one instance of the
xhr
event. Something like this maybe?This might work
Kevin
Yet again, thank you for responding!
Did you mean this?
The scroller.toPostion call still scrolls to the right spot - the new row is visible, but no luck with it getting focus
First you need to change
table.on.('xhr.dt',
totable.one.('xhr.dt',
so you invoke the event only once. Otherwise you will be creating additional events each time thesuccess
function runs.Without a test case its hard to say what the problem might be. Start with finding out what
table.row(idx).node()
results in. Maybe you should do something more like the solution in your other thread.Kevin
Thanks again for replying.
Did you mean this? I have it in the success function of the ajax:
The scroll to index still works, but no luck with it focusing on the cell
Change
table.on('xhr.dt', function
totable.one('xhr.dt', function
like I show in my example.Did you change anything? It doesn't look like the code in your other thread. You are trying to focus the first row displayed, correct? Use
console.log( table.row(idx).node() );
to find out whattable.row(idx).node()
returns.Please post a link to your page or a test case showing the issue so we can help debug.
Kevin