Laravel 5.2中的Ajax分页问题

时间:2016-06-02 10:11:36

标签: jquery ajax pagination laravel-5.2

我想让默认的laravel分页不通过ajax刷新页面但是得到错误,你的帮助真的很感激。 当我点击第2页时,它显示500错误

GET http://localhost:8000/stock?page=2 500 (Internal Server Error)

不知道是什么问题。

这是我的控制者:

public function index()
{
    $menu = 'stock';
    $sub_menu = 'stock_list';
    $category = DB::table('categories')->select('cat_id','category_name')->get();
    $units = DB::table('units')->select('unit_id','unit_name')->get();
    $brands = DB::table('brands')->select('bid','brand_name')->get();
    $purchase_type = DB::table('purchase_types')->select('pt_id','type')->get();

    $items = DB::table('items')
        ->join('categories','categories.cat_id','=','items.category_id')
        ->join('units','units.unit_id','=','items.unit_id')
        ->select('items.*','categories.cat_id','categories.category_name','units.unit_id','units.unit_name')
        ->where('items.quantity','>',0)
        ->paginate(1);
    $total = Item::where('status', 0)->count();
    if (Request::ajax()) {
        return Response::json(view('partials.pagination')->with(compact('items','total'))->render());
    }
    return view('stock.stock-list', compact('items','category', 'purchase_type', 'units','brands','total','menu', 'sub_menu'));
}

和js代码:

  $(document).ready(function (){
  $(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: '/stock?page=' + page
    }).done(function(data){
        $('#theContent').html(data);
    });
}

查看:

<div class="col-sm-6 text-right" id="pagination">
   {{ $items->links() }}

 </div>

1 个答案:

答案 0 :(得分:0)

返回视图而不是json对象

if (Request::ajax()) {
        return view('partials.pagination')->with(compact('items','total'))->render();
    }