Search code examples
jquerytablesorter

JQuery TableSorter 2.25 (Mottie fork) w/ Scroller: Programmatically scroll to row?


I've been using the Mottie fork of the JQuery TableSorter plugin for a while now, and it's pretty phenomenal. I'm using the "scroller" widget to make the table scrollable vertically as well as horizontally while keeping some fixed columns on the left.

Here's the question: Is there a way to programmatically scroll to a particular row? e.g. "myTable.scrollToRow(40)"? A pixel offset would be perfectly acceptable too.


Solution

  • I'm not sure what interface is being used to allow the user to scroll to a particular row, but you can use the following code to set the scroll (demo):

    HTML (used in demo)

    Scroll to row: <button type="button">20</button> <button type="button">40</button>
    

    Script

    $('button').click(function(){
        var $scroller = $('.tablesorter-scroller-table'),
        row = parseInt( $(this).text(), 10 ),
        position = $table.find('tbody tr').eq(row).position();
      $scroller.scrollTop( $scroller.scrollTop() + position.top );
    });