【问题标题】:How to solve DataTables table header columns width and table body columns width mismatch sometimes?有时如何解决 DataTables 表头列宽和表体列宽不匹配?
【发布时间】:2018-05-05 06:08:14
【问题描述】:

使用以下 .js 文件。

  • jquery.dataTables.min.js (DataTables 1.10.16)。
  • dataTables.bootstrap.min.js(DataTables Bootstrap 3 集成)。
  • dataTables.responsive.min.js(响应式 2.2.1)。
  • dataTables.scroller.min.js (Scroller 1.4.4)。

使用以下 .css 文件。

  • jquery.dataTables.min.css
  • dataTables.bootstrap.min.css
  • responsive.dataTables.min.css
  • scroller.dataTables.min.css

这是表格布局:

<table class="table table-responsive table-bordered table-striped table-hover" id="tbl-cat-lvl-two" style="width:100%">
    <thead>
        <tr>
            <th>ID</th>
            <th>Code</th>
            <th>Icon</th>
            <th>Category Label</th>
            <th>Precedence</th>
            <th>Visibility</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>

这是表格脚本:

catLvl2table = $('#tbl-cat-lvl-two').DataTable( {
    "processing"    : true,
    "serverSide"    : true,
    "order"         : [[ 4, "asc" ]],
    "responsive"    : true,
    "scrollY"       : "236px",
    "scrollCollapse": true,
    "ajax"          : {
        "url"       : baseUrl+getCatLvl2DataUrl+lvl1CatId,
        "type"      : "POST"
    },
    "columnDefs"    : [
        { "visible"     : false, "targets": [0] },
        { "orderable"   : false, "targets": [0, 2, 5, 6] },
        { 
            className: "align-center check-align-center",
            "targets": [6],
            "render"    : function (data, type, full, meta){
                let id = full.id;
                return `<button href="" class="btn btn-sm btn-info dt-btn-view" data-id="${id}"><i class="fa fa-eye"></i></button><button href="" class="btn btn-sm btn-primary dt-btn-edit" data-id="${id}"><i class="glyphicon glyphicon-pencil"></i></button><button href="" class="btn btn-sm btn-danger dt-btn-del" data-id="${id}"><i class="glyphicon glyphicon-trash"></i></button>`;
            } 
        }
    ],
    "columns": [
        { "data": "id" },
        { "data": "code" },
        { "data": "icon" },
        { "data": "category" },
        { "data": "precedence" },
        { "data": "visibility" },
    ],
});

这是输出:

【问题讨论】:

  • 拜托,有人帮我解决这个问题..
  • 没有人?? :/ :(((

标签: javascript jquery datatables datatables-1.10


【解决方案1】:
<table class="table table-responsive table-bordered table-striped table-hover" id="tbl-cat-lvl-two" style="width:100%">
    <thead>
        <tr>
            <!-- <th>ID</th> --> //Remove completely this column
            <th>Code</th>
            <th>Icon</th>
            <th>Category Label</th>
            <th>Precedence</th>
            <th>Visibility</th>
            <th>Actions</th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>

只需从“columnDefs”数组中删除以下代码。

{ 
    className: "align-center check-align-center",
    "targets": [6],
    "render"    : function (data, type, full, meta){
        let id = full.id;
        return `<button href="" class="btn btn-sm btn-info dt-btn-view" data-id="${id}"><i class="fa fa-eye"></i></button><button href="" class="btn btn-sm btn-primary dt-btn-edit" data-id="${id}"><i class="glyphicon glyphicon-pencil"></i></button><button href="" class="btn btn-sm btn-danger dt-btn-del" data-id="${id}"><i class="glyphicon glyphicon-trash"></i></button>`;
    } 
}

并添加以下代码:

"columns": [
        { "data": "code" },
        { "data": "icon" },
        { "data": "category" },
        { "data": "precedence" },
        { "data": "visibility" },
        { "data" : null,
            className : "align-center" // You should style for this class
        }
],
"rowCallback": function(row, data, index) {
    $('td:eq(5)', row).html(
        '<button href="" class="btn btn-sm btn-info dt-btn-view" data-id="'+data.id+'"><i class="fa fa-eye"></i></button>'+
        '<button href="" class="btn btn-sm btn-primary dt-btn-edit" data-id="'+data.id+'"><i class="glyphicon glyphicon-pencil"></i></button>'+
        '<button href="" class="btn btn-sm btn-danger dt-btn-del" data-id="'+data.id+'"><i class="glyphicon glyphicon-trash"></i></button>'
    );
},

在外部 css 类中

.align-center {
  text-align: center; // whatever you want
}

【讨论】:

  • 谢谢..我会尝试这种方式。
  • 嘿,我按照你的建议试过了。宽度不匹配可以通过它来解决,但是现在,在 td-6:actions 中传递 [object Object] 而不是按钮。如何解决?请告诉我。
  • 这个 [object object] 可以传递给列问题的数量。
  • 移除完全隐藏的“ID”列(0)。您可以通过 'data.id'(objectname.id) 处理按钮操作。只需添加 'td:eq(5)'。
  • 我删除了 column[0]: Id 并且我没有收到 [object Object] 错误。但是, data-id="${id}" 现在没有通过。 ://
猜你喜欢
  • 1970-01-01
  • 2019-11-11
  • 1970-01-01
  • 2012-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-16
相关资源
最近更新 更多