Reputation: 79
How can I use the links for paginate I return in JSON Response.
This is how I return it
return response()->json(['data' => $data,
'pagination' => $data->links()]);
This is how I use it but it does not work
$('#pagination').html(response['pagination']);
This is the response in console.log(response);
{data: {…}, pagination: {…}}
data: {current_page: 1, data: Array(2), first_page_url: 'http://127.0.0.1:8000/get-orders/2?
page=1', from: 1, last_page: 6, …}
pagination: {}
[[Prototype]]: Object
This is the response in console.log(response['pagination']);
{}
[[Prototype]]: Object
Upvotes: -3
Views: 827
Reputation: 1
$output = '<p>your data</p>';
if($data['current_page']==1){
$pb = 'disabled';
}else{
$pb = '';
}
if($data['current_page']==$data['last_page']){
$pbl = 'disabled';
}else{
$pbl = '';
}
$prev = $data['current_page']-1;
$nxt = $data['current_page']+1;
$page = '<ul class="pagination">
<li><a class="gotopage btn" page="'.$prev.'" '. $pb.'> < </a></li>';
for ($i=1; $i <=$data['last_page'] ; $i++) {
if( $i>=$data['current_page']-5 && $i<=$data['current_page']+5){
if($data['current_page']==$i){
$page .= '<li><a class="gotopage btn btn-primary" page="'.$i.'">'.$i.'</a></li>';
}else{
$page .= '<li><a class="gotopage btn" page="'.$i.'">'.$i.'</a></li>';
}
}
}
if($data['current_page']==$data['last_page']){
$page .= '<li><a class="gotopage btn" page="'.$data['last_page'].'" '. $pbl.'> > </a></li></ul>';
}else{
$page .= '<li><a class="gotopage btn" page="'.$nxt.'" '. $pbl.'> > </a></li></ul>';
}
return json_encode(['data'=>$output,'page'=>$page]);
Upvotes: -2