Skip to content

Commit

Permalink
allow datatable url configuration (#5713)
Browse files Browse the repository at this point in the history
  • Loading branch information
pxpm authored Dec 5, 2024
1 parent 714f500 commit 523aeb8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
1 change: 1 addition & 0 deletions src/app/Http/Controllers/Operations/ListOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ protected function setupListDefaults()

$this->crud->operation('list', function () {
$this->crud->loadDefaultOperationSettingsFromConfig();
$this->crud->setOperationSetting('datatablesUrl', $this->crud->getRoute());
});
}

Expand Down
44 changes: 22 additions & 22 deletions src/resources/views/crud/inc/datatables_logic.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
// datatables caches the ajax responses with pageLength in LocalStorage so when changing this
// settings in controller users get unexpected results. To avoid that we will reset
// the table cache when both lengths don't match.
let $dtCachedInfo = JSON.parse(localStorage.getItem('DataTables_crudTable_/{{$crud->getRoute()}}'))
? JSON.parse(localStorage.getItem('DataTables_crudTable_/{{$crud->getRoute()}}')) : [];
let $dtCachedInfo = JSON.parse(localStorage.getItem('DataTables_crudTable_/{{$crud->getOperationSetting("datatablesUrl")}}'))
? JSON.parse(localStorage.getItem('DataTables_crudTable_/{{$crud->getOperationSetting("datatablesUrl")}}')) : [];
var $dtDefaultPageLength = {{ $crud->getDefaultPageLength() }};
let $dtStoredPageLength = localStorage.getItem('DataTables_crudTable_/{{$crud->getRoute()}}_pageLength');
let $dtStoredPageLength = localStorage.getItem('DataTables_crudTable_/{{$crud->getOperationSetting("datatablesUrl")}}_pageLength');
if(!$dtStoredPageLength && $dtCachedInfo.length !== 0 && $dtCachedInfo.length !== $dtDefaultPageLength) {
localStorage.removeItem('DataTables_crudTable_/{{$crud->getRoute()}}');
localStorage.removeItem('DataTables_crudTable_/{{$crud->getOperationSetting("datatablesUrl")}}');
}
// in this page we always pass the alerts to localStorage because we can be redirected with
Expand All @@ -51,7 +51,7 @@
@if ($crud->getPersistentTable())
var saved_list_url = localStorage.getItem('{{ Str::slug($crud->getRoute()) }}_list_url');
var saved_list_url = localStorage.getItem('{{ Str::slug($crud->getOperationSetting("datatablesUrl")) }}_list_url');
//check if saved url has any parameter or is empty after clearing filters.
if (saved_list_url && saved_list_url.indexOf('?') < 1) {
Expand All @@ -71,7 +71,7 @@
}
@if($crud->getPersistentTableDuration())
var saved_list_url_time = localStorage.getItem('{{ Str::slug($crud->getRoute()) }}_list_url_time');
var saved_list_url_time = localStorage.getItem('{{ Str::slug($crud->getOperationSetting("datatablesUrl")) }}_list_url_time');
if (saved_list_url_time) {
var $current_date = new Date();
Expand Down Expand Up @@ -118,7 +118,7 @@ functionsToRunOnDataTablesDrawEvent: [],
fn.apply(window, args);
},
updateUrl : function (url) {
let urlStart = "{{ url($crud->route) }}";
let urlStart = "{{ url($crud->getOperationSetting("datatablesUrl")) }}";
let urlEnd = url.replace(urlStart, '');
urlEnd = urlEnd.replace('/search', '');
let newUrl = urlStart + urlEnd;
Expand All @@ -139,7 +139,7 @@ functionsToRunOnDataTablesDrawEvent: [],
}
window.history.pushState({}, '', newUrl);
@if ($crud->getPersistentTable())
localStorage.setItem('{{ Str::slug($crud->getRoute()) }}_list_url', newUrl);
localStorage.setItem('{{ Str::slug($crud->getOperationSetting("datatablesUrl")) }}_list_url', newUrl);
@endif
},
dataTableConfiguration: {
Expand Down Expand Up @@ -195,7 +195,7 @@ functionsToRunOnDataTablesDrawEvent: [],
stateSaveParams: function(settings, data) {
localStorage.setItem('{{ Str::slug($crud->getRoute()) }}_list_url_time', data.time);
localStorage.setItem('{{ Str::slug($crud->getOperationSetting("datatablesUrl")) }}_list_url_time', data.time);
data.columns.forEach(function(item, index) {
var columnHeading = crud.table.columns().header()[index];
Expand All @@ -213,11 +213,11 @@ functionsToRunOnDataTablesDrawEvent: [],
//if the save time as expired we force datatabled to clear localStorage
if($saved_time < $current_date) {
if (localStorage.getItem('{{ Str::slug($crud->getRoute())}}_list_url')) {
localStorage.removeItem('{{ Str::slug($crud->getRoute()) }}_list_url');
if (localStorage.getItem('{{ Str::slug($crud->getOperationSetting("datatablesUrl"))}}_list_url')) {
localStorage.removeItem('{{ Str::slug($crud->getOperationSetting("datatablesUrl")) }}_list_url');
}
if (localStorage.getItem('{{ Str::slug($crud->getRoute())}}_list_url_time')) {
localStorage.removeItem('{{ Str::slug($crud->getRoute()) }}_list_url_time');
if (localStorage.getItem('{{ Str::slug($crud->getOperationSetting("datatablesUrl"))}}_list_url_time')) {
localStorage.removeItem('{{ Str::slug($crud->getOperationSetting("datatablesUrl")) }}_list_url_time');
}
return false;
}
Expand Down Expand Up @@ -269,7 +269,7 @@ functionsToRunOnDataTablesDrawEvent: [],
@endif
searching: @json($crud->getOperationSetting('searchableTable') ?? true),
ajax: {
"url": "{!! url($crud->route.'/search').'?'.Request::getQueryString() !!}",
"url": "{!! url($crud->getOperationSetting("datatablesUrl").'/search').'?'.Request::getQueryString() !!}",
"type": "POST",
"data": {
"totalEntryCount": "{{$crud->getOperationSetting('totalEntryCount') ?? false}}"
Expand Down Expand Up @@ -312,24 +312,24 @@ functionsToRunOnDataTablesDrawEvent: [],
@if($crud->getOperationSetting('resetButton') ?? true)
// create the reset button
var crudTableResetButton = '<a href="{{url($crud->route)}}" class="ml-1 ms-1" id="crudTable_reset_button">{{ trans('backpack::crud.reset') }}</a>';
var crudTableResetButton = '<a href="{{url($crud->getOperationSetting("datatablesUrl"))}}" class="ml-1 ms-1" id="crudTable_reset_button">{{ trans('backpack::crud.reset') }}</a>';
$('#datatable_info_stack').append(crudTableResetButton);
// when clicking in reset button we clear the localStorage for datatables.
$('#crudTable_reset_button').on('click', function() {
//clear the filters
if (localStorage.getItem('{{ Str::slug($crud->getRoute())}}_list_url')) {
localStorage.removeItem('{{ Str::slug($crud->getRoute()) }}_list_url');
if (localStorage.getItem('{{ Str::slug($crud->getOperationSetting("datatablesUrl"))}}_list_url')) {
localStorage.removeItem('{{ Str::slug($crud->getOperationSetting("datatablesUrl")) }}_list_url');
}
if (localStorage.getItem('{{ Str::slug($crud->getRoute())}}_list_url_time')) {
localStorage.removeItem('{{ Str::slug($crud->getRoute()) }}_list_url_time');
if (localStorage.getItem('{{ Str::slug($crud->getOperationSetting("datatablesUrl"))}}_list_url_time')) {
localStorage.removeItem('{{ Str::slug($crud->getOperationSetting("datatablesUrl")) }}_list_url_time');
}
//clear the table sorting/ordering/visibility
if(localStorage.getItem('DataTables_crudTable_/{{ $crud->getRoute() }}')) {
localStorage.removeItem('DataTables_crudTable_/{{ $crud->getRoute() }}');
if(localStorage.getItem('DataTables_crudTable_/{{ $crud->getOperationSetting("datatablesUrl") }}')) {
localStorage.removeItem('DataTables_crudTable_/{{ $crud->getOperationSetting("datatablesUrl") }}');
}
});
@endif
Expand All @@ -350,7 +350,7 @@ functionsToRunOnDataTablesDrawEvent: [],
// so in next requests we know if the length changed by user
// or by developer in the controller.
$('#crudTable').on( 'length.dt', function ( e, settings, len ) {
localStorage.setItem('DataTables_crudTable_/{{$crud->getRoute()}}_pageLength', len);
localStorage.setItem('DataTables_crudTable_/{{$crud->getOperationSetting("datatablesUrl")}}_pageLength', len);
});
$('#crudTable').on( 'page.dt', function () {
Expand Down
2 changes: 1 addition & 1 deletion src/resources/views/crud/inc/filters_navbar.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ function normalizeAmpersand(string) {
e.preventDefault();
// behaviour for ajax table
var new_url = '{{ url($crud->route.'/search') }}';
var new_url = '{{ url($crud->getOperationSetting("datatablesUrl").'/search') }}';
var ajax_table = $("#crudTable").DataTable();
// replace the datatables ajax url with new_url and reload it
Expand Down

0 comments on commit 523aeb8

Please sign in to comment.