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
26 changes: 24 additions & 2 deletions src/Html/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Yajra\DataTables\Html;

use Yajra\DataTables\Html\HtmlBuilder;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Contracts\View\Factory;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -30,6 +29,11 @@ class Builder
const SELECT_ITEMS_COLUMN = 'column';
const SELECT_ITEMS_CELL = 'cell';

/**
* The default type to use for the DataTables javascript.
*/
protected static string $jsType = 'text/javascript';

/**
* @var Collection<int, \Yajra\DataTables\Html\Column>
*/
Expand Down Expand Up @@ -73,6 +77,22 @@ public function __construct(public Repository $config, public Factory $view, pub
];
}

/**
* Set the default type to module for the DataTables javascript.
*/
public static function useVite(): void
{
static::$jsType = 'module';
}

/**
* Set the default type to text/javascript for the DataTables javascript.
*/
public static function useWebpack(): void
{
static::$jsType = 'text/javascript';
}

/**
* Generate DataTable javascript.
*
Expand All @@ -83,7 +103,9 @@ public function __construct(public Repository $config, public Factory $view, pub
public function scripts(string $script = null, array $attributes = ['type' => 'text/javascript']): HtmlString
{
$script = $script ?: $this->generateScripts();
$attributes = $this->html->attributes($attributes);
$attributes = $this->html->attributes(
array_merge($attributes, ['type' => static::$jsType])
);

return new HtmlString("<script{$attributes}>$script</script>");
}
Expand Down
10 changes: 10 additions & 0 deletions tests/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@

class BuilderTest extends TestCase
{
/** @test */
public function it_can_use_vitejs_module_script()
{
Builder::useVite();

$this->assertStringContainsString('type="module"', $this->getHtmlBuilder()->scripts()->toHtml());

Builder::useWebpack();
}

/** @test */
public function it_can_resolved_builder_class()
{
Expand Down