Skip to content

Commit fd0f9b5

Browse files
authored
Merge pull request #29 from CakeDC/feature/add-callback-created-row
add new config for set callback 'CreatedRow' for datatable
2 parents 8d3c70a + 131d079 commit fd0f9b5

File tree

3 files changed

+62
-28
lines changed

3 files changed

+62
-28
lines changed

README.md

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -331,17 +331,31 @@ $this->Datatable->getInstance()->setConfig('csrfToken', $this->getRequest()->get
331331
```
332332
Important: if you set POST you must set "unlockedActions" on Security Component, specify the target action in the controller's initialize function
333333
```php
334-
public function initialize(): void
335-
{
336-
parent::initialize();
334+
public function initialize(): void
335+
{
336+
parent::initialize();
337337

338-
...
338+
...
339339

340-
if ($this->components()->has('Security')) {
341-
$this->Security->setConfig('unlockedActions', ['list']);
342-
}
340+
if ($this->components()->has('Security')) {
341+
$this->Security->setConfig('unlockedActions', ['list']);
342+
}
343343

344-
...
344+
...
345345

346-
}
346+
}
347347
```
348+
349+
# Add callback when row is created
350+
351+
For example, if you need to add a css class (to change row color) to each tr according to a specific value in the data
352+
353+
```php
354+
$this->Datatable->setCallbackCreatedRow(
355+
'function( row, data, dataIndex ) {
356+
if (!data.viewed) {
357+
$(row).addClass("rowhighlight");
358+
}
359+
}'
360+
);
361+
``

src/Datatable/Datatable.php

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class Datatable
5454
'drawCallback' => null,
5555
//complete callback function
5656
'onCompleteCallback' => null,
57+
'createdRow' => false,
5758
'ajaxUrl' => null,
5859
'ajaxType' => 'GET',
5960
'csrfToken' => null,
@@ -300,6 +301,7 @@ class Datatable
300301
columnDefs: [
301302
:definitionColumns
302303
],
304+
:callbackCreatedRow
303305
language: :language,
304306
lengthMenu: :lengthMenu,
305307
//@todo add function callback in callback Datatable function
@@ -391,6 +393,10 @@ public function getTableHeaders()
391393
return $this->Helper->Html->tableHeaders($tableHeaders, $headersConfig['headersAttrsTr'], $headersConfig['headersAttrsTh']);
392394
}
393395

396+
public function setCallbackCreatedRow(string $functionCallback)
397+
{
398+
$this->setConfig('createdRow', $functionCallback);
399+
}
394400

395401
public function getDatatableScript(): string
396402
{
@@ -438,27 +444,35 @@ public function getDatatableScript(): string
438444
}
439445

440446
// @todo change values to config _default
447+
$valuesToReplace = [
448+
'getDataMethod' => $this->getDataTemplate,
449+
'searchTemplate' => $searchTemplate,
450+
'columnSearchTemplate' => $columnSearchTemplate,
451+
'tagId' => $tagId,
452+
'fixedHeader' => $this->getConfig('fixedHeader') ? 'true' : 'false',
453+
'autoWidth' => $this->getConfig('autoWidth') ? 'true' : 'false',
454+
'pageLength' => $this->getConfig('pageLength') ?? '10',
455+
'processing' => $this->getConfig('processing') ? 'true' : 'false',
456+
'serverSide' => $this->getConfig('serverSide') ? 'true' : 'false',
457+
'configColumns' => $this->configColumns,
458+
'definitionColumns' => $this->getConfig('definitionColumns'),
459+
'language' => json_encode($this->getConfig('language')),
460+
'lengthMenu' => json_encode($this->getConfig('lengthMenu')),
461+
'drawCallback' => $this->getConfig('drawCallback') ? $this->getConfig('drawCallback') : 'null',
462+
'onCompleteCallback' => $this->getConfig('onCompleteCallback') ? $this->getConfig('onCompleteCallback') : 'null',
463+
'columnSearch' => $this->getConfig('columnSearch') ? $this->columnSearchTemplate : '',
464+
'tableCss' => json_encode($this->getConfig('tableCss')),
465+
];
466+
467+
if ($this->getConfig('createdRow')) {
468+
$valuesToReplace['callbackCreatedRow'] = 'createdRow: ' . $this->getConfig('createdRow') . ',';
469+
} else {
470+
$valuesToReplace['callbackCreatedRow'] = '';
471+
}
472+
441473
return Text::insert(
442474
$this->datatableConfigurationTemplate,
443-
[
444-
'getDataMethod' => $this->getDataTemplate,
445-
'searchTemplate' => $searchTemplate,
446-
'columnSearchTemplate' => $columnSearchTemplate,
447-
'tagId' => $tagId,
448-
'fixedHeader' => $this->getConfig('fixedHeader') ? 'true' : 'false',
449-
'autoWidth' => $this->getConfig('autoWidth') ? 'true' : 'false',
450-
'pageLength' => $this->getConfig('pageLength') ?? '10',
451-
'processing' => $this->getConfig('processing') ? 'true' : 'false',
452-
'serverSide' => $this->getConfig('serverSide') ? 'true' : 'false',
453-
'configColumns' => $this->configColumns,
454-
'definitionColumns' => $this->getConfig('definitionColumns'),
455-
'language' => json_encode($this->getConfig('language')),
456-
'lengthMenu' => json_encode($this->getConfig('lengthMenu')),
457-
'drawCallback' => $this->getConfig('drawCallback') ? $this->getConfig('drawCallback') : 'null',
458-
'onCompleteCallback' => $this->getConfig('onCompleteCallback') ? $this->getConfig('onCompleteCallback') : 'null',
459-
'columnSearch' => $this->getConfig('columnSearch') ? $this->columnSearchTemplate : '',
460-
'tableCss' => json_encode($this->getConfig('tableCss')),
461-
]
475+
$valuesToReplace
462476
);
463477
}
464478

src/View/Helper/DatatableHelper.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,12 @@ public function setRowActions(array $rowActions)
114114
$this->dtInstance->setRowActions($rowActions);
115115
}
116116

117+
118+
public function setCallbackCreatedRow(string $functionCallback)
119+
{
120+
$this->dtInstance->setCallbackCreatedRow($functionCallback);
121+
}
122+
117123
/**
118124
* @param string $tagId
119125
* @return string

0 commit comments

Comments
 (0)