ahmyi/cakephp-datatables

DataTables plugin for CakePHP

Installs: 8

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 2

Forks: 0

Open Issues: 0

Type:cakephp-plugin

dev-master 2018-12-25 04:06 UTC

This package is auto-updated.

Last update: 2024-04-07 01:59:32 UTC


README

Installation

You can install this plugin into your CakePHP application using composer.

The recommended way to install composer packages is:

composer require ahmyi/cakephp-datatables:dev-master

Usage

src/Application.php

$this->addPlugin('Ahmyi/DataTables');

config/bootstrap.php [prior 3.6]

Plugin::load('Ahmyi/DataTables');

Controller

public function initialize(){
        parent::initialize();

        $this->loadComponent('Ahmyi/DataTables.DataTables');
}

public function index(){
    

   $this->DataTables->use("Pages");  // $this->DataTables->use($this->Pages);
   
   if($datatables = $this->DataTables->process()){
        return $datatables;
   }
}

Template

<div class="row">
	<div class="col-sm-12">
		<?= $this->DataTables->render("Pages");?>
	</div>
</div>

Customizing

Templating

If you want to customize the template from current at your controller define new element

public function initialize(){
        parent::initialize();

        $this->loadComponent('Ahmyi/DataTables.DataTables',[
		'element'=>'your_element'
	]);
}

Your element should have 2 major variables $ModelName and $fields here is an example

</div><div class='box box-theme'>
	<div class='box-header'>
		<h3><?=$header;?></h3>
	</div>
	<div class='box-body'>
		<div class='col-sm-12'>
			<table id='DT<?=$ModelName?>'  cellpadding='0' cellspacing='0' border='0' class='display' width='100%''>
				<thead>
					<tr><?=$fields?></tr>
				</thead>
			</table>
		</div>
	</div>
</div>

Use different js or css assets

You can use differnt Css or Js as example below where it can be in string or array for multiple assets

public function initialize(){
        parent::initialize();

        $this->loadComponent('Ahmyi/DataTables.DataTables',[
		'css'=>'your.css'
		'js'=>'your.js'
	]);
}