user544079
user544079

Reputation: 16629

Scroll to selected row in DataTables jQuery plugin

I have a dataTable with infinite scrolling. I want to scroll to a selected row on table refresh

$('#table1').dataTable({
    'aaData': data,
    'aoColumns': columns,
    'bInfiniteScroll': true,
    'bColumnCollapse': true,
    'sScrollY': '200px'
});

$('#btnScroll').click(function(){
     $('.dataTables_scrollBody').scrollTo($('#table1 tbody tr').eq(3), 800);
});

But it does not scroll to the row

Upvotes: 0

Views: 7472

Answers (2)

hrabinowitz
hrabinowitz

Reputation: 1650

You are using the scrollTo plugin. Have you loaded it? You can write this without that plugin as follows:

var selectedRow = $('#table1 tbody tr').eq(3);
$('.dataTables_scrollBody').scrollTop(selectedRow.prop('offsetTop') - $('.dataTables_scrollBody').height()/2);

Upvotes: 2

Mindaugas Večkys
Mindaugas Večkys

Reputation: 423

You can use animate to scroll to your position

$('.dataTables_scrollBody').animate({
    scrollTop: $('#table1 tbody tr').eq(3).offset().top
}, 800)

DEMO

Upvotes: 2

Related Questions