2017 © Pedro Peláez
 

cakephp-plugin cakephp-datatable

CakePHP DataTable Plugin

image

lucasff/cakephp-datatable

CakePHP DataTable Plugin

  • Wednesday, February 12, 2014
  • by lucasff
  • Repository
  • 1 Watchers
  • 1 Stars
  • 22 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 35 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

CakePHP DataTable Plugin

DataTable is a cakephp plugin for JQuery DataTables., (*1)

Requirements

  • CakePHP 2.2 or higher

Installation

[Manual], (*2)

  1. Download this: http://github.com/tigrang/cakephp-datatable/zipball/master.
  2. Unzip
  3. Copy the resulting folder to app/Plugin
  4. Rename the folder to DataTable

[GIT Submodule], (*3)

In your app directory run:, (*4)

git submodule add https://github.com/tigrang/cakephp-datatable.git Plugin/DataTable
git submodule init
git submodule update

[GIT Clone], (*5)

In your plugin directory run:, (*6)

git clone https://github.com/tigrang/cakephp-datatable.git DataTable

Enable Plugin

In your app/Config/bootstrap.php:, (*7)

CakePlugin::loadAll();
// OR
CakePlugin::load('DataTable');

Usage

Example controller:

<?php
class UsersController extends AppController {

/**
 * Components
 *
 * @var array
 */
    public $components = array(
        'DataTable.DataTable' => array(
            'columns' => array(
                'id' => false,                      // bSearchable and bSortable will be false
                'username' => 'Name',               // bSearchable and bSortable will be true, with a custom label `Name`
                                                    // by default, the label with be a Inflector::humanize() version of the key
                'email' => array(
                    'bSearchable' => 'customSearch',// will use model callback to add search conditions
                ),
                'Actions' => null,                  // tells DataTable that this column is not tied to a field
            ),
        ),
    );

/**
 * Helpers
 *
 * @var array
 */
    public $helpers = array(
        'DataTable.DataTable',
    );
}

You may now paginate more than one model in a view. This becomes useful when displaying two or more hasMany associated models for a view page. List all models to paginate in $this->DataTable->paginate property:, (*8)

<?php
public function view() {
    $this->DataTable->paginate = array('User', 'Article');
}

Note: These models must be directly related., (*9)

See docblock for complete list of component settings., (*10)

Once you have your component setup, you will need to add your view., (*11)

  • First create a View/[ModelName]/datatable folder
  • Create action_name.ctp view inside the folder

The DataTableResponseView (automatically set by the component) class has a member called dtResponse which holds the return data for jquery datatables, including aaData., (*12)

By default, the view var $dtResults will hold resultant model data after searching and paginating. It can be customized with the viewVar setting., (*13)

Example view file:

<?php
foreach($dtResults as $result) {
    $this->dtResponse['aaData'][] = array(
        $result['User']['id'],
        $result['User']['username'],
        $result['User']['email'],
        'actions',
    );
}

Example bSearchable callback:

<?php
class Users extends AppModel {
    public function customSearch($field, $searchTerm, $columnSearchTerm, $conditions) {
        if ($searchTerm) {
            $conditions[] = array("$field LIKE" => '%' . $searchTerm);  // only do left search
        }
        if ($columnSearchTerm) {
            $conditions[] = array($field => $columnSearchTerm);         // only do exact match
        }
        return $conditions;
    }
}

Helper Usage

<?php
$this->DataTable->render(); // renders default model for this view
$this->DataTable->render('AssociatedModel'); // renders 'AssociatedModel' table

If you create the <table> yourself, be sure to add a data-model="Model" attribute to the table tag. The helper is still required to parse the column settings and outputs a global javascript dataTableSettings available for you to use. The helper by default uses the following init script:, (*14)

$('.dataTable').each(function() {
    var table = $(this);
    var model = table.attr('data-model');
    var settings = dataTableSettings[model];
    table.dataTable(settings);
});

The Versions

12/02 2014

dev-master

9999999-dev https://github.com/tigrang/cakephp-datatable

CakePHP DataTable Plugin

  Sources   Download

MIT

The Requires

 

cakephp table datatable