How to scroll to bottom of 'scroller' table

How to scroll to bottom of 'scroller' table

sronsieksronsiek Posts: 52Questions: 11Answers: 0

Hi,

I'm displaying a log file in a DataTable with the scroller plugin, data src is a websocket. The bulk of the logs are initially received, after which the ws remains open such that additional log messages are appended. Much like tail -f in a terminal on *nix.

Here are the options in use:

    logTableDef: {
      paging: true,   // needs to be true for deferRender!
      searching: true,
      ordering: false,  // Rely on order of data as received
      deferRender: true,
      scrollY: "600px",
      // scrollCollapse: true,
      scroller: true,

The described mechanism works, but for logs it is desirable to see the most recent messages at the bottom of the table (this is what users will be used to), with new messages inserted at the bottom & popping into view at the bottom as they arrive.

To achieve this I want to set scroll to the last row. I've noted that use of row( 100 ).scrollTo() does not seem to work when the scroller plugin is in use, and I see no options in the scroller documentation for this. Resorting to jquery I tried the following code after each call to draw():

      var scrollBody = $(table.get(0)).parent()
      scrollBody.scrollTop(scrollBody.get(0).scrollHeight);

This seems to work the first time, though I'm not sure if thi plays well with scroller. When a new message is updated, and the above code is executed, I see blank space where the bottom of the table should be. I can scroll up manually - all the data is there as expected.

What is the correct way to scroll to the bottom (or even tell scroller to initially set the scroll to bottom instead of top) when using the scroller plugin?

Thanks for any suggestions!

Stefan

Answers

  • sronsieksronsiek Posts: 52Questions: 11Answers: 0

    Update:

    I found one way of doing this with unpleasant side effects:

    table.api().draw()
    var num_rows = table.api().page.info().recordsTotal
    table.api().settings().scroller().scrollToRow( num_rows )
    

    This does work, but on every update the draw() initially re-displays the top of the table after which there is a gradual (well - somewhat jerky) slide to the bottom. Seems like I need to draw the last page ...

  • bindridbindrid Posts: 730Questions: 0Answers: 119

    can you just use the sorting features to keep latest stuff at the top

  • sronsieksronsiek Posts: 52Questions: 11Answers: 0

    Sorting is disabled for performance - the logs can grow to many thousands of lines.
    I do not want to alter the ordering of data.

    The issue seems to be:

    draw( true ) - re-inits the scrollbar to the top
    draw( false ) - does not move the scrollbar to the top, but also does not insert the row data (an empty row is displayed) until the user triggers something by manual scrolling.

  • sronsieksronsiek Posts: 52Questions: 11Answers: 0

    Finally got it to work like this:

    var num_rows = table.api().page.info().recordsTotal
    table.api().page( 'last' ).draw( false );
    table.api().row( num_rows-1 ).scrollTo()
    
This discussion has been closed.