跟随所选行自动滚动数据表jquery

问题描述 投票:0回答:1

这是我的数据表:

enter image description here

如果我按向下箭头,滚动条不会跟随表格的焦点,因此我必须手动移动滚动条才能看到它。我怎样才能自动执行此操作?

enter image description here

$(document).keydown(function(e) {
  if (e.keyCode == 40) { //arrow down
    if (tr.next().length) {
      table.$('tr.selected').removeClass('selected');
      tr.next().addClass('selected');
      tr = table.$('tr.selected');
    }
  }
  
  if (e.keyCode == 38) { // arrow up
    if (tr.prev().length) {
      table.$('tr.selected').removeClass('selected');
      tr.prev().addClass('selected');
      tr = table.$('tr.selected');
    }
  }
})
javascript jquery datatable
1个回答
0
投票

您可以在代码中添加滚动逻辑来实现这一点。

 function scrollToRow(row) { 
        // Check if the row is not visible
        if (row.position().top < 0 || row.position().top > row.parent().height()) {
            // Scroll smoothly to the new row
            $(Your_dropdown_table).animate({ 
                scrollTop: row.position().top
            }, 100);
        }
    }
希望可以帮到你。

© www.soinside.com 2019 - 2024. All rights reserved.