Thursday, 11 June 2015

Cakephp Ajax Pagination

Cakephp Ajax Pagination

Cakephp Ajax pagination works same like normal pagination , the difference is that it load the pages without refreshing

Controller index function

public function index(){
        $this->paginate = array('limit'=>5,'order'=>'User.id desc');  
$userDetail = $this->paginate('User');
$this->set('userDetail',$userDetail);
if($this->RequestHandler->isAjax()){ // check the type of request
$this->layout = '';
$this->autoRender = false;
$this->render('index');  // render the same view
}
}

User index view

// script to load page by ajax
<script>
$(document).ready(function(){
$(".pagination a, .header a").on('click',function(){
$('#content').load(unescape($(this).attr("href")),function(){
});
return false;
  });
});
</script>


<div id="content">
// show the content
    <table>
<?php foreach($userDetail as $admins){?>
<tr>
<td><?php echo $admins['User']['name'];?></td>
<td><?php echo $admins['User']['email'];?></td>
<td><?php echo $admins['User']['message'];?></td>
</tr>
<?php } ?>
    </table>

// pagination helper
<div class="pagination">
<?php echo $this->Paginator->prev('Prev');?>
<?php echo $this->Paginator->numbers(array('separator' => false));?>                  
<?php echo $this->Paginator->next('Next');?>
</div>
</div>

0 comments:

Post a Comment