Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to add datatable in cakephp with records fetching from database

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.66k
    Comment on it

    In cakePhp if you have an extensive database which contains thousands and a huge number of record so you can apply the datatables there. By applying the datatable you have given pagination highlight in which it is dependent upon you that what number of records the client need to appear as a matter of course, values are 10, 25, 50, 100. In datatable you additionally have the pursuit catch in which you can seek the specific field section of the specific table. At the base of the record, there is pagination which demonstrates the page number where you are and you can change to numerous pages and you can likewise guide hop from one page to other.

     

    For utilizing the datatable as a part of your undertaking you have to do the accompanying things:

    Step-1 First of all you ought to have a database with numerous sections. The name of my database is client and the name of the table is clients.

     

    Step-2 Then you need to incorporate the js joins for the datatable and CSS. The connections are given underneath.

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.12.0.min.js"></script>
    <script type="text/javascript" src="http://cdn.datatables.net/1.10.1/js/jquery.dataTables.min.js"></script>
    <link rel="stylesheet" type="text/css" href="http://cdn.datatables.net/1.10.11/css/jquery.dataTables.min.css">

     

    Step-3 Then you have to make index.ctp in your view folder. The code for the index.ctp goes like this.

    <?php
    echo $this->Html->script('custom');
    ?>
    
    
    
    <table id="example" class="display" width="100%" cellspacing="0">
        <thead>
            <tr>
                <th>Id</th>
                <th>Name</th>
                <th>First Name</th>
                <th>Last Name</th>
                <th>Gender</th>
                <th>Password</th>
                <th>Status</th>
            </tr>
        </thead>
    
    
        <?php foreach($users as $user): ?> 
    
            <tr>
    
                <td><?php echo $user['User']['user_id'];?></td>
                <td><?php echo $user['User']['username'];?></td>
                <td><?php echo $user['User']['first_name'];?></td>
                <td><?php echo $user['User']['last_name'];?></td>
                <td><?php echo $user['User']['gender'];?></td>
                <td><?php echo $user['User']['password'];?></td>
                <td><?php echo $user['User']['status'];?></td>
    
    
    
    
    
    
            </tr>
    
        <?php endforeach; ?>
    
    </table>
    

     

    And the code for the controller goes like this.

    <?php
    /**
    * Static content controller.
    *
    * This file will render views from views/pages/
    *
    * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
    * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
    *
    * Licensed under The MIT License
    * For full copyright and license information, please see the LICENSE.txt
    * Redistributions of files must retain the above copyright notice.
    *
    * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
    * @link          http://cakephp.org CakePHP(tm) Project
    * @package       app.Controller
    * @since         CakePHP(tm) v 0.2.9
    * @license       http://www.opensource.org/licenses/mit-license.php MIT License
    */
    
    App::uses('AppController', 'Controller');
    
    /**
    * Static content controller
    *
    * Override this controller by placing a copy in controllers directory of an application
    *
    * @package       app.Controller
    * @link http://book.cakephp.org/2.0/en/controllers/pages-controller.html
    */
    class UsersController extends AppController {
    
    /**
    * This controller does not use a model
    *
    * @var array
    */
    public $uses = array();
    
    /**
    * Displays a view
    *
    * @return void
    * @throws NotFoundException When the view file could not be found
    *  or MissingViewException in debug mode.
    */
    public function index() { 
        $this->set('users', $this->User->find('all'));
    
    }
    }

     

    Step-4 Then you have to write the code for custom.js. The code for the custom.js goes like this.

    $(document).ready(function() {
    	$('#example').DataTable();
    	"paging":   false,
            "ordering": false,
            "info":     false
    } );
    

    Note: In your table it is important to add the thead tag because if you don't apply the thead then nothing is going to work.

 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: