如何将 <th> Header 与 Table 中的 <td> 对齐?
How to align <th> Header with <td> in Table?
在 DataTable 中启用滚动条 (ScrollY) 后,无法将 header 与 table 数据对齐。
我尝试了很多在线提供的解决方案,但似乎没有用。
我尝试了 display:block 但没有成功。
检查 jsfiddle:https://jsfiddle.net/VM001/dzukjbh3/
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.21/css/jquery.dataTables.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.21/css/dataTables.bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.5.1.js"> </script>
<script src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.21/js/dataTables.bootstrap.min.js"></script>
Datatable:
$(document).ready(function() {
$("#TableName").Datatable( {
language: { "search": "Filter"},
paging: false,
info: true,
scrollY:'200px',
scrollCollapse: true,
scrollX: false,
});
});
<table id="TableName" class="table table-hover table-bordered table-striped>
<thead style="display:block">
<tr>
<th>Item1</th>
<th>Item2</th>
<th>Item3</th>
<th>Item4</th>
</tr>
</thead>
<tbody>
{% for item in items_json %}
<tr>
<td>{{ item.a }}</td>
<td>{{ item.b }}</td>
<td>{{ item.c }}</td>
<td>{{ item.d }}</td>
</tr>
</tbody>
</table>
我必须删除您的一些 css 规则,但它似乎对我有用:
$(document).ready(function() {
$("#P1SxTable").DataTable( {
language: { "search": "Filter"},
paging: false,
info: true,
scrollY:'50px',
scrollCollapse: true,
scrollX: false,
});
});
/*table {
width: 100%;
table-layout: fixed;
}*/
th {
font-size: 15px;
font-weight: bolder;
text-align: center;
}
td {
font-size: 15px;
text-align: center;
}
table.table-bordered {
border: 2px solid black;
}
table.table-bordered > thead > tr > th {
border: 1px solid black;
background-color: lightgrey;
}
table.table-bordered > tbody > tr > td{
border: 1px solid black;
border-top: 1px solid;
}
/*thead {
display: table;
width: 100%;
}
tbody {
display: block;
height: 200px;
}*/
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.21/css/dataTables.bootstrap.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.21/js/dataTables.bootstrap.min.js"></script>
<div class="container-fluid">
<h2>Data</h2>
<table id="P1SxTable" class ="table table-hover table-bordered table-striped" style="width: 100%;">
<!--caption><b>P1Sx YTD data</b></caption-->
<thead>
<tr>
<th>Number</th>
<th>Severity</th>
<th>Description </th>
<th>Node Name</th>
<th>Assignee</th>
<th>Impact minutes</th>
</tr>
</thead>
<tbody>
<tr>
<td>NumberXXXXX</td>
<td>S1 </td>
<td>Description size keeps on varying</td>
<td>Node cisco123</td>
<td>Network Operations</td>
<td>Impact1</td>
</tr>
<tr>
<td>NumberYYYYYY</td>
<td>S3 </td>
<td>Description again is very very very long</td>
<td>Firewall Operations</td>
<td>Assignee2</td>
<td>Impact2</td>
</tr>
<tr>
<td>NumberXXXXX</td>
<td>S1 </td>
<td>Description size keeps on varying</td>
<td>Node cisco123</td>
<td>Network Operations</td>
<td>Impact1</td>
</tr>
<tr>
<td>NumberYYYYYY</td>
<td>S3 </td>
<td>Description again is very very very very very very long</td>
<td>Firewall Operations</td>
<td>Assignee2</td>
<td>Impact2</td>
</tr>
<tr>
<td>NumberXXXXX</td>
<td>S1 </td>
<td>Description size keeps on varying</td>
<td>Node cisco123</td>
<td>Network Operations</td>
<td>Impact1</td>
</tr>
<tr>
<td>NumberYYYYYY</td>
<td>S3 </td>
<td>Description again is very very very long</td>
<td>Firewall Operations</td>
<td>Assignee2</td>
<td>Impact2</td>
</tr>
<tr>
<td>NumberXXXXX</td>
<td>S1 </td>
<td>Description size keeps on varying</td>
<td>Node cisco123</td>
<td>Network Operations</td>
<td>Impact1</td>
</tr>
<tr>
<td>NumberYYYYYY</td>
<td>S3 </td>
<td>Description again is very very very long</td>
<td>Firewall Operations</td>
<td>Assignee2</td>
<td>Impact2</td>
</tr>
<tr>
<td>NumberXXXXX</td>
<td>S1 </td>
<td>Description size keeps on varying</td>
<td>Node cisco123</td>
<td>Network Operations</td>
<td>Impact1</td>
</tr>
<tr>
<td>NumberYYYYYY</td>
<td>S3 </td>
<td>Description again is very very very long</td>
<td>Firewall Operations</td>
<td>Assignee2</td>
<td>Impact2</td>
</tr>
<tr>
<td>NumberXXXXX</td>
<td>S1 </td>
<td>Description size keeps on varying</td>
<td>Node cisco123</td>
<td>Network Operations</td>
<td>Impact1</td>
</tr>
<tr>
<td>NumberYYYYYY</td>
<td>S3 </td>
<td>Description again is very very very long</td>
<td>Firewall Operations</td>
<td>Assignee2</td>
<td>Impact2</td>
</tr>
</tbody>
</table>
</div>
在 DataTable 中启用滚动条 (ScrollY) 后,无法将 header 与 table 数据对齐。 我尝试了很多在线提供的解决方案,但似乎没有用。 我尝试了 display:block 但没有成功。
检查 jsfiddle:https://jsfiddle.net/VM001/dzukjbh3/
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.21/css/jquery.dataTables.min.css">
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.21/css/dataTables.bootstrap.min.css">
<script src="https://code.jquery.com/jquery-3.5.1.js"> </script>
<script src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.21/js/dataTables.bootstrap.min.js"></script>
Datatable:
$(document).ready(function() {
$("#TableName").Datatable( {
language: { "search": "Filter"},
paging: false,
info: true,
scrollY:'200px',
scrollCollapse: true,
scrollX: false,
});
});
<table id="TableName" class="table table-hover table-bordered table-striped>
<thead style="display:block">
<tr>
<th>Item1</th>
<th>Item2</th>
<th>Item3</th>
<th>Item4</th>
</tr>
</thead>
<tbody>
{% for item in items_json %}
<tr>
<td>{{ item.a }}</td>
<td>{{ item.b }}</td>
<td>{{ item.c }}</td>
<td>{{ item.d }}</td>
</tr>
</tbody>
</table>
我必须删除您的一些 css 规则,但它似乎对我有用:
$(document).ready(function() {
$("#P1SxTable").DataTable( {
language: { "search": "Filter"},
paging: false,
info: true,
scrollY:'50px',
scrollCollapse: true,
scrollX: false,
});
});
/*table {
width: 100%;
table-layout: fixed;
}*/
th {
font-size: 15px;
font-weight: bolder;
text-align: center;
}
td {
font-size: 15px;
text-align: center;
}
table.table-bordered {
border: 2px solid black;
}
table.table-bordered > thead > tr > th {
border: 1px solid black;
background-color: lightgrey;
}
table.table-bordered > tbody > tr > td{
border: 1px solid black;
border-top: 1px solid;
}
/*thead {
display: table;
width: 100%;
}
tbody {
display: block;
height: 200px;
}*/
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.21/css/dataTables.bootstrap.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.21/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.21/js/dataTables.bootstrap.min.js"></script>
<div class="container-fluid">
<h2>Data</h2>
<table id="P1SxTable" class ="table table-hover table-bordered table-striped" style="width: 100%;">
<!--caption><b>P1Sx YTD data</b></caption-->
<thead>
<tr>
<th>Number</th>
<th>Severity</th>
<th>Description </th>
<th>Node Name</th>
<th>Assignee</th>
<th>Impact minutes</th>
</tr>
</thead>
<tbody>
<tr>
<td>NumberXXXXX</td>
<td>S1 </td>
<td>Description size keeps on varying</td>
<td>Node cisco123</td>
<td>Network Operations</td>
<td>Impact1</td>
</tr>
<tr>
<td>NumberYYYYYY</td>
<td>S3 </td>
<td>Description again is very very very long</td>
<td>Firewall Operations</td>
<td>Assignee2</td>
<td>Impact2</td>
</tr>
<tr>
<td>NumberXXXXX</td>
<td>S1 </td>
<td>Description size keeps on varying</td>
<td>Node cisco123</td>
<td>Network Operations</td>
<td>Impact1</td>
</tr>
<tr>
<td>NumberYYYYYY</td>
<td>S3 </td>
<td>Description again is very very very very very very long</td>
<td>Firewall Operations</td>
<td>Assignee2</td>
<td>Impact2</td>
</tr>
<tr>
<td>NumberXXXXX</td>
<td>S1 </td>
<td>Description size keeps on varying</td>
<td>Node cisco123</td>
<td>Network Operations</td>
<td>Impact1</td>
</tr>
<tr>
<td>NumberYYYYYY</td>
<td>S3 </td>
<td>Description again is very very very long</td>
<td>Firewall Operations</td>
<td>Assignee2</td>
<td>Impact2</td>
</tr>
<tr>
<td>NumberXXXXX</td>
<td>S1 </td>
<td>Description size keeps on varying</td>
<td>Node cisco123</td>
<td>Network Operations</td>
<td>Impact1</td>
</tr>
<tr>
<td>NumberYYYYYY</td>
<td>S3 </td>
<td>Description again is very very very long</td>
<td>Firewall Operations</td>
<td>Assignee2</td>
<td>Impact2</td>
</tr>
<tr>
<td>NumberXXXXX</td>
<td>S1 </td>
<td>Description size keeps on varying</td>
<td>Node cisco123</td>
<td>Network Operations</td>
<td>Impact1</td>
</tr>
<tr>
<td>NumberYYYYYY</td>
<td>S3 </td>
<td>Description again is very very very long</td>
<td>Firewall Operations</td>
<td>Assignee2</td>
<td>Impact2</td>
</tr>
<tr>
<td>NumberXXXXX</td>
<td>S1 </td>
<td>Description size keeps on varying</td>
<td>Node cisco123</td>
<td>Network Operations</td>
<td>Impact1</td>
</tr>
<tr>
<td>NumberYYYYYY</td>
<td>S3 </td>
<td>Description again is very very very long</td>
<td>Firewall Operations</td>
<td>Assignee2</td>
<td>Impact2</td>
</tr>
</tbody>
</table>
</div>