如何解决 DataTables table header 列宽和 table body 列宽有时不匹配的问题?
How to solve DataTables table header columns width and table body columns width mismatch sometimes?
使用了以下 .js 文件。
- jquery.dataTables.min.js(数据表 1.10.16)。
- dataTables.bootstrap.min.js(DataTables Bootstrap 3 集成)。
- dataTables.responsive.min.js(响应式 2.2.1)。
- dataTables.scroller.min.js(滚动条 1.4.4)。
使用了以下 .css 个文件。
- jquery.dataTables.min.css
- dataTables.bootstrap.min.css
- responsive.dataTables.min.css
- scroller.dataTables.min.css
这是 table 布局:
<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>
这是 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" },
],
});
这是输出:
<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>'
);
},
在外部cssclass
.align-center {
text-align: center; // whatever you want
}
使用了以下 .js 文件。
- jquery.dataTables.min.js(数据表 1.10.16)。
- dataTables.bootstrap.min.js(DataTables Bootstrap 3 集成)。
- dataTables.responsive.min.js(响应式 2.2.1)。
- dataTables.scroller.min.js(滚动条 1.4.4)。
使用了以下 .css 个文件。
- jquery.dataTables.min.css
- dataTables.bootstrap.min.css
- responsive.dataTables.min.css
- scroller.dataTables.min.css
这是 table 布局:
<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>
这是 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" },
],
});
这是输出:
<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>'
);
},
在外部cssclass
.align-center {
text-align: center; // whatever you want
}