Reputation: 1929
I have a view with a table and pagination for this table but when i use de pagination the view is duplicated.
my controller function:
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));
}
}
my 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);
});
});
}
my view:
@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
And when i click at pagination button, the view code is all duplicated within the , the table information is updated but also adds all existing html of view, duplicating code.
Anyone can help me? I already search a lot about this problem, i don't know if is the how to return the information in the controller.
Thank's
Upvotes: 2
Views: 1197
Reputation: 2495
there have some problems with your code. first why you need ajax to render your datas?
if you need use ajax render datas, your controller should be like this:
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));
}
}
if you don't need ajax controller should be like this:
public function index()
{
$items = Items::where('active', '=', 1)->paginate(5);
return view('app.items.index', array('items' => $items));
}
view like this:
<nav class="pagination">
{!! $items->render() !!}
</nav>
Upvotes: 1