Scroll to selected row in DataTables jQuery plugin

て烟熏妆下的殇ゞ 提交于 2019-12-13 03:48:50

问题


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


回答1:


You can use animate to scroll to your position

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

DEMO




回答2:


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);


来源:https://stackoverflow.com/questions/24731204/scroll-to-selected-row-in-datatables-jquery-plugin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!