【问题标题】:Datatables column width increases automatically when paginated分页时数据表列宽自动增加
【发布时间】:2019-11-11 07:20:07
【问题描述】:

我的页面上有三个数据表,带有分页选项。表上的数据通过ajax 加载。在对桌子进行任何操作之前,一切都是完美的。如果我对表格进行分页,那么表格的大小会自动增加。这是我的代码:

$.ajax({
        type: 'GET',
        url: '/get/data',
        success: function (resp) {

            let str = '';
            for (let j = 1; j < resp.length; j++) {
                str += `<tr>
                                <td>${moment(resp[j].purchase_date).format('MM-DD-YYYY')}</td>
                                <td>${resp[j].items}</td>
                            </tr>\n`;
            }
            $("#table1 tbody").html(str);
            initDataTable();
        }
    });


    function initDataTable() {
            $('#table1').DataTable({
                "scrollY": 250,
                "scrollX": false,
                "pagingType": "full",
                "autoWidth": false,
                "pageLength": 10,
                bFilter: false,
                bInfo: false,
                aoColumnDefs: [
                    {
                        bSortable: false,
                        aTargets: [-1]
                    }
                ]
            });
        }

这是数据表的 CSS。

#table1, #table2, #table3 {
    table-layout: fixed;
    width: 100% !important;
}

.dataTables_wrapper {
    background: #fff;
    border-radius: 8px;
    padding:0 30px 0px;
    overflow: hidden;
}

.dataTables_wrapper table.dataTable thead .sorting {
    background: url(../img/sort_both.png) no-repeat center right!important
}

.dataTables_wrapper table.dataTable thead .sorting_asc {
    background-image: url(../img/sort_asc.png)!important;
    background-repeat: no-repeat!important
}

.dataTables_wrapper table.dataTable thead .sorting_desc {
    background-image: url(../img/sort_desc.png)!important;
    background-repeat: no-repeat!important
}

【问题讨论】:

    标签: jquery datatables


    【解决方案1】:

    默认情况下,Datatable 通过documentation 中给出的默认sWidth 属性将智能宽度应用于所有列。您可以在 aoColumnDefsaoColumnDefs 属性中为特定列修改它。

    【讨论】:

    • 这对我不起作用。我尝试了aoColumnDefsaoColumns。但问题依然存在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-02
    • 1970-01-01
    • 1970-01-01
    • 2016-02-20
    • 2017-05-04
    • 1970-01-01
    • 2019-02-03
    相关资源
    最近更新 更多