Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,15 @@ composer require cakedc/cakephp-datatables
```

### Load plugin
# To bake datatable index pages.

```shell
bin/cake plugin load CakeDC/Datatables
```

# To bake datatable index pages.
```shell
bin/cake bake all Articles --theme Cakedc-datatables
```

### To overwrite existing index pages.
```shell
bin/cake bake all Articles --theme Cakedc-datatables -f
bin/cake bake all Articles -t CakeDC/Datatables
```

# Setting up the datatable fields
Expand Down
144 changes: 84 additions & 60 deletions src/View/Helper/DatatableHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace CakeDC\Datatables\View\Helper;

use Cake\Utility\Inflector;
use Cake\Utility\Text;
use Cake\View\Helper;
use Cake\View\View;
use CakeDC\Datatables\Datatables;
Expand Down Expand Up @@ -49,10 +50,17 @@ class DatatableHelper extends Helper
'drawCallback' => null,
//complete callback function
'onCompleteCallback' => null,
'ajaxUrl' => null,
'autoWidth' => false,
'tableCss' => [
'width' => '100%',
'table-layout' => 'fixed',
'word-wrap' => 'break-word',
],
];

// @todo change to Text::insert format
private $columnSearchTemplate = <<<COLUMN_SEARCH_CONFIGURATION

var api = this.api();

var columnsSearch = %s;
Expand Down Expand Up @@ -94,7 +102,8 @@ class DatatableHelper extends Helper
case 'input':
case 'default':
case 'input':
cell.html('<input type="text" class="form-control input-sm" placeholder="'+ cell.text() +'" />');
var title = $(cell).text();
cell.html('<input type="text" style="width:100%;" placeholder="'+ title +'" />');
$(
'input',
$('.filters th').eq($(api.column(colIdx).header()).index())
Expand Down Expand Up @@ -126,11 +135,10 @@ class DatatableHelper extends Helper
});
break;
}


});
COLUMN_SEARCH_CONFIGURATION;

// @todo change to Text::insert format
private $genericSearchTemplate = <<<GENERIC_SEARCH_CONFIGURATION
$('#%s').on( 'keyup click', function () {
$('#%s').DataTable().search(
Expand All @@ -139,6 +147,7 @@ class DatatableHelper extends Helper
});
GENERIC_SEARCH_CONFIGURATION;

// @todo change to Text::insert format
private $columnSearchHeaderTemplate = <<<COLUMN_SEARCH_HEADER_CONFIGURATION
$('#%s thead tr')
.clone(true)
Expand All @@ -153,48 +162,50 @@ class DatatableHelper extends Helper
*/
private $datatableConfigurationTemplate = <<<DATATABLE_CONFIGURATION
// API callback
%s
:getDataMethod

// Generic search
%s
// Generic search
:searchTemplate

// Datatables configuration
$(() => {

//@todo use configuration for multicolumn filters
%s
:columnSearchTemplate

const dt = $('#:tagId');

$('#%s').DataTable({
dt.DataTable({
orderCellsTop: true,
fixedHeader: true,
ajax: getData(),
autoWidth: :autoWidth,
ajax: getData(),
//searching: false,
pageLength: %s,
processing: %s,
serverSide: %s,
pageLength: :pageLength,
processing: :processing,
serverSide: :serverSide,
//@todo: add option to select the paging type
//pagingType: "simple",
columns: [
%s
:configColumns
],
columnDefs: [
%s
],
language: %s,
lengthMenu: %s,
:definitionColumns
],
language: :language,
lengthMenu: :lengthMenu,
//@todo add function callback in callback Datatable function
drawCallback: %s,
drawCallback: :drawCallback,
//@todo use configuration instead
initComplete: function () {

//onComplete
%s

//column search
%s

:onCompleteCallback
//column search
:columnSearch
},
});

dt.css(:tableCss);
});
DATATABLE_CONFIGURATION;

Expand Down Expand Up @@ -264,32 +275,36 @@ public function setConfigKey($key, $value = null, $merge = true)
* @param string|array $url url to ajax call
* @return void
*/
public function setGetDataUrl($url = null)
public function setGetDataUrl($defaultUrl = null)
{
$url = (array)$url;
$url = (array) $this->getConfig('ajaxUrl', $defaultUrl);
$url = array_merge($url, ['fullBase' => true, '_ext' => 'json']);
$url = $this->Url->build($url);

if (!empty($this->getConfig('extraFields'))) {
$extraFields = $this->processExtraFields();
//@todo change to async or anonymous js function
$this->getDataTemplate = <<<GET_DATA
function getData() {
let getData = async () => {
return {
url:'{$url}',
url:'{$url}',
data: function ( d ) {
return $.extend( {}, d, {
return $.extend( {}, d, {
$extraFields
});
}
}
}
}
}
};
GET_DATA;
} else {
// @todo setConfig type POST
$this->getDataTemplate = <<<GET_DATA
let getData = async () => {
let res = await fetch('{$url}')
}
return {
url:'{$url}',
type: 'POST',
}
};
GET_DATA;
}
}
Expand Down Expand Up @@ -335,7 +350,7 @@ public function setRowActions(?iterable $rowActions = null)
$this->rowActions = [
'name' => 'actions',
'orderable' => 'false',
'width' => '30px',
'width' => '60px',
//@todo: provide template customization for row actions default labels
'links' => [
[
Expand Down Expand Up @@ -388,22 +403,28 @@ public function getDatatableScript(string $tagId): string
$searchTemplate = '';
}

return sprintf(
// @todo change values to config _default
return Text::insert(
$this->datatableConfigurationTemplate,
$this->getDataTemplate,
$searchTemplate,
$columnSearchTemplate,
$tagId,
$this->getConfig('pageLentgh') ?? '10',
$this->getConfig('processing') ? 'true' : 'false',
$this->getConfig('serverSide') ? 'true' : 'false',
$this->configColumns,
$this->definitionColumns,
json_encode($this->getConfig('language')),
json_encode($this->getConfig('lengthMenu')),
$this->getConfig('drawCallback') ? $this->getConfig('drawCallback') : 'null',
$this->getConfig('onCompleteCallback') ? $this->getConfig('onCompleteCallback') : 'null',
$this->getConfig('columnSearch') ? $this->columnSearchTemplate : '',
[
'getDataMethod' => $this->getDataTemplate,
'searchTemplate' => $searchTemplate,
'columnSearchTemplate' => $columnSearchTemplate,
'tagId' => $tagId,
'autoWidth' => $this->getConfig('autoWidth') ? 'true' : 'false',
'pageLength' => $this->getConfig('pageLentgh') ?? '10',
'processing' => $this->getConfig('processing') ? 'true' : 'false',
'serverSide' => $this->getConfig('serverSide') ? 'true' : 'false',
'configColumns' => $this->configColumns,
'definitionColumns' => $this->definitionColumns,
'language' => json_encode($this->getConfig('language')),
'lengthMenu' => json_encode($this->getConfig('lengthMenu')),
'drawCallback' => $this->getConfig('drawCallback') ? $this->getConfig('drawCallback') : 'null',
'onCompleteCallback' => $this->getConfig('onCompleteCallback') ? $this->getConfig('onCompleteCallback') : 'null',
'columnSearch' => $this->getConfig('columnSearch') ? $this->columnSearchTemplate : '',
'tableCss' => json_encode($this->getConfig('tableCss')),
]

);
}

Expand Down Expand Up @@ -489,7 +510,7 @@ protected function processColumnDefinitionsCallbacks()
$parts[] = "'{$key}': {$val}";
}
$rows[] = '{' . implode(',', $parts) . '}';
}
}var title = $(cell).text();
$this->definitionColumns = implode(',', $rows);
}

Expand All @@ -514,14 +535,17 @@ protected function processColumnRenderCallbacks()
if (isset($key['links'])) {
$output .= "\nrender: function(data, type, obj) { ";
$links = $this->processActionLinkList((array)$key['links']);
$output .= 'return ' . implode("\n + ", $links);
$output .= '}';
} elseif ($key['render'] ?? null) {
$output .= "render: {$key['render']}";
} elseif ($key['orderable'] ?? null) {
$output .= "orderable: {$key['orderable']}";
} elseif ($key['width'] ?? null) {
$output .= "width: '{$key['width']}'";
$output .= "return " . implode("\n + ", $links);
$output .= '},';
}
if ($key['render'] ?? null) {
$output .= "\nrender: {$key['render']},";
}
if ($key['orderable'] ?? null) {
$output .= "\norderable: {$key['orderable']},";
}
if ($key['width'] ?? null) {
$output .= "\nwidth: '{$key['width']}',";
}
}
$output .= '}';
Expand Down