带有ajax的laravel Paginator仅显示第一页

时间:2018-03-09 00:32:42

标签: php jquery ajax laravel pagination

我正在使用laravel和jquery在SPA页面中进行分页 但是第一页只返回结果而其余的都没有返回任何内容 这是我的控制器

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];
      }

这是路线

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

这是ajax请求

$('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);
        });
  });

最后在视图页面中使用

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

1 个答案:

答案 0 :(得分:1)

$('.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);
    });

});

使用此代码..

这是您的Javascript问题..