From 523aeb856124d331d29cd0f6021a01fb3a5832cd Mon Sep 17 00:00:00 2001 From: Pedro Martins Date: Thu, 5 Dec 2024 11:07:22 +0000 Subject: [PATCH] allow datatable url configuration (#5713) --- .../Controllers/Operations/ListOperation.php | 1 + .../views/crud/inc/datatables_logic.blade.php | 44 +++++++++---------- .../views/crud/inc/filters_navbar.blade.php | 2 +- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/app/Http/Controllers/Operations/ListOperation.php b/src/app/Http/Controllers/Operations/ListOperation.php index f9ea830507..b7e2606232 100644 --- a/src/app/Http/Controllers/Operations/ListOperation.php +++ b/src/app/Http/Controllers/Operations/ListOperation.php @@ -45,6 +45,7 @@ protected function setupListDefaults() $this->crud->operation('list', function () { $this->crud->loadDefaultOperationSettingsFromConfig(); + $this->crud->setOperationSetting('datatablesUrl', $this->crud->getRoute()); }); } diff --git a/src/resources/views/crud/inc/datatables_logic.blade.php b/src/resources/views/crud/inc/datatables_logic.blade.php index d02e3070d2..befd31c474 100644 --- a/src/resources/views/crud/inc/datatables_logic.blade.php +++ b/src/resources/views/crud/inc/datatables_logic.blade.php @@ -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 @@ -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) { @@ -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(); @@ -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; @@ -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: { @@ -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]; @@ -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; } @@ -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}}" @@ -312,7 +312,7 @@ functionsToRunOnDataTablesDrawEvent: [], @if($crud->getOperationSetting('resetButton') ?? true) // create the reset button - var crudTableResetButton = '{{ trans('backpack::crud.reset') }}'; + var crudTableResetButton = '{{ trans('backpack::crud.reset') }}'; $('#datatable_info_stack').append(crudTableResetButton); @@ -320,16 +320,16 @@ functionsToRunOnDataTablesDrawEvent: [], $('#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 @@ -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 () { diff --git a/src/resources/views/crud/inc/filters_navbar.blade.php b/src/resources/views/crud/inc/filters_navbar.blade.php index 437661e80b..1b629ba199 100644 --- a/src/resources/views/crud/inc/filters_navbar.blade.php +++ b/src/resources/views/crud/inc/filters_navbar.blade.php @@ -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