Vertical Scroll settings effecting horizontal scrolling with Datatables

荒凉一梦 提交于 2021-02-08 06:41:21

问题


I am trying to create a datatable that has vertical but not horizontal scrolling. The create table statement I am using here as follows:

$(document).ready( function () {
    $('#order-attention-table').DataTable( {
        "bFilter": false,
        "scrollY": 300,
        "scrollX": false,
        "paging":   false,
        "ordering": false,
        "info":     false,
    });
} );

Currently my columns are wider than the screen and this causes a horizontal scroll bar to appear even with "scrollX": false. The only way I can get the horizontal scrollbar to not appear is by removing the "scrollY": 300. When I remove the verticle scrolling property the horizontal scroll bar goes away.

So my question is two parts.

  1. How to I force the columns to fit the screen?
  2. How to I stop the horizontal scroll bar while still allowing vertical scrolling?

回答1:


SOLUTION #1

You can use Responsive extension if data in your columns don't fit into the page/container.

var table = $('#example').DataTable({
   "searching": false,
   "scrollY": 300,
   "paging":   false,
   "ordering": false,
   "info":     false,        
   "responsive": true
});

DEMO

See this jsFiddle for code and demonstration.

SOLUTION #2

You can disable automatic width calculation autoWidth:false and set minimal width with columnDefs.width as shown below.

var table = $('#example').DataTable({
   "searching": false,
   "paging":   false,
   "ordering": false,
   "info":     false,
   "autoWidth": false,
   "columnDefs": [
       { "targets": "_all", "width": "1%" }
   ]        
});

Also you can add compact class to the table to reduce table width.

<table id="example" class="display compact" cellspacing="0" width="100%">

DEMO

See this jsFiddle for code and demonstration.




回答2:


Just had this issue and was pulling my hair out to find a solution, turns out removing the right and left borders on my TABLE fixed it. The tiny 1 px on each side were causing the scrollbar to appear.

<table id="example" class="display sortable" cellspacing="0" width="100%">
.sortable {
    border-left:none;
    border-right:none;
}


来源:https://stackoverflow.com/questions/32847344/vertical-scroll-settings-effecting-horizontal-scrolling-with-datatables

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