fnReloadAjax() avoid reseting scroll bar

fnReloadAjax() avoid reseting scroll bar

wmacgyverwmacgyver Posts: 1Questions: 0Answers: 0
edited June 2011 in General
I have a table with about 50 rows of data. I'm using sAjaxSource to pull it from
server, though i'm not doing serverside processing. I also use the scrollbar feature.

I need to refresh the table every 5 secs. so I setup setInterval to run
fnReloadAjax().

the problem is, whenever the fnReloadAjax fires, the scrollbar gets reset to the beginning.
I think it's because fnReloadAjax destory the entire table and rebuild it. So the user loses his
place that he's scrolling down to.

Any suggestion how I can avoid the scroll bar being reset?

Thank you

Replies

  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    You need to modify the fnReloadAjax function to save the scrolling (scrollTop) position before it redraws the table and then reapply that just after the draw.

    Allan
  • wmacgyverwmacgyver Posts: 1Questions: 0Answers: 0
    that works great. though I did end up hardcode the .scrollTop() by doing var scrollPos=$(".dataTables_scrollBody").scrollTop();

    and set it the the same way after the redraw complete.

    $(".dataTables_scrollBody").scrollTop(scrollPos);

    not the most elegant. but it does work.

    Thanks!
  • allanallan Posts: 63,535Questions: 1Answers: 10,475 Site admin
    Looks good to me - new functionality in two lines of code :-). Nice one and thanks for sharing your solution with us.

    Allan
  • sethgsethg Posts: 1Questions: 0Answers: 0
    While wmacgyvers approach worked for me, the table would visably jump when setting the scroll position.

    I had more luck modifying the call to fnDraw() in the fnReloadAjax plugin to be fnDraw(false). This prevents resetting sort/search/etc and reloads the table content smoothly.
This discussion has been closed.