我读过https://gist.github.com/tobysteward/6163902 & AJAX pagination with Laravel但似乎我有一个不同的问题,所以无论如何。Ajax搜索与分页[Laravel]
长话短说:当我搜索时,正确的数据被获取并且ajax只为第一页工作“显示数据,不刷新”,但是从页面的下一页刷新,所以如何进行ajax调用循环返回的数据并根据每个页面显示它?
此外,如何将搜索查询添加到URL中,当前数据获取时,URL不会更新,而是保持其当前状态(即index?page=3
)。
搜索表单
{{ Form::open(['route' => 'search', 'method' => 'GET', 'id' => 's-form',])}}
{{ Form::text('search', null, ['id' => 'search', 'placeholder' => 'Search For ...']) }}
{{ Form::submit(Go) }}
{{ Form::close() }}
搜索控制器
public function search() {
$input = Input::get('search');
$posts = Post::where('title','LIKE',$input)->paginate(4);
if (Request::ajax()) {
return View::make('posts.search')->withPosts($posts);
}
return View::make('posts.index')->withPosts($posts);
}
搜索视图
@forelse ($posts as $post)
// do stuff
@endforeach
<nav>{{ $posts->appends(Request::except('page'))->links() }}</nav>
个的JS
$('#s-form').submit(function(e)
{
e.preventDefault();
$.ajax({
url: $(this).attr('action'),
data:{
search: $('#search').val()
},
success: function(data){
$('#result').html(data);
}
});
});
你没有在JavaScript中的任何地方传递页面参数,这应该如何工作?分页需要正确的参数,也没有任何点击处理程序来处理分页通过JavaScript – pfried 2015-01-16 06:59:40
是的,我知道,但如何做到这一点?那是什么实际上要求。 – ctf0 2015-01-16 07:00:22
尝试在ajax请求的数据部分中添加一个页面参数,只需像数据一样硬编码一个数字:{search:$('#search')。val(),page:'2'} – pfried 2015-01-16 07:03:46