简体   繁体   English

数据表-仅对某些列进行水平滚动

[英]Datatables - horizontal scrolling for only certain columns

The scrollX option in Datatables allows the user to scroll the grid horizontally - however, in my table, I am needing to keep the first two columns in place, but allow the next x columns scroll horizontally. Datatables中的scrollX选项允许用户水平滚动网格-但是,在我的表中,我需要将前两列保持在适当的位置,但允许接下来的x列水平滚动。 I'm not seeing any good examples of how to do this. 我没有看到任何有关如何执行此操作的好例子。

Example: https://datatables.net/examples/basic_init/scroll_x.html 示例: https//datatables.net/examples/basic_init/scroll_x.html

You could achieve this by changing the relative position of each cell based on the scrollLeft value. 您可以通过基于scrollLeft值更改每个单元格的相对位置来实现。

$('.dataTables_scrollBody').scroll(function (){
    var cols = 2 // how many columns should be fixed
    var container = $(this)
    var offset = container.scrollLeft()
    container.add(container.prev()).find('tr').each(function (index,row){ // .add(container.prev()) to include the header
        $(row).find('td, th').each(function (index,cell){
            if(index>=cols) return
            $(cell).css({position:'relative',left:offset+'px'})
        })
    })
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM