Skip to content

Commit 60fc09b

Browse files
committed
Added optional scout js script if scout search engine is used (see yajra/laravel-datatables#3082)
1 parent 9fb58f4 commit 60fc09b

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

src/Html/Builder.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ class Builder
5959
*/
6060
protected string|array $ajax = '';
6161

62+
/**
63+
* @var array
64+
*/
65+
protected array $additionalScripts = [];
66+
6267
/**
6368
* @param Repository $config
6469
* @param Factory $view
@@ -179,7 +184,7 @@ protected function template(): string
179184

180185
$template = $this->template ?: $configTemplate;
181186

182-
return $this->view->make($template, ['editors' => $this->editors])->render();
187+
return $this->view->make($template, ['editors' => $this->editors, 'scripts' => $this->additionalScripts])->render();
183188
}
184189

185190
/**
@@ -291,4 +296,17 @@ public function getAjax(string $key = null): array|string
291296

292297
return $this->ajax;
293298
}
299+
300+
/**
301+
* Add additional scripts to the DataTables JS initialization.
302+
*
303+
* @param string $view
304+
* @return $this
305+
*/
306+
public function addScript(string $view): static
307+
{
308+
$this->additionalScripts[] = $view;
309+
310+
return $this;
311+
}
294312
}

src/resources/views/scout.blade.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
$(function(){
2+
$('#%1$s').on('xhr.dt', function (e, settings, json, xhr) {
3+
if (json == null || !('disableOrdering' in json)) return;
4+
5+
let table = LaravelDataTables[$(this).attr('id')];
6+
if (json.disableOrdering) {
7+
table.settings()[0].aoColumns.forEach(function(column) {
8+
column.bSortable = false;
9+
$(column.nTh).removeClass('sorting_asc sorting_desc sorting').addClass('sorting_disabled');
10+
});
11+
} else {
12+
let changed = false;
13+
table.settings()[0].aoColumns.forEach(function(column) {
14+
if (column.bSortable) return;
15+
column.bSortable = true;
16+
changed = true;
17+
});
18+
if (changed) {
19+
table.draw();
20+
}
21+
}
22+
});
23+
});

src/resources/views/script.blade.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
$(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);});
2+
@foreach ($scripts as $script)
3+
@include($script)
4+
@endforeach

0 commit comments

Comments
 (0)