滚动到Datatable API中的特定行

我需要滚动到我的数据表中的特定行,如下所示:

https://datatables.net/extensions/scroller/examples/initialisation/api_scrolling.html

这是我的代码:

       ....  $(document).ready(function() { tableEntityList = $('#accentityListTable').DataTable({ "ordering": false, "scrollY":"120px", "scrollCollapse": true, "paging":false, "dom":'<ft>', "deferRender":true, "scroller":true, "columnDefs": [ {"targets": [ 0 ],"visible": false} ], "createdRow": function( row, data, dataIndex ) { if ( data[ 3 ] == "Inp" ) $(row).css('color', 'green') else $(row).css('color', 'red') if ( data[7] > 0 ) $(row).css('font-weight', 'bold') }, "initComplete": function () { alert("first"); this.api().row( 2 ).scrollTo(); alert("second"); } }); })  

出现第一个警报:

 alert("first"); 

但第二个,不是:

 alert("second"); 

我做错了什么?

谢谢。

我认为不是“scrollY”:“120px”,它应该是“scrollY”:“120”。 尝试没有px。

我认为问题可能在于您使用’this’。 在这种情况下,我认为它实际上引用了回调函数,而不是DataTable。 我不确定你是否可以以某种方式将它作为参数传递给回调函数,但如果在回调函数的作用域之外创建对DataTable的引用,则可以通过该变量访问它。

请使用以下initComplete函数替换。 它会工作。

 "initComplete": function () { $('#example').DataTable().row(2).scrollTo(); } 

注意:2代表n-1目标行