首页 > 解决方案 > 如何修复 DataTables autoWidth 不起作用

问题描述

我尝试使用自动更改表格宽度,但未修复 DataTables。当我使用手机像素尺寸时会发生这种情况。它不会发生在笔记本电脑/台式机像素上。是否有一些缺失的代码?

下图显示数据超出了 DataTables。

在此处输入图像描述

HTML

<div class="col-lg-12 col-md-12 col-sm-12 mb-30">
   <table id="example" class="display compact nowrap" style="width:100%">
      <thead>
        <tr>
          <th class="table-plus datatable-nosort">Num</th>
          <th>Region</th>
              .....
        </tr>
      </thead>
      <tbody>
        <tr>
            <td class="table-plus">1</td>
            <td>NORTHERN</td>
               .....
        </tr>
      </tbody>
   </table>
</div>

编辑: CSS

https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.css
https://cdn.datatables.net/1.10.19/css/dataTables.bootstrap4.min.css
https://cdn.datatables.net/responsive/2.2.3/css/responsive.bootstrap4.min.css

JS

$(document).ready(function() {
    $('#example').DataTable( {
        scrollCollapse: true,
        autoWidth: true,
        responsive: true,
        columnDefs: [{
            targets: "datatable-nosort",
            orderable: false,
        }],
        "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
        "language": {
        "info": "_START_-_END_ of _TOTAL_ entries",
        searchPlaceholder: "Search"
        },
    });
});  

标签: javascriptjqueryhtmldatatabledatatables

解决方案


推荐阅读