Created
March 12, 2015 21:38
-
-
Save cojahmetov/acf732557d6a161dd3f9 to your computer and use it in GitHub Desktop.
#laravel# Pagination with Ajax ver.2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // controller | |
| public function getDetails($id) | |
| { | |
| //Retrieve post details | |
| $data["post"] = Blog::post_details($id); | |
| //Retrieve comments for this post | |
| $data["comments"] = $comments = Blog::post_comments($id,4); | |
| //Comments pagination | |
| $data["comments_pages"] = $comments->links(); | |
| if(Request::ajax()) | |
| { | |
| $html = View::make('blog.post_comments', $data)->render(); | |
| return Response::json(array('html' => $html)); | |
| } | |
| return View::make('blog.post', $data); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //js | |
| $(document).ready(function() | |
| { | |
| $(".pagination a").click(function() | |
| { | |
| var myurl = $(this).attr('href'); | |
| $.ajax( | |
| { | |
| url: myurl, | |
| type: "get", | |
| datatype: "html", | |
| beforeSend: function() | |
| { | |
| $('#ajax-loading').show(); | |
| } | |
| }) | |
| .done(function(data) | |
| { | |
| $('#ajax-loading').hide(); | |
| $("#comments").empty().html(data.html); | |
| }) | |
| .fail(function(jqXHR, ajaxOptions, thrownError) | |
| { | |
| alert('No response from server'); | |
| }); | |
| return false; | |
| }); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //partial | |
| <?php foreach($comments as $comment): ?> | |
| <div> | |
| <?= HTML::image('img/ph38x38.png', '', array('class' => 'pull-left', 'style' => 'margin-right: 10px')) ?> | |
| <p><?= $comment->name ?> has writen @ <?= strftime('%d/%m/%Y %H:%M:%S', strtotime($comment->date)) ?> </p> | |
| <?= nl2br($comment->body) ?> | |
| </div> | |
| <hr> | |
| <?php endforeach; ?> | |
| <?= $comments_pages ?> | |
| <?= HTML::script('js/jquery.ajax.pagination.js') ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // view | |
| <div id="ajax-loading" class="alert alert-warning" style="display: none;"> | |
| <strong>Loading...</strong> | |
| </div> | |
| <div id="comments"> | |
| @include('blog.post_comments') | |
| </div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment