无法重新初始化数据表错误弹出窗口和居中对齐数据表



如何克服此弹出错误"数据表警告:表 id=DataTables_Table_0 - 无法重新初始化数据表。有关此错误的详细信息,请参阅 http://datatables.net/tn/3">

另外,我想居中对齐表格该怎么做?

我已经尝试了Datatables网站上给出的方法,但它不会解决任何问题 如果我忽略此错误会发生什么?

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>UIT-RGPV</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

</head>
<body>
<div class="container" style="margin-top: 30px">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<table class="table table-bordered table-hover" style="width:100%">
<thead>
<tr>
<td>ID</td>
<td>Alloted Room</td>
</tr>
</thead>
<tbody>
<?php
$conn = new mysqli('localhost', 'root', '', 'data');
$sql = $conn->query('SELECT * FROM user');
while($data = $sql->fetch_array()) {
echo '
<tr>
<td>'.$data['id'].'</td>
<td>'.$data['fname'].'</td>
</tr>
';
}
?>
</tbody>
</table>
</div>
</div>
</div>
<script src="js/jquery-3.4.1.min.js"></script>
<script src="js/jquery.dataTables.js"></script>
<script src="js/dataTables.bootstrap.js"></script>
<script>
$(".table").DataTable();
</script>
<script type="text/javascript">
$(document).ready(function() {
$(".table").DataTable({
"ordering": true,
"searching": true,
"paging": true,
"columnDefs": [
{
"targets": 0,
"searchable": false,
"visible": true
}
],
"order": [[2, "desc"]]
});
});
</script>
</body>
</html>

我想使表格居中对齐,但它在左侧对齐,我想克服那个弹出错误。

删除

<script>
$(".table").DataTable();
</script>

从您的代码中添加style="margin: 0 auto;"到您的表格以使表格位于页面的中心,如果您需要内容必须居中对齐意味着使用style="text-align: center;",如果两者都对齐以居中意味着只使用style="margin: 0 auto; text-align: center;"

最新更新