8

I am having difficulty getting the horizontal scroll to work with dataTables.js. The expected result is a table that allows me to scroll horizontally within the table. The current result is a table that extends outside of the container div. Is there a solution?

HTML:

<div class="box-body table-responsive">
     <table id="portal-drivers" class="table table-bordered table-striped" cellspacing="0" width="100%">
         <thead>
             <tr>
                 <th>First Name</th>
                 <th>Last Name</th>
                 <th>Email</th>
                 <th>Number</th>
                 <th>Rating</th>
                 <th>Transactions</th>
             </tr>
         </thead>
         <tbody>
             <tr>
                 <td>Bugs</td>
                 <td>Bunny</td>
                 <td>[email protected]</td>
                 <td>(310) 530-6789</td>
                 <td>4.8</td>
                 <td>309239045293459823945234895</td>
             </tr>
          </tbody>
     </table>

CSS:

.table-striped > tbody > tr:nth-child(odd) > td,
.table-striped > tbody > tr:nth-child(odd) > th {
  white-space: nowrap;
}
#portal-drivers {
  overflow: auto;
}

jQuery

$("#portal-drivers").dataTable({
    "scrollX": true
});

5 Answers 5

15

Change "scrollX" to "sScrollX": '100%'

$("#portal-drivers").dataTable( {
    "sScrollX": '100%'
} );
Sign up to request clarification or add additional context in comments.

2 Comments

whats the difference between scrollx and sscrollx?
I also found adding responsive:false did the trick for me. I am using ASP Net Zero, which also adds a dt-responsive class to the table, which should be removed.
9

In order to make the datatable scrollable (header and body both), use property sScrollXInner along with sScrollX as follows:

$("#my-demo-datable").dataTable({
    "sScrollX": "100%",
    "sScrollXInner": "110%",
});

Setting sScrollXInner to 100% will allow the table to be responsive and show the scroll bar only when the table overflows. At 110%, it will always be overflowing.

1 Comment

for me changing the "sScrollXInner" to "100" worked for responsive show down.
7

Try putting this into CSS:

#portal-drivers {
    overflow-x: scroll;
    max-width: 40%;
    display: block;
    white-space: nowrap;
}

2 Comments

Really working well. I also applied "sScrollX": '100%' in datatable application but width issue occur for header.
Nice, this works well. Thanks
0

I tried this and it worked for me,

$(document).ready(function() {
    $('#example').dataTable( {
        "sScrollX": "100%",
        "sScrollXInner": "110%",
        "bScrollCollapse": true
    } );
} );

To enable x scrolling, you can set the sScrollX parameter your container wrapper's width to be. Also the sScrollXInner is used here to force the table to be wider than is strictly needed.

1 Comment

Re "you can set the sScrollX parameter your container wrapper's width to be": This is partly incomprehensible. Are you using machine translation? Can you fix it?
-1

For making datatable scroll-able ,you can try out this

$(document).ready(function() {
    $('#example').DataTable( {
        *"scrollY": 200,
        "scrollX": true
    } );
} )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.