Mohamed Gaber
Mohamed Gaber

Reputation: 33

laravel Paginator with ajax shows the first page only

i'm using laravel and jquery to make a pagination in SPA page
but the first page only returns results and the rest returns nothing
and here is my controller

public function getList($type)
  {
    if($type == 'all') {
      $html = view('comp.allLists', $this->getAllLists())->render();
    } elseif($type == 'maintenances') {
      $html = view('comp.maintenancesList', $this->getAllMaintenances())->render();
    } elseif($type == 'orders') {
      $html = view('comp.ordersList', $this->getAllOrders())->render();
    }
    return response()->json(['success' => true, 'html' => $html]);
  }
    public function getAllLists()
      {
        $orders = Orders::paginate();
        $maintenances = Maintenance::paginate();
        $items = collect(array_merge($orders->items(), $maintenances->items()))->sortByDesc('received_at');
        $total = $orders->total() + $maintenances->total();
        $perPage = 5;
        $currentPage = LengthAwarePaginator::resolveCurrentPage();
        $currentPageResults = $items->slice(($currentPage - 1) * $perPage, $perPage);
        $all = new LengthAwarePaginator($currentPageResults, $total, $perPage, $currentPage);
        return ['all' => $all];
      }

here is the route

Route::prefix('get')->group(function() {
  Route::get('list/{type}', 'gettingController@getList');
});

here is the ajax request

$('body').on('click', '.pagination li', function() {
    var type = $('.pagType').data('type'),
        page = $(this).find('a').attr('href').split('page=')[1];
        getPage('/get/list/'+ type +'?page='+ page,{}, function(data) {
          $('#table-wrapper').html(data.html);
        });
  });

and finally in the view page iam using

{!! $all->links() !!}

Upvotes: 0

Views: 513

Answers (1)

Anandu Krishna
Anandu Krishna

Reputation: 123

$('.pagination li').on('click',function(e){
var type = $('.pagType').data('type'),
    page = $(this).find('a').attr('href').split('page=')[1];
    getPage('/get/list/'+ type +'?page='+ page,{}, function(data) {
      $('#table-wrapper').html(data.html);
    });

});

Use This Code..

It's your Javascript Issue..

Upvotes: 1

Related Questions