Scroll to specific row in Datatable API

送分小仙女□ 提交于 2019-12-25 18:26:39

问题


I need to scroll until specific row in my datatable like this:

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

This is my code:

        <script src="js/jquery-1.12.4.js"></script>
        <script src="js/jquery-3.1.1.min.js"></script>
        <script src="js/jquery.dataTables.min.js"></script>
        <script type="text/javascript" src="https://cdn.datatables.net/scroller/1.4.2/js/dataTables.scroller.min.js"></script>
        <script src="js/bootstrap.min.js"></script>
        <script src="js/form-builder.min.js"></script>
        <script src="js/jquery-ui.min.js"></script>
        ....
        <script>
            $(document).ready(function() {
                tableEntityList = $('#accentityListTable').DataTable({      
                    "ordering": false,  
                    "scrollY":"120px",
                    "scrollCollapse": true,
                    "paging":false, 
                    "dom":'<<"top"i>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");
                    }           

                });
    })
</script>

The first alert appears:

alert("first");

But the second one, doesnt:

alert("second");

What i am doing wrong?

Thanks.


回答1:


I think instead of "scrollY":"120px", it should be "scrollY":"120". try without px.




回答2:


I think the problem is probably with your use of 'this'. In this case, I think it actually references the callback function, and not the DataTable. I'm not sure if you could pass it as a parameter to the callback function somehow, but if you create a reference to the DataTable outside the callback function's scope, you can access it by that variable.




回答3:


Kindly replace with the below initComplete function. It will work.

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

Note: 2 represents n-1 targetted row



来源:https://stackoverflow.com/questions/42730920/scroll-to-specific-row-in-datatable-api

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