Skip to content

Instantly share code, notes, and snippets.

@cojahmetov
Created March 12, 2015 21:38
Show Gist options
  • Select an option

  • Save cojahmetov/acf732557d6a161dd3f9 to your computer and use it in GitHub Desktop.

Select an option

Save cojahmetov/acf732557d6a161dd3f9 to your computer and use it in GitHub Desktop.
#laravel# Pagination with Ajax ver.2
<?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);
}
//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;
});
});
//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') ?>
// 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