我正在使用jquery ajax数据表。我不知道为什么这个错误出现在我的浏览器中。我认为我的代码没有错误。有人知道这是什么错误吗?我希望将$details
数据显示在数据表中。
我得到DataTables warning: table id=datatable - Ajax error. For more information about this error, please see http://datatables.net/tn/7
控制器
public function edit($id)
{
$item = User::where('id', $id)->first();
$details = \DB::table('finances')
->where('finances.user_id', $id)
->orderBy('finances.id' ,'DESC')
->get();
// dd($details);
return view('admin.pages.finances.show',[
'mnuname' => $this->page_title,
'page_title' => $this->page_title,
'item' => $item,
'details' => $details
]);
}
脚本
@section('javascript')
//<script src="{{ asset('js/app.js') }}" ></script>
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
//<script type="text/javascript" charset="utf8" src="//cdn.datatables.net/1.10.16/js/jquery.dataTables.js"></script>
<script>
console.log('asdasd');
$(document).ready( function () {
$('#datatable').DataTable({
"processing": false,
"serverSide": true,
"ajax": "/finances/{id}",
"type": "GET",
"columns": [
{ "data": "id" },
{ "data": "description" },
{ "data": "amount" },
{ "data": "date" }
]
});
});
</script>
@show
路线
Route::get('finances/{id}', 'FinancesController@edit')->name('admin.finances.edit');
当我dd($details)
Collection {#555 ▼
#items: array:3 [▼
0 => {#541 ▼
+"id": 5
+"school_id": 1
+"user_id": 11
+"amount": "20"
+"description": "Latest"
+"date": "2019-11-15"
+"created_at": null
+"updated_at": null
}
1 => {#558 ▼
+"id": 2
+"school_id": 1
+"user_id": 11
+"amount": "1000"
+"description": "New Payables"
+"date": "2019-11-14"
+"created_at": null
+"updated_at": null
}
2 => {#549 ▼
+"id": 1
+"school_id": 1
+"user_id": 11
+"amount": "5000"
+"description": "Old Payables"
+"date": "2019-11-13"
+"created_at": null
+"updated_at": null
}
]
答案 0 :(得分:1)
替换此
"columns": [
{ "data": "id" },
{ "data": "description" },
{ "data": "amount" },
{ "data": "date" }
]
通过
data : { "id": $("#id").val(), "description":
$("#description").val(), "amount":$("#amount").val(), "date":
$("#date").val()} ,
"columns": [
{ data: "id" },
{ data: "description" },
{ data: "amount" },
{ data: "date" }
]
具有ID的绑定列
答案 1 :(得分:1)
您应该返回json编码的数据。
public function ajaxDatatableEdit($id)
{
$item = User::where('id', $id)->first();
$details = \DB::table('finances')
->where('finances.user_id', $id)
->orderBy('finances.id' ,'DESC')
->get();
return json_encode($details);
}
还为您的ajax和视图提供了一个功能。
public function viewEdit($id)
{
return view('admin.pages.finances.show',[
'mnuname' => $this->page_title,
'page_title' => $this->page_title,
'item' => $item,
]);
}
在您的路线上:
Route::get('finances/{id}', 'FinancesController@ajaxDatatableEdit')->name('admin.finances.edit');`
或者如果您使用表,则可以使用laravel数据表
阅读文档here