Skip to content

Commit cdb2560

Browse files
author
Andres Campanario
committed
add new config for set callback 'CreatedRow' for datatable
1 parent 8d3c70a commit cdb2560

File tree

2 files changed

+44
-19
lines changed

2 files changed

+44
-19
lines changed

src/Datatable/Datatable.php

Lines changed: 38 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,9 +393,18 @@ 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
{
403+
//debug('getDatatableScript');
404+
//debug($this->getConfig('createdRow'));
405+
//debug($this->getConfig('tableId'));
406+
//die("2");
407+
397408
if (empty($this->getDataTemplate)) {
398409
$this->setGetDataUrl();
399410
}
@@ -438,27 +449,35 @@ public function getDatatableScript(): string
438449
}
439450

440451
// @todo change values to config _default
452+
$valuesToReplace = [
453+
'getDataMethod' => $this->getDataTemplate,
454+
'searchTemplate' => $searchTemplate,
455+
'columnSearchTemplate' => $columnSearchTemplate,
456+
'tagId' => $tagId,
457+
'fixedHeader' => $this->getConfig('fixedHeader') ? 'true' : 'false',
458+
'autoWidth' => $this->getConfig('autoWidth') ? 'true' : 'false',
459+
'pageLength' => $this->getConfig('pageLength') ?? '10',
460+
'processing' => $this->getConfig('processing') ? 'true' : 'false',
461+
'serverSide' => $this->getConfig('serverSide') ? 'true' : 'false',
462+
'configColumns' => $this->configColumns,
463+
'definitionColumns' => $this->getConfig('definitionColumns'),
464+
'language' => json_encode($this->getConfig('language')),
465+
'lengthMenu' => json_encode($this->getConfig('lengthMenu')),
466+
'drawCallback' => $this->getConfig('drawCallback') ? $this->getConfig('drawCallback') : 'null',
467+
'onCompleteCallback' => $this->getConfig('onCompleteCallback') ? $this->getConfig('onCompleteCallback') : 'null',
468+
'columnSearch' => $this->getConfig('columnSearch') ? $this->columnSearchTemplate : '',
469+
'tableCss' => json_encode($this->getConfig('tableCss')),
470+
];
471+
472+
if ($this->getConfig('createdRow')) {
473+
$valuesToReplace['callbackCreatedRow'] = 'createdRow: ' . $this->getConfig('createdRow') . ',';
474+
} else {
475+
$valuesToReplace['callbackCreatedRow'] = '';
476+
}
477+
441478
return Text::insert(
442479
$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-
]
480+
$valuesToReplace
462481
);
463482
}
464483

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)