Skip to content

Commit 6305260

Browse files
committed
allow datatable url configuration (#5713)
1 parent e2f164f commit 6305260

File tree

3 files changed

+24
-23
lines changed

3 files changed

+24
-23
lines changed

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

+1
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

+22-22
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
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() }};
26-
let $dtStoredPageLength = localStorage.getItem('DataTables_crudTable_/{{$crud->getRoute()}}_pageLength');
26+
let $dtStoredPageLength = localStorage.getItem('DataTables_crudTable_/{{$crud->getOperationSetting("datatablesUrl")}}_pageLength');
2727
2828
if(!$dtStoredPageLength && $dtCachedInfo.length !== 0 && $dtCachedInfo.length !== $dtDefaultPageLength) {
29-
localStorage.removeItem('DataTables_crudTable_/{{$crud->getRoute()}}');
29+
localStorage.removeItem('DataTables_crudTable_/{{$crud->getOperationSetting("datatablesUrl")}}');
3030
}
3131
3232
// in this page we always pass the alerts to localStorage because we can be redirected with
@@ -51,7 +51,7 @@
5151
5252
@if ($crud->getPersistentTable())
5353
54-
var saved_list_url = localStorage.getItem('{{ Str::slug($crud->getRoute()) }}_list_url');
54+
var saved_list_url = localStorage.getItem('{{ Str::slug($crud->getOperationSetting("datatablesUrl")) }}_list_url');
5555
5656
//check if saved url has any parameter or is empty after clearing filters.
5757
if (saved_list_url && saved_list_url.indexOf('?') < 1) {
@@ -71,7 +71,7 @@
7171
}
7272
7373
@if($crud->getPersistentTableDuration())
74-
var saved_list_url_time = localStorage.getItem('{{ Str::slug($crud->getRoute()) }}_list_url_time');
74+
var saved_list_url_time = localStorage.getItem('{{ Str::slug($crud->getOperationSetting("datatablesUrl")) }}_list_url_time');
7575
7676
if (saved_list_url_time) {
7777
var $current_date = new Date();
@@ -118,7 +118,7 @@ functionsToRunOnDataTablesDrawEvent: [],
118118
fn.apply(window, args);
119119
},
120120
updateUrl : function (url) {
121-
let urlStart = "{{ url($crud->route) }}";
121+
let urlStart = "{{ url($crud->getOperationSetting("datatablesUrl")) }}";
122122
let urlEnd = url.replace(urlStart, '');
123123
urlEnd = urlEnd.replace('/search', '');
124124
let newUrl = urlStart + urlEnd;
@@ -139,7 +139,7 @@ functionsToRunOnDataTablesDrawEvent: [],
139139
}
140140
window.history.pushState({}, '', newUrl);
141141
@if ($crud->getPersistentTable())
142-
localStorage.setItem('{{ Str::slug($crud->getRoute()) }}_list_url', newUrl);
142+
localStorage.setItem('{{ Str::slug($crud->getOperationSetting("datatablesUrl")) }}_list_url', newUrl);
143143
@endif
144144
},
145145
dataTableConfiguration: {
@@ -195,7 +195,7 @@ functionsToRunOnDataTablesDrawEvent: [],
195195
196196
stateSaveParams: function(settings, data) {
197197
198-
localStorage.setItem('{{ Str::slug($crud->getRoute()) }}_list_url_time', data.time);
198+
localStorage.setItem('{{ Str::slug($crud->getOperationSetting("datatablesUrl")) }}_list_url_time', data.time);
199199
200200
data.columns.forEach(function(item, index) {
201201
var columnHeading = crud.table.columns().header()[index];
@@ -213,11 +213,11 @@ functionsToRunOnDataTablesDrawEvent: [],
213213
214214
//if the save time as expired we force datatabled to clear localStorage
215215
if($saved_time < $current_date) {
216-
if (localStorage.getItem('{{ Str::slug($crud->getRoute())}}_list_url')) {
217-
localStorage.removeItem('{{ Str::slug($crud->getRoute()) }}_list_url');
216+
if (localStorage.getItem('{{ Str::slug($crud->getOperationSetting("datatablesUrl"))}}_list_url')) {
217+
localStorage.removeItem('{{ Str::slug($crud->getOperationSetting("datatablesUrl")) }}_list_url');
218218
}
219-
if (localStorage.getItem('{{ Str::slug($crud->getRoute())}}_list_url_time')) {
220-
localStorage.removeItem('{{ Str::slug($crud->getRoute()) }}_list_url_time');
219+
if (localStorage.getItem('{{ Str::slug($crud->getOperationSetting("datatablesUrl"))}}_list_url_time')) {
220+
localStorage.removeItem('{{ Str::slug($crud->getOperationSetting("datatablesUrl")) }}_list_url_time');
221221
}
222222
return false;
223223
}
@@ -269,7 +269,7 @@ functionsToRunOnDataTablesDrawEvent: [],
269269
@endif
270270
searching: @json($crud->getOperationSetting('searchableTable') ?? true),
271271
ajax: {
272-
"url": "{!! url($crud->route.'/search').'?'.Request::getQueryString() !!}",
272+
"url": "{!! url($crud->getOperationSetting("datatablesUrl").'/search').'?'.Request::getQueryString() !!}",
273273
"type": "POST",
274274
"data": {
275275
"totalEntryCount": "{{$crud->getOperationSetting('totalEntryCount') ?? false}}"
@@ -312,24 +312,24 @@ functionsToRunOnDataTablesDrawEvent: [],
312312
313313
@if($crud->getOperationSetting('resetButton') ?? true)
314314
// create the reset button
315-
var crudTableResetButton = '<a href="{{url($crud->route)}}" class="ml-1 ms-1" id="crudTable_reset_button">{{ trans('backpack::crud.reset') }}</a>';
315+
var crudTableResetButton = '<a href="{{url($crud->getOperationSetting("datatablesUrl"))}}" class="ml-1 ms-1" id="crudTable_reset_button">{{ trans('backpack::crud.reset') }}</a>';
316316
317317
$('#datatable_info_stack').append(crudTableResetButton);
318318
319319
// when clicking in reset button we clear the localStorage for datatables.
320320
$('#crudTable_reset_button').on('click', function() {
321321
322322
//clear the filters
323-
if (localStorage.getItem('{{ Str::slug($crud->getRoute())}}_list_url')) {
324-
localStorage.removeItem('{{ Str::slug($crud->getRoute()) }}_list_url');
323+
if (localStorage.getItem('{{ Str::slug($crud->getOperationSetting("datatablesUrl"))}}_list_url')) {
324+
localStorage.removeItem('{{ Str::slug($crud->getOperationSetting("datatablesUrl")) }}_list_url');
325325
}
326-
if (localStorage.getItem('{{ Str::slug($crud->getRoute())}}_list_url_time')) {
327-
localStorage.removeItem('{{ Str::slug($crud->getRoute()) }}_list_url_time');
326+
if (localStorage.getItem('{{ Str::slug($crud->getOperationSetting("datatablesUrl"))}}_list_url_time')) {
327+
localStorage.removeItem('{{ Str::slug($crud->getOperationSetting("datatablesUrl")) }}_list_url_time');
328328
}
329329
330330
//clear the table sorting/ordering/visibility
331-
if(localStorage.getItem('DataTables_crudTable_/{{ $crud->getRoute() }}')) {
332-
localStorage.removeItem('DataTables_crudTable_/{{ $crud->getRoute() }}');
331+
if(localStorage.getItem('DataTables_crudTable_/{{ $crud->getOperationSetting("datatablesUrl") }}')) {
332+
localStorage.removeItem('DataTables_crudTable_/{{ $crud->getOperationSetting("datatablesUrl") }}');
333333
}
334334
});
335335
@endif
@@ -350,7 +350,7 @@ functionsToRunOnDataTablesDrawEvent: [],
350350
// so in next requests we know if the length changed by user
351351
// or by developer in the controller.
352352
$('#crudTable').on( 'length.dt', function ( e, settings, len ) {
353-
localStorage.setItem('DataTables_crudTable_/{{$crud->getRoute()}}_pageLength', len);
353+
localStorage.setItem('DataTables_crudTable_/{{$crud->getOperationSetting("datatablesUrl")}}_pageLength', len);
354354
});
355355
356356
$('#crudTable').on( 'page.dt', function () {

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

+1-1
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)