diff --git a/src/Html/Builder.php b/src/Html/Builder.php index c38aa27..5335e06 100644 --- a/src/Html/Builder.php +++ b/src/Html/Builder.php @@ -59,6 +59,11 @@ class Builder */ protected string|array $ajax = ''; + /** + * @var array + */ + protected array $additionalScripts = []; + /** * @param Repository $config * @param Factory $view @@ -179,7 +184,7 @@ protected function template(): string $template = $this->template ?: $configTemplate; - return $this->view->make($template, ['editors' => $this->editors])->render(); + return $this->view->make($template, ['editors' => $this->editors, 'scripts' => $this->additionalScripts])->render(); } /** @@ -291,4 +296,17 @@ public function getAjax(string $key = null): array|string return $this->ajax; } + + /** + * Add additional scripts to the DataTables JS initialization. + * + * @param string $view + * @return $this + */ + public function addScript(string $view): static + { + $this->additionalScripts[] = $view; + + return $this; + } } diff --git a/src/resources/views/scout.blade.php b/src/resources/views/scout.blade.php new file mode 100644 index 0000000..506ad40 --- /dev/null +++ b/src/resources/views/scout.blade.php @@ -0,0 +1,23 @@ +$(function(){ + $('#%1$s').on('xhr.dt', function (e, settings, json, xhr) { + if (json == null || !('disableOrdering' in json)) return; + + let table = {{ config('datatables-html.namespace', 'LaravelDataTables') }}[$(this).attr('id')]; + if (json.disableOrdering) { + table.settings()[0].aoColumns.forEach(function(column) { + column.bSortable = false; + $(column.nTh).removeClass('sorting_asc sorting_desc sorting').addClass('sorting_disabled'); + }); + } else { + let changed = false; + table.settings()[0].aoColumns.forEach(function(column) { + if (column.bSortable) return; + column.bSortable = true; + changed = true; + }); + if (changed) { + table.draw(); + } + } + }); +}); diff --git a/src/resources/views/script.blade.php b/src/resources/views/script.blade.php index 82b3282..b01ac74 100644 --- a/src/resources/views/script.blade.php +++ b/src/resources/views/script.blade.php @@ -1 +1,4 @@ $(function(){window.{{ config('datatables-html.namespace', 'LaravelDataTables') }}=window.{{ config('datatables-html.namespace', 'LaravelDataTables') }}||{};window.{{ config('datatables-html.namespace', 'LaravelDataTables') }}["%1$s"]=$("#%1$s").DataTable(%2$s);}); +@foreach ($scripts as $script) +@include($script) +@endforeach