2016-07-18 1 views
1

私はこのテーブルのテーブルとページ番号を持つビューを持っていますが、ページを使用するとビューが複製されます。 Laravel - ページネーションがうまくいきません

私のコントローラ機能:

public function index() 
{ 
    $items = Items::where('active', '=', 1)->paginate(5); 
    if (Request::ajax()) 
    { 
     return View::make('app/items.index')->with('items', $items)->render(); 
    } 
    else 
    { 
     return view('app/items.index', array('items' => $items));  
    } 

} 

私のjQuery:

function pagination() 
    { 
     $(document).on("click", ".pagination a", function(e) 
     { 
      e.preventDefault(); 
      alert($(this).attr('href')); 
      $.ajax({ 
       url: $(this).attr('href'), 
      }) 
      .done(function(data) 
      { 
       console.log(data); 
       $('#listSearchItems').html(data); 
      }); 


     }); 
    } 

私の見解:

@section('content') 
<div class="header"></div> 
<div id="listSearchItem" class="content"> 
<table> 
</table> 
@if($items->render()) 
       <nav class="pagination"> 
        <a href="{{$items->previousPageUrl()}}"> 
         <i class="material-icons">arrow_back</i> 
        </a> 
        <a href="{{$items->nextPageUrl() }}"> 
         <i class="material-icons">arrow_forward</i> 
        </a> 
       </nav> 
      @endif</div>@stop 

と私は改ページボタンでクリックしたときに、ビューのコードは、すべての中に重複していますテーブル情報は更新されますが、ビューの既存のHTMLもすべて追加され、コードが複製されます。

誰でも手伝ってもらえますか?私はすでにこの問題について多くのことを探していますが、コントローラーの情報を返す方法がわからないのです。

ありがとうございました

答えて

0

コードに問題があります。 なぜデータをレンダリングするためにajaxが必要なのですか?

あなたは件のデータをレンダリングするAJAXを使用する必要がある場合は、お使いのコントローラがこのようにする必要があります:あなたはAJAXのコントローラを必要としない場合

public function index() 
{ 
    $items = Items::where('active', '=', 1)->paginate(5); 
    if (Request::ajax()) 
    { 
     $datas = $items->items(); 
     $results = ''; 
     foreach ($datas as $item) { 
      $results .="<tr><td>{$item->whateverInYourTable}</td><td>{$item->whateverInYourTable}</td></tr>" 
    } 
     return $results; 

    } 
    else 
    { 
    return view('app/items.index', array('items' => $items)); 
    } 
} 

は次のようにする必要があります:このような

public function index() 
{ 
    $items = Items::where('active', '=', 1)->paginate(5); 
    return view('app.items.index', array('items' => $items)); 
} 

ビュー:

<nav class="pagination"> 
    {!! $items->render() !!} 
</nav> 
関連する問題