Coder Social home page Coder Social logo

cakephp-datatable's Introduction

CakePHP DataTable Plugin

DataTable is a cakephp plugin for JQuery DataTables.

Requirements

  • CakePHP 2.2 or higher

Installation

[Manual]

  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]

In your app directory run:

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

[GIT Clone]

In your plugin directory run:

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

Enable Plugin

In your app/Config/bootstrap.php:

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',
 	);
}

See docblock for complete list of component settings.

Once you have your component setup, you will need to add your view.

  • 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.

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

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
		}
	}
}

cakephp-datatable's People

Contributors

shama avatar tigrang avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

josegonzalez

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.