Saturday, 15 February 2014

jquery - AJAX pagination with Laravel -



jquery - AJAX pagination with Laravel -

i have view called posts.blade.php gets included in home.blade.php:

<div id="posts"> @foreach ($posts $post) <div class="list-item clearfix"> <div class="content"> <img src="{{ url::to($post->thumbnail) }}" alt="" /> <h1>{{{ $post->title }}}</h1> </div> <div class="score">{{ $post->rating }}</div> </div> @endforeach <div id="pagination">$posts->links()</div> </div>

when user searches posts, controller's postsearch() function returns json response:

function postsearch() { $posts = $posts->select(...)->where(...)->orderby(...)->paginate(5); //search posts homecoming response::json(view::make('includes.posts', ['posts' => $posts])->render()); }

and jquery appends html on #posts div:

$('#search').submit(function(e) { e.preventdefault(); var form = $(this); $.post(form.attr('action'), form.serialize(), function(data) { $('#posts').html(data); }); });

this works perfect. when click on pagination link, page reloads , search results gone (obvious). how paginate these posts? i've read this, this , this article, don't understand how implement it.

edit

this jquery pagination far:

$('#posts').on('click', '.pagination a', function(e) { e.preventdefault(); var url = $(this).attr('href'), page = url.split('page=')[1], info = $('#search').serializearray(); data.push({page: page}); // add together page variable post info console.log(data); $.post($('#search').attr('action'), data, function(data) { $('#posts').html(data['posts']); }); });

this info beingness send when click on pagination link (page 2):

unfortunately nil happens, posts page 1 maintain showing.

i'm not familiar pagination in laravel judging links listed doesn't hard. code untested though...

$('#pagination a').on('click', function(e){ e.preventdefault(); var url = $(this).attr('href'); $.post(url, $('#search').serialize(), function(data){ $('#posts').html(data); }); }); update

in case pagination links wrong (like op) have build url yourself. take url want , add together ?page=#number it.

// page beingness page number stripped original link var url = $('#search').attr('action')+'?page='+page;

jquery ajax search laravel pagination

No comments:

Post a Comment