Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to use Cakephp pagination using ajax in 2.x version?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 944
    Comment on it

    Hi Reader's,
    Welcome to FindNerd,today we are going to discuss how to use Cakephp pagination using Ajax in 2.x version?

    Pagination is a very important feature for showing multiple records on a single page in a web application. CakePHP pagination  provides a component which is defined in the controller for making paginated queries much easier. we can use by default pagination helper in CakePHP.

    In CakePHP we set default query conditions pagination using the $paginator controller variable in the controller. It will also help for our basis pagination queries.

    There is a compulsory helper (PaginatorHelper) include in our controller and we also need the RequestHandler component for AJAX.

    You can see below how both will be add.

     // AJAX Pagination tools
      public $components = array('RequestHandler', 'Paginator');
      public $helpers = array('Js' => array('Jquery'), 'Paginator');

    In below example you can see how set a view for pagination

    Firstly making a layout by using default pagination helper

    <?php
      <div class="paginator">
        <?php echo $paginator->first(' First ', null, null, array('class' => 'disabled')); ?>
        <?php echo $paginator->prev('Previous ', null, null, array('class' => 'disabled')); ?>
        <?php echo $paginator->numbers(); ?>
        <?php echo $paginator->next(' Next ', null, null, array('class' => 'disabled')); ?>
        <?php echo $paginator->last(' Last ', null, null, array('class' => 'disabled')); ?>
      </div>
    ?>

    Now use javascript for click and load the data

    <script>
        $(document).ready(function(){
          $(".paginator a").click(function(){
            $("#updated_div_id").load(this.href);
             return false;
          })
        });
    </script>

    When you click on div, it fetch the URL (this.href) and calling page using Ajax.

    For example your controller calling 'pagination' action

    <?php
      function pagination(){
        $this->layout = 'ajax';
        $this->paginate = array(
        'order' => array('Module.created_date' => 'desc'),
        'recursive' => -1,
        "limit" => PAGINATION_LIMIT
        );
        $conditions['Module.module_type'] = $module_type;
        $data = $this->paginate("Module", $conditions);
        $this->set(compact("data"));
      }
    ?>

    I hope this blog will be helpful to implement pagination in your web application.

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: