我在我的 Laravel 项目中完美地实现了 dataTable。为此,我遵循了一些教程,数据不显示,但一次又一次显示“DataTables warning: table id=DataTables_Table_0 - Ajax error。有关此错误的更多信息,请参阅 https ://datatables.net/tn/7"。我不明白如何解决这个问题。
在控制台中显示以下错误
你能告诉我我的问题出在哪里吗?
我的控制器代码
use App\Models\Totalmember;
use Yajra\DataTables\DataTables;
public function totalviewmembers(Request $request)
{
if ($request->ajax()) {
$data = Totalmember::select('membername', 'cellnumber', 'branchname', 'presentaddress', 'uniqueid');
//echo $data;
return DataTables::of($data)
->addColumn('action', function ($row) {
return '<a href="'.url('edit/'.$row->uniqueid).'" class="btn btn-sm btn-primary">Edit</a>';
})
->rawColumns(['action'])
->make(true);
}
// Load the view
return view('admin.totalmember.viewtotalmembers');
}
路线
Route::get('/viewmember', [TotalmembersController::class, 'totalviewmembers']);
这是我的刀片编码
<table class="table table-bordered data-table">
<thead>
<tr>
<th>Name</th>
<th>Mobile Number</th>
<th>Branch Name</th>
<th>Present Address</th>
<th style="display: none;">Unique ID</th> <!-- Hidden -->
<th>Action</th>
</tr>
</thead>
</table>
<script>
$(function () {
var table = $('.data-table').DataTable({
processing: true,
serverSide: true,
ajax: "{{ url('totalviewmembers') }}", // AJAX Route
columns: [
{data: 'membername', name: 'membername'},
{data: 'cellnumber', name: 'cellnumber'},
{data: 'branchname', name: 'branchname'},
{data: 'presentaddress', name: 'presentaddress'},
{
data: 'uniqueid',
name: 'uniqueid',
visible: false, // Hidden column
searchable: true // Used for search functionality
},
{data: 'action', name: 'action', orderable: false, searchable: false},
]
});
});
</script>
添加路由后,是否清除了路由缓存?
php artisan cache:clear
php artisan route:clear
php artisan config:clear