Coding Cheatsheets - Learn web development code and tutorials for Software developers which will helps you in project. Get help on JavaScript, PHP, XML, and more.

Post Page Advertisement [Top]

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>

No comments:

Post a Comment

Bottom Ad [Post Page]