8000 Merge pull request #253 from ejgandelaberon/master · yajra/laravel-datatables-html@b5bde2b · GitHub
[go: up one dir, main page]

Skip to content

Commit b5bde2b

Browse files
authored
Merge pull request #253 from ejgandelaberon/master
feat: Allow passing data to javascript template
2 parents d24dfd2 + fe96503 commit b5bde2b

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

src/Html/Builder.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ class Builder
6363

6464
protected array $additionalScripts = [];
6565

66+
protected array $templateData = [];
67+
6668
public function __construct(public Repository $config, public Factory $view, public HtmlBuilder $html)
6769
{
6870
/** @var array $defaults */
@@ -159,7 +161,13 @@ protected function template(): string
159161

160162
$template = $this->template ?: $configTemplate;
161163

162-
return $this->view->make($template, ['editors' => $this->editors, 'scripts' => $this->additionalScripts])->render();
164+
return $this->view->make(
165+
$template,
166+
array_merge(
167+
['editors' => $this->editors, 'scripts' => $this->additionalScripts],
168+
$this->templateData,
169+
)
170+
)->render();
163171
}
164172

165 10BC0 173
/**
@@ -292,4 +300,15 @@ public function getAdditionalScripts(): array
292300
{
293301
return $this->additionalScripts;
294302
}
303+
304+
public function setTemplateData(array|\Closure $data = []): static
305+
{
306+
if ($data instanceof \Closure) {
307+
$data = $data($this) ?? [];
308+
}
309+
310+
$this->templateData = $data;
311+
312+
return $this;
313+
}
295314
}

tests/Html/Builder/BuilderTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,4 +303,31 @@ public function it_ignores_unauthorized_columns(): void
303303

304304
$this->assertCount(1, $builder->getColumns());
305305
}
306+
307+
#[Test]
308+
public function it_can_set_template_data(): void
309+
{
310+
$builder = $this->getHtmlBuilder()
311+
->addScript('test-builder-script')
312+
->setTemplateData(['message' => 'Test Message']);
313+
314+
$this->assertStringContainsString(
315+
"console.log({ tableId: 'noneset', message: 'Test Message' });",
316+
$builder->generateScripts()->toHtml()
317+
);
318+
319+
$builder
320+
->setTableId('my-table')
321+
->setTemplateData(function (Builder $builder): array {
322+
return [
323+
'tableId' => $builder->getTableId(),
324+
'message' => 'Set Template Data Using Callback',
325+
];
326+
});
327+
328+
$this->assertStringContainsString(
329+
"console.log({ tableId: 'my-table', message: 'Set Template Data Using Callback' });",
330+
$builder->generateScripts()->toHtml()
331+
);
332+
}
306333
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log({ tableId: @js($tableId ?? 'noneset'), message: @js($message) });

0 commit comments

Comments
 (0)
0