Skip to content

Conversation

ejgandelaberon
Copy link
Contributor

This pull request allows devs to add extra template data to javascript template.

Currently, we can add extra js using addScript() method:

class MyDataTableHtml extends DataTableHtml
{
    public function handle(): Builder
    {
        return $this->getHtmlBuilder();
            ->addScript('inline-edit-script');
    }
}

However, it is not possible to pass data to the template. Since the template is just a blade file, I think we should be able to do something like this:

{{-- inline-edit-script.blade.php -- }}

table.on('click', 'tbody td:not(:first-child)', function (e) {
   {{ $editorInstance }}.inline(this);
});

By allowing so, the example script could be reused on datatables that needs inline editing having $editorInstance dynamically.

With this PR, $editorInstance can be set dynamically.

class MyDataTableHtml extends DataTableHtml
{
    public function handle(): Builder
    {
        return $this->getHtmlBuilder();
            ->addScript('inline-edit-script')
            ->setTemplateData([
                'editorInstance' => 'inlineEditor',
            ])

            // or

            ->setTemplateData(function (Builder $builder): array {
                if ($condition) {
                    return ['editorInstance' => 'specialInlineEditor'];
                }

                return ['editorInstance' => 'inlineEditor'];
            });
    }
}

@ejgandelaberon ejgandelaberon marked this pull request as ready for review October 1, 2025 02:09
Copy link
Owner

@yajra yajra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please fix the conflicts, thanks!

Copy link

sonarqubecloud bot commented Oct 1, 2025

@yajra yajra changed the title Allow passing data to javascript template feat: Allow passing data to javascript template Oct 1, 2025
@yajra yajra merged commit b5bde2b into yajra:master Oct 1, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants