你能建議一種方法來使用帶有分頁的 AJAX LARAVEL 獲取資料嗎?我已經有一個使用 AJAX 的 fetch 函式并且它已經在作業了,但是我想添加一個分頁。先感謝您。
這是我的控制器
public function fetchAllCustomer()
{
$customers = Customer::all();
return response()->json([
'customers'=>$customers,
]);
}
這是我的 AJAX 功能
function fetchstudent() {
$.ajax({
type: "GET",
url: "/fetch-customer",
dataType: "json",
success: function (response) {
// console.log(response);
$('tbody').html("");
$.each(response.customers, function (key, item) {
$('tbody').append('<tr>\
<td>' item.first_name '</td>\
<td><button type="button" value="' item.id '" >Edit</button></td>\
<td><button type="button" value="' item.id '" >Delete</button></td>\
\</tr>');
});
}
});
}
uj5u.com熱心網友回復:
Laravel 檔案 - 使用 Eloquent 進行分頁: https ://laravel.com/docs/9.x/pagination#paginating-eloquent-results
代替:
public function fetchAllCustomer()
{
$customers = Customer::all();
return response()->json([
'customers'=>$customers,
]);
}
應該:
public function fetchAllCustomer()
{
$customers = Customer::paginate(15);
// handle your pagination links in AJAX
$paginationLinks = (string) $customers->links();
return response()->json([
'customers' =>$customers,
'pagination' => $paginationLinks
]);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/440029.html
標籤:javascript jQuery 阿贾克斯 拉拉维尔 分页
下一篇:兩步授權中間件
