【问题标题】:Problem with the responsive of Datatables, size of column it's to bigger to the normal数据表的响应问题,列的大小比正常大
【发布时间】:2020-01-14 01:09:24
【问题描述】:

我正在使用数据表,并且遇到了数据表对列大小的响应性问题。 当数据表被定义为列的大小时,即更大并且表的最后一列不显示。 所以我尝试添加一个带有max-width 的 CSS 类,但它不起作用。

HTML:

<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.js"></script>
<script src="https://cdn.datatables.net/responsive/2.2.3/js/dataTables.responsive.min.js"></script>

<table class = "display" id = "datatable"></table>

CSS:

.cell_table {
  max-width: 250px;
}

JS:

$('#datatable').DataTable({
    "sPaginationType" : "full_numbers",
    "columnDefs" : [{
        "targets": "_all",
        "createdCell" : function (td, cellData, rowData, row, col) {
            $(td).attr('id', 'cell-' + col);
        },
    }, {
        "targets" : [0, 1, 2, 3],
        "className" : "text_center cell_table",
    }],
    "lengthMenu" : [[5, 10, 20, 30, -1], [5, 10, 20, 30, "All"]],
    "iDisplayLength" : -1,
    data : data_use,
    columns : column_name,
    dom : 'lfrtip',
    responsive : true,
    destroy : true,
    searching: true,
});

Here你可以在JSfiddle上找到代码。

【问题讨论】:

  • 尝试重现问题here
  • @wazz 问题只在 Safari 和 Mozilla Firfox 上(抱歉我忘了提),here你可以在 JSfiddle 上找到代码。
  • 我也在 Firefox 上看到了这个问题——数据表的 Javascript 并不总是正确设置列大小。设置 CSS 毫无价值,因为它总是会被 Javascript 设置的“样式”值覆盖。
  • 我没有看到分配给单元格的类名!也不能在 chrome 上工作
  • 我认为这是 Datatables 的一个错误,应该处理它们。

标签: javascript css datatables


【解决方案1】:

你做错了,两次设置 className 显然不正确。

Fiddle

$('#datatable').DataTable({
            "sPaginationType" : "full_numbers",
            "columnDefs" : [{
                "targets": "_all",
                "createdCell" : function (td, cellData, rowData, row, col) {
                    $(td).attr('id', 'cell-' + col);
                },
            }, {
                "targets" : [0, 1, 2, 3],
                "className" : "text_center cell_table",
            }],
            "lengthMenu" : [[5, 10, 25, -1], [5, 10, 25, "All"]],
            "iDisplayLength" : 25,
            data : data_use,
            columns : column_name,
            dom : 'lfrtip',
            responsive : true,
            destroy : true,
            searching: true,
});

CSS:

.text_center{ 
    text-align : center;
}

.cell_table {
    max-width : 250px;
}

.dataTable{
  width:100%;
  max-width:100%;
}

【讨论】:

  • 这并不能解决问题。 Datatable 仍然在一个小得多的窗口中为&lt;table&gt; 元素设置style="width: 1044px;"
  • @Martin 我们在这里讨论的是单个单元格,而不是整个表格。
  • @SebCollard 我看到了 Martin 现在提到的案例,但仅供参考也发生在 chrome 上,所以让我检查一下。
  • 这里:ibb.co/kxXGBDwibb.co/TmMBPxv(之前和之后)。注意水平滚动条
  • @Martin 你是怎么得到滚动条的?
猜你喜欢
  • 2023-04-03
  • 1970-01-01
  • 2016-06-13
  • 1970-01-01
  • 2010-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多