我有一个带有表和该表的分页的视图,但是当我使用分页时,该视图是重复的。
我的控制器功能:
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,从而重复了代码。
有人可以帮助我吗?我已经搜索了很多有关此问题的信息,我不知道该如何返回控制器中的信息。
谢谢
最佳答案
您的代码有一些问题。
首先,为什么需要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));
}
}
如果不需要ajax控制器,应如下所示:
public function index()
{
$items = Items::where('active', '=', 1)->paginate(5);
return view('app.items.index', array('items' => $items));
}
这样的视图:
<nav class="pagination">
{!! $items->render() !!}
</nav>
关于php - Laravel-分页效果不佳,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38446478/