Skip to content

Getting Started

Tigran Gabrielyan edited this page Jun 23, 2015 · 2 revisions

Overview

1. Setup Controller to Handle DataTable Requests Setting it up in AppController will make it available in all controllers. You may want it for just one controller.

<?php
App::uses('Controller', 'Controller');
App::uses('DataTableRequestHandlerTrait', 'DataTable.Lib');
class AppController extends Controller {

	use DataTableRequestHandlerTrait;
}

2. Add Javascript Code to Layout Add the following after jquery and datatable assets:

<?php
echo $this->fetch('dataTableSettings');
echo $this->fetch('script');
?>

3. Add DataTable Component and Helper To Controller With Configuration See Component Configuration

4. Pass the Config to The View and Render The Table In the action that will contain the tables, set the view var that will be used by the helper to generate the markup.

// Controller
public function index() {
	$this->DataTable->setViewVar('{{config_name}}');
}
<!-- View -->
<h1>Users</h1>
<?php echo $this->DataTable->render('{{config_name}}'); ?>

5. Generate DataTable Response View Create app/View/{{Model}}/datatable/{{config_name}}.ctp This view file will use DataTableResponseView which provides $this->dtResponse property that will be returned to the javascript lib. Build the aaData property with the results that are stored in $dtResults view var.

If autoData and autoRender are being used, this file is not required. See Component Configuration for more information on autoData and autoRender.

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

And you're done!