Danielle Rose Mabunga Danielle Rose Mabunga - 5 months ago 49
jQuery Question

Paginate Laravel 5.2 with Jquery

I know how to paginate in Laravel, I tried it according to its docs and its so easy.However, I am implementing a Jquery autocomplete on its one of its view, and pagination this time does not work. How would you achieve this?

jquery

$("#city_autocomplete").autocomplete({
source: '/lead-users/autocomplete',
minLength: 2,
select: function(event, ui) {
$("#city_autocomplete").val(ui.item.value);
}
});


Controller

$leads = LeadUser::where('gender', 'LIKE', '%'.$gender.'%')
->orderBy('first_name', 'ASC')
->paginate(20);


View

<tbody>
@foreach($leads as $count)
<tr>
<td>{{ $count->getFullName() }}</td>
<td>{{ $count->email }}</td>
<td>{{ $count->city }}</td>
<td>{{ $count->state }}</td>
<td>{{ $count->getGender() }}</td>
<td>{{ $count->yesOrNo() }}</td>
</tr>
@endforeach
</tbody>
</table>
{{ $counts->links() }}


Ok I update my Jquery

<script>
$(document).ready(function (){
$("#city_autocomplete").autocomplete({
source: '/lead-users/autocomplete',
minLength: 2,
select: function(event, ui) {
$("#city_autocomplete").val(ui.item.value);
}
});
$(window).on('hashchange',function(){
page = window.location.hash.replace('#','');
getItems(page);
});
$(document).on('click','.pagination a', function(e){
e.preventDefault();
var page = $(this).attr('href').split('page=')[1];
// getItems(page);
location.hash = page;
});
function getItems(page){
$.ajax({
url: '/lead-users/autocomplete?page=' + page
}).done(function(data){
$('#leads').html(data);
});
};
});
</script>


This will only work for the pagination 2(1,2,3,4,5,6) the rest will not redirect.It does change the url in the browser but nothing happens on the view

city=Fremont#2
city=Fremont#3
city=Fremont#4

ian ian
Answer

You can try this in your controller

  $leads = LeadUser::where('gender', 'LIKE', '%'.$gender.'%')
        ->orderBy('first_name', 'ASC')
        ->paginate(20);
  return $leads->appends(['gender' => $gender]);
Comments