0

I have set of data that should display in JQuery Datatables with options to filter, and scroll without losing column headers. All features work so far but there is problem with the column header. If you click on the link and check run example you will see the problem. The column header is on the left while tbody is centered. I have tried fixing this problem and changing my HTML code, removing some of the JQuery table attributes but still no success. Please if anyone knows how to fix this problem or if there is any problem with my code please elt me know. Here is example:

$('#btnGet').on('click', function() { buildTable(); }); var dataTable; function GenerateTable(tblID) { // Setup - add a text input to each footer cell $('#rptTbl thead tr').clone(true).appendTo('#rptTbl thead'); $('#rptTbl thead tr:eq(1) th').each(function(i) { //var title = $(this).text(); $(this).html('<input type="text" placeholder="Search" />'); $('input', this).on('keyup change', function() { if (dataTable.column(i).search() !== this.value) { dataTable.column(i).search(this.value).draw(); } }); }); dataTable = $('#' + tblID).DataTable({ dom: 'Bfrtip', buttons: [ 'copy', 'csv', 'excel', 'print', { extend: 'pdfHtml5', orientation: 'landscape', pageSize: 'LEGAL' } ], orderCellsTop: true, fixedHeader: { header: true, footer: true }, scrollX: true, scrollY: "400px", scrollCollapse: true, iDisplayLength: 25, language: { "emptyTable": "No records were found." } }); } function buildTable() { var htmlTbl = '<table id="rptTbl" class="table table-bordered"><thead><tr><th>Area</th></tr></thead><tbody><tr><td>Central</td></tr><tr><td>Eastern</td></tr><tr><td>Central</td></tr><tr><td>Central</td></tr><tr><td>Central</td></tr><tr><td>Central</td></tr><tr><td>Eastern</td></tr><tr><td>Central</td></tr><tr><td>Central</td></tr><tr><td>Central</td></tr><tr><td>Central</td></tr></tbody></table>'; $('#rptData').empty().append(htmlTbl); GenerateTable('rptTbl'); }
<!DOCTYPE html> <html> <head> <title>Test JQuery Datatables</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <!-- *** Start: CSS for DataTables. *** --> <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.16/css/dataTables.jqueryui.min.css" /> <link rel="stylesheet" type="text/css" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" /> <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/buttons/1.4.2/css/buttons.dataTables.min.css" /> <!-- *** End: CSS for DataTables. *** --> <script language="javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script language="javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <!-- *** Start: JS for DataTables. *** --> <script type="text/javascript" src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script> <script type="text/javascript" src="https://cdn.datatables.net/1.10.16/js/dataTables.jqueryui.min.js"></script> <script type="text/javascript" src="https://cdn.datatables.net/buttons/1.4.2/js/dataTables.buttons.min.js"></script> <script type="text/javascript" src="https://cdn.datatables.net/buttons/1.4.2/js/buttons.flash.min.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.32/pdfmake.min.js"></script> <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.32/vfs_fonts.js"></script> <script type="text/javascript" src="https://cdn.datatables.net/buttons/1.4.2/js/buttons.html5.min.js"></script> <script type="text/javascript" src="https://cdn.datatables.net/buttons/1.4.2/js/buttons.print.min.js"></script> <!-- *** End: JS for DataTables. *** --> </head> <body> <div class="container"> <div id="report_filters" class="collapse in"> <div class="row"> <div class="form-group col-xs-10 col-sm-5 col-md-3 col-lg-3"> <button type="button" id="btnGet" class="btn btn-primary btn-block" data-toggle="collapse" data-target="#report_data,#report_filters">Get Report</button> </div> </div> </div> <div id="report_data" class="collapse"> <div class="row"> <div class="form-group col-xs-12 col-sm-12 col-md-12 col-lg-12"> <button type="button" class="btn btn-primary" data-toggle="collapse" data-target="#report_data,#report_filters"> <span class="glyphicon glyphicon-circle-arrow-left"></span> Go Back </button> </div> </div> <div id="rptData" class="table-responsive"></div> </div> </div> </body> </html>

5
  • I have investigated your problem, these option are causing your issues: scrollX: true, scrollY: "400px", scrollCollapse: true. If you comment they all you will see your table centered. However, i don't found a nice solution to solve the problems these option introduces. Commented Nov 5, 2018 at 18:44
  • @D.Smania I was able to detect the same thing but no solution if they are included. Something is off in datatables css. Commented Nov 5, 2018 at 18:46
  • Just a warning, if you are integrating Datatables with Bootstrap, then you should include all the Bootstrap support, not the JQuery UI one, maybe that is the source of issues. Read: Bootstrap Integration. The same is valid for the buttons extension. Commented Nov 5, 2018 at 22:01
  • @D.Smania Can you specify what exactly is off with the libraries that I included? Do I have some that are not necessary or there are some missing? Commented Nov 6, 2018 at 11:59
  • I'm note really sure if what you do is wrong or not, but for Bootstrap, the Datatables plugin have his set of customs CSS styles in replacement of, for example: <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.16/css/dataTables.jqueryui.min.css" /> that is the JQuery UI one style. Since you are using Bootstrap I think this is more coherent to use: <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.16/css/dataTables.bootstrap.min.css" />, nothing more. This same will apply for the replacement of the other sources related to JQueryUI. Commented Nov 6, 2018 at 15:18

1 Answer 1

2

Add a drawCallback function in dataTable options

 dataTable = $('#' + tblID).DataTable({ dom: 'Bfrtip', buttons: [ 'copy', 'csv', 'excel', 'print', { extend: 'pdfHtml5', orientation: 'landscape', pageSize: 'LEGAL' } ], orderCellsTop: true, fixedHeader: { header: true, footer: true }, scrollX: true, scrollY: "400px", scrollCollapse: true, iDisplayLength: 25, drawCallback: function( settings ) { // Reset margin to 0 after datatable render var ele = document.querySelector('.dataTables_scrollBody'); if(ele){ ele = ele.querySelector('.dataTable'); if(ele){ ele.style.margin = 0; } } }, language: { "emptyTable": "No records were found." } }); 
Sign up to request clarification or add additional context in comments.

3 Comments

That fixed the problem. I notice that table is aligned to the left. In my example tbody was centered and thead was on the left. Is there a way to center the table? Also can you please elaborate why this problem happened and what exactly margin did to fix the issue?
Don't know but somehow jquery ui css has mess up with datatable default css.
Fair enough. Thank you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.