jquery 数据表响应宽度大于父级宽度

wf82jlnq  于 2023-06-29  发布在  jQuery
关注(0)|答案(2)|浏览(119)

即使在jquery datatables中使用responsive option,在较低的分辨率下,计算出的表的宽度也会大于父div,所以会发生这种情况:

表的定义如下:

<table id="datatableGroupsList" class="table table-striped table-bordered responsive no-wrap table-hover partialViewPanel" cellspacing="0" width="100%">

datatables脚本的调用方式如下:

/* Datatables responsive */
var dataTableGroupList = function () {
    var table = $('#datatableGroupsList').DataTable({
        responsive: true,
        "columns": [null, null, null, { "orderable": false } //Desabilitar ordenação na coluna das acções
        ],
        language: {
            url: window.location.origin + '/Home/GetLocalizationForDataTable'
        },
        paging: true
    }).on("init", function () {
        var tt = new $.fn.dataTable.TableTools(table,
            {
                "aButtons": [
                    {
                        "sExtends": "copy",
                        "sButtonText": "@Resources.ResourcesGeneral.Copy",
                        "mColumns": [0, 1, 2]
                    },
                    {
                        "sExtends": "xls",
                        "mColumns": [0, 1, 2]
                    },
                    {
                        "sExtends": "pdf",
                        "mColumns": [0, 1, 2]
                    },
                    {
                        "sExtends": "print",
                        "sButtonText": "@Resources.ResourcesGeneral.Print",
                        "mColumns": [0, 1, 2]
                    }
                ]
            }
            );
        $(tt.fnContainer()).insertBefore('#datatableGroupsList_wrapper div.dataTables_filter');
        $('.DTTT_container').addClass('btn-group');
        $('.DTTT_container a').addClass('btn btn-default btn-md');
        $('.dataTables_filter input').attr("placeholder", "@(Resources.ResourcesGeneral.Search)..."); 

    })
    ;
}
$(document).ready(function () {
         dataTableGroupList();   
});

顺便说一句,这个问题和我问的差不多:jQuery DataTables:另外,我使用的是datatable.js的1.10.2版本
有没有人遇到过这个问题,或者知道解决办法?谢谢

56lgkhnf

56lgkhnf2#

以下是对我有效的方法:
1.定义一个新的类,我称我的类为“wrappable”。将这个类添加到datatable列类中,使它在单元格中换行,这样它就不会溢出到右边的其他单元格中。
table.dataTable tbody td.wrappable { white-space:normal; }
1.将类应用于dataTable中的列
var columns = [ { data: null, title: " Request Details ", className: "all wrappable", width: '200px', sortable: true, autoWidth: false, render: function (data, type, row) { var year = '<div>Fiscal Year: ' + naIfNullOrEmpty(data.FISCAL_YEAR) + '</div>'; var cycle = '<div>Cycle: ' + naIfNullOrEmpty(data.CdSnfCycle.NAME) + '</div>'; var notes = '<br />Notes: ' + naIfNullOrEmpty(data.NOTES) + ' '; return year + cycle + notes; } }, ]
希望这能帮上忙。

相关问题