我正在运行Mojave / Laravel 5.8 / PHP 7.2 / Jquery.dataTables 10.1.19。
第一轮:
如果我不输入:if (!$.fn.DataTable.isDataTable('.table')) {...
我收到此错误: DataTables warning: table id=DataTables_Table_0 - Cannot reinitialise DataTable.
我不知道为什么,但是我把这个IF成功了。我还有其他项目可以在没有它的情况下工作。
第二轮:
在索引中,我调用DataTable()
来调用Ajax请求来填充表,因此Ajax URL是(从资源自动创建的)到控制器的路由。
在我的控制器中,我有一个函数INDEX,它将接收该请求,但是需要检查它是否来自Ajax请求(请求必须是JSON)还是来自查看请求。
我不知道为什么总是会有一个非Json类型。
所以,我收到了这个错误: DataTables warning: table id=DataTables_Table_0 - Invalid JSON response.
index.blade.php
<div class="table-responsive">
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>Nome</th>
<th>NIF</th>
<th>Email</th>
</tr>
</thead>
</table>
</div>
.
.
.
<script type="text/javascript">
if (!$.fn.DataTable.isDataTable('.table')) {
$('.table').DataTable({
"ajax": "{{ route('usuarios.index') }}",
"responsive": true,
"processing": true,
"serverSide": true,
"columns": [
{ "data": "name" },
{ "data": "nif" },
{ "data": "email" },
]
});
};
</script>
UsuariosController.php
public function index(Request $request){
if (request()->wantsJson()) {
NEVER HERE
return ...
}
return view('admin.acesso.usuarios.index');
}
Routes.php
Route::group(['prefix' => 'acesso'], function () {
Route::resource('usuarios', "Acesso\UsuariosController");
});
答案 0 :(得分:0)
错误:DataTables警告:表id = DataTables_Table_0-无法重新初始化DataTable。
您无法重新初始化数据表。你必须摧毁它。
if ($.fn.DataTable.isDataTable("#your_table")) {
$('#mytable').DataTable().clear().destroy();
}
$("#your_table").dataTable({
...
});
DataTables警告:表id = DataTables_Table_0-无效的JSON响应。
表示响应,不包含所有指定的数据。在您的代码中,您具有以下内容
"columns": [
{ "data": "name" },
{ "data": "nif" },
{ "data": "email" },
]
您的回复,需要定义姓名,名字和电子邮件。
示例
...
$dataRes = [
[
"name" => "test name",
"nif" => "test nif",
"email" => "test email",
],
[
"name" => "test name",
"nif" => "test nif",
"email" => "test email",
],
];
$response = [
'data' => $dataRes,
'draw' => $_POST['draw'], // this is from your param
'recordsFiltered' => sizeof($dataRes), // this is if your using search
'recordsTotal' => sizeof($dataRes),
];
return $response