简体   繁体   中英

how to use pagination links from json data in laravel blade

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

在此处输入图像描述

$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]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM