Skip to content

Commit a72098a

Browse files
committed
allow datatable url configuration (#5713)
1 parent 6842c91 commit a72098a

File tree

3 files changed

+24
-23
lines changed

3 files changed

+24
-23
lines changed

src/app/Http/Controllers/Operations/ListOperation.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ protected function setupListDefaults()
4545

4646
$this->crud->operation('list', function () {
4747
$this->crud->loadDefaultOperationSettingsFromConfig();
48+
$this->crud->setOperationSetting('datatablesUrl', $this->crud->getRoute());
4849
});
4950
}
5051

src/resources/views/crud/inc/datatables_logic.blade.php

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@
2020
// datatables caches the ajax responses with pageLength in LocalStorage so when changing this
2121
// settings in controller users get unexpected results. To avoid that we will reset
2222
// the table cache when both lengths don't match.
23-
let $dtCachedInfo = JSON.parse(localStorage.getItem('DataTables_crudTable_/{{$crud->getRoute()}}'))
24-
? JSON.parse(localStorage.getItem('DataTables_crudTable_/{{$crud->getRoute()}}')) : [];
23+
let $dtCachedInfo = JSON.parse(localStorage.getItem('DataTables_crudTable_/{{$crud->getOperationSetting("datatablesUrl")}}'))
24+
? JSON.parse(localStorage.getItem('DataTables_crudTable_/{{$crud->getOperationSetting("datatablesUrl")}}')) : [];
2525
var $dtDefaultPageLength = {{ $crud->getDefaultPageLength() }};
2626
let $pageLength = @json($crud->getPageLengthMenu());
2727
28-
let $dtStoredPageLength = parseInt(localStorage.getItem('DataTables_crudTable_/{{$crud->getRoute()}}_pageLength'));
28+
let $dtStoredPageLength = parseInt(localStorage.getItem('DataTables_crudTable_/{{$crud->getOperationSetting("datatablesUrl")}}_pageLength'));
2929
3030
if(!$dtStoredPageLength && $dtCachedInfo.length !== 0 && $dtCachedInfo.length !== $dtDefaultPageLength) {
31-
localStorage.removeItem('DataTables_crudTable_/{{$crud->getRoute()}}');
31+
localStorage.removeItem('DataTables_crudTable_/{{$crud->getOperationSetting("datatablesUrl")}}');
3232
}
3333
3434
if($dtCachedInfo.length !== 0 && $pageLength.indexOf($dtCachedInfo.length) === -1) {
@@ -58,7 +58,7 @@
5858
5959
@if ($crud->getPersistentTable())
6060
61-
var saved_list_url = localStorage.getItem('{{ Str::slug($crud->getRoute()) }}_list_url');
61+
var saved_list_url = localStorage.getItem('{{ Str::slug($crud->getOperationSetting("datatablesUrl")) }}_list_url');
6262
6363
//check if saved url has any parameter or is empty after clearing filters.
6464
if (saved_list_url && saved_list_url.indexOf('?') < 1) {
@@ -78,7 +78,7 @@
7878
}
7979
8080
@if($crud->getPersistentTableDuration())
81-
var saved_list_url_time = localStorage.getItem('{{ Str::slug($crud->getRoute()) }}_list_url_time');
81+
var saved_list_url_time = localStorage.getItem('{{ Str::slug($crud->getOperationSetting("datatablesUrl")) }}_list_url_time');
8282
8383
if (saved_list_url_time) {
8484
var $current_date = new Date();
@@ -125,7 +125,7 @@ functionsToRunOnDataTablesDrawEvent: [],
125125
fn.apply(window, args);
126126
},
127127
updateUrl : function (url) {
128-
let urlStart = "{{ url($crud->route) }}";
128+
let urlStart = "{{ url($crud->getOperationSetting("datatablesUrl")) }}";
129129
let urlEnd = url.replace(urlStart, '');
130130
urlEnd = urlEnd.replace('/search', '');
131131
let newUrl = urlStart + urlEnd;
@@ -146,7 +146,7 @@ functionsToRunOnDataTablesDrawEvent: [],
146146
}
147147
window.history.pushState({}, '', newUrl);
148148
@if ($crud->getPersistentTable())
149-
localStorage.setItem('{{ Str::slug($crud->getRoute()) }}_list_url', newUrl);
149+
localStorage.setItem('{{ Str::slug($crud->getOperationSetting("datatablesUrl")) }}_list_url', newUrl);
150150
@endif
151151
},
152152
dataTableConfiguration: {
@@ -202,7 +202,7 @@ functionsToRunOnDataTablesDrawEvent: [],
202202
203203
stateSaveParams: function(settings, data) {
204204
205-
localStorage.setItem('{{ Str::slug($crud->getRoute()) }}_list_url_time', data.time);
205+
localStorage.setItem('{{ Str::slug($crud->getOperationSetting("datatablesUrl")) }}_list_url_time', data.time);
206206
207207
data.columns.forEach(function(item, index) {
208208
var columnHeading = crud.table.columns().header()[index];
@@ -220,11 +220,11 @@ functionsToRunOnDataTablesDrawEvent: [],
220220
221221
//if the save time as expired we force datatabled to clear localStorage
222222
if($saved_time < $current_date) {
223-
if (localStorage.getItem('{{ Str::slug($crud->getRoute())}}_list_url')) {
224-
localStorage.removeItem('{{ Str::slug($crud->getRoute()) }}_list_url');
223+
if (localStorage.getItem('{{ Str::slug($crud->getOperationSetting("datatablesUrl"))}}_list_url')) {
224+
localStorage.removeItem('{{ Str::slug($crud->getOperationSetting("datatablesUrl")) }}_list_url');
225225
}
226-
if (localStorage.getItem('{{ Str::slug($crud->getRoute())}}_list_url_time')) {
227-
localStorage.removeItem('{{ Str::slug($crud->getRoute()) }}_list_url_time');
226+
if (localStorage.getItem('{{ Str::slug($crud->getOperationSetting("datatablesUrl"))}}_list_url_time')) {
227+
localStorage.removeItem('{{ Str::slug($crud->getOperationSetting("datatablesUrl")) }}_list_url_time');
228228
}
229229
return false;
230230
}
@@ -276,7 +276,7 @@ functionsToRunOnDataTablesDrawEvent: [],
276276
@endif
277277
searching: @json($crud->getOperationSetting('searchableTable') ?? true),
278278
ajax: {
279-
"url": "{!! url($crud->route.'/search').'?'.Request::getQueryString() !!}",
279+
"url": "{!! url($crud->getOperationSetting("datatablesUrl").'/search').'?'.Request::getQueryString() !!}",
280280
"type": "POST",
281281
"data": {
282282
"totalEntryCount": "{{$crud->getOperationSetting('totalEntryCount') ?? false}}"
@@ -319,24 +319,24 @@ functionsToRunOnDataTablesDrawEvent: [],
319319
320320
@if($crud->getOperationSetting('resetButton') ?? true)
321321
// create the reset button
322-
var crudTableResetButton = '<a href="{{url($crud->route)}}" class="ml-1 ms-1" id="crudTable_reset_button">{{ trans('backpack::crud.reset') }}</a>';
322+
var crudTableResetButton = '<a href="{{url($crud->getOperationSetting("datatablesUrl"))}}" class="ml-1 ms-1" id="crudTable_reset_button">{{ trans('backpack::crud.reset') }}</a>';
323323
324324
$('#datatable_info_stack').append(crudTableResetButton);
325325
326326
// when clicking in reset button we clear the localStorage for datatables.
327327
$('#crudTable_reset_button').on('click', function() {
328328
329329
//clear the filters
330-
if (localStorage.getItem('{{ Str::slug($crud->getRoute())}}_list_url')) {
331-
localStorage.removeItem('{{ Str::slug($crud->getRoute()) }}_list_url');
330+
if (localStorage.getItem('{{ Str::slug($crud->getOperationSetting("datatablesUrl"))}}_list_url')) {
331+
localStorage.removeItem('{{ Str::slug($crud->getOperationSetting("datatablesUrl")) }}_list_url');
332332
}
333-
if (localStorage.getItem('{{ Str::slug($crud->getRoute())}}_list_url_time')) {
334-
localStorage.removeItem('{{ Str::slug($crud->getRoute()) }}_list_url_time');
333+
if (localStorage.getItem('{{ Str::slug($crud->getOperationSetting("datatablesUrl"))}}_list_url_time')) {
334+
localStorage.removeItem('{{ Str::slug($crud->getOperationSetting("datatablesUrl")) }}_list_url_time');
335335
}
336336
337337
//clear the table sorting/ordering/visibility
338-
if(localStorage.getItem('DataTables_crudTable_/{{ $crud->getRoute() }}')) {
339-
localStorage.removeItem('DataTables_crudTable_/{{ $crud->getRoute() }}');
338+
if(localStorage.getItem('DataTables_crudTable_/{{ $crud->getOperationSetting("datatablesUrl") }}')) {
339+
localStorage.removeItem('DataTables_crudTable_/{{ $crud->getOperationSetting("datatablesUrl") }}');
340340
}
341341
});
342342
@endif
@@ -357,7 +357,7 @@ functionsToRunOnDataTablesDrawEvent: [],
357357
// so in next requests we know if the length changed by user
358358
// or by developer in the controller.
359359
$('#crudTable').on( 'length.dt', function ( e, settings, len ) {
360-
localStorage.setItem('DataTables_crudTable_/{{$crud->getRoute()}}_pageLength', len);
360+
localStorage.setItem('DataTables_crudTable_/{{$crud->getOperationSetting("datatablesUrl")}}_pageLength', len);
361361
});
362362
363363
$('#crudTable').on( 'page.dt', function () {

src/resources/views/crud/inc/filters_navbar.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ function normalizeAmpersand(string) {
113113
e.preventDefault();
114114
115115
// behaviour for ajax table
116-
var new_url = '{{ url($crud->route.'/search') }}';
116+
var new_url = '{{ url($crud->getOperationSetting("datatablesUrl").'/search') }}';
117117
var ajax_table = $("#crudTable").DataTable();
118118
119119
// replace the datatables ajax url with new_url and reload it

0 commit comments

Comments
 (0)