Skip to content

Commit 079d0a5

Browse files
authored
Merge pull request #5847 from Laravel-Backpack/fix-filters
fix filter usage
2 parents d1dcff1 + 4ab91c8 commit 079d0a5

File tree

3 files changed

+66
-22
lines changed

3 files changed

+66
-22
lines changed

src/resources/views/crud/components/datatable/datatable.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class="{{ backpack_theme_config('classes.table') ?? 'table table-striped table-h
4949
data-persistent-table-duration="{{ $crud->getPersistentTableDuration() ?: '' }}"
5050
data-subheading="{{ $crud->getSubheading() ? 'true' : 'false' }}"
5151
data-reset-button="{{ ($crud->getOperationSetting('resetButton') ?? true) ? 'true' : 'false' }}"
52-
data-modifies-url="{{ var_export($modifiesUrl ?? false) }}"
52+
data-modifies-url="{{ ($modifiesUrl ?? false) ? 'true' : 'false' }}"
5353
data-has-export-buttons="{{ var_export($crud->get('list.exportButtons') ?? false) }}"
5454
data-default-page-length="{{ $crud->getDefaultPageLength() }}"
5555
data-page-length-menu="{{ json_encode($crud->getPageLengthMenu()) }}"

src/resources/views/crud/components/datatable/datatable_logic.blade.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,7 @@ function resizeCrudTableColumnWidths() {
784784
document.addEventListener('backpack:filters:cleared', function (event) {
785785
// Get the table ID from the event detail or default to the current table ID
786786
const tableId = event.detail && event.detail.tableId ? event.detail.tableId : 'crudTable';
787+
787788
if (!window.crud.tableConfigs[tableId]) return;
788789
789790
const config = window.crud.tableConfigs[tableId];
@@ -835,9 +836,10 @@ function updateDatatablesOnFilterChange(filterName, filterValue, shouldUpdateUrl
835836
// Set the new URL for the table
836837
table.ajax.url(newUrl);
837838
838-
// Update the browser URL if needed
839+
// Update the browser URL if needed - use browser URL, not AJAX URL
839840
if (shouldUpdateUrl) {
840-
window.crud.updateUrl(newUrl);
841+
let browserUrl = addOrUpdateUriParameter(window.location.href, filterName, filterValue);
842+
tableConfig.updateUrl(browserUrl);
841843
}
842844
843845
// Reload the table with the new URL if needed

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

Lines changed: 61 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ function addOrUpdateUriParameter(uri, parameter, value) {
4646
new_url = new_url.addQuery(parameter, value);
4747
}
4848
49-
$('#remove_filters_button').toggleClass('invisible', !new_url.query());
49+
// Update all remove filter buttons visibility
50+
document.querySelectorAll('.remove_filters_button').forEach(function(button) {
51+
button.classList.toggle('invisible', !new_url.query());
52+
});
5053
5154
return new_url.normalizeQuery().toString();
5255
}
@@ -66,18 +69,28 @@ function updateDatatablesOnFilterChange(filterName, filterValue, update_url = fa
6669
// Get the table instance based on the tableId
6770
let table = window.crud.tables[tableId] || window.crud.table;
6871
72+
if (!table) {
73+
console.error('No table found for tableId:', tableId);
74+
return;
75+
}
76+
6977
// behaviour for ajax tables
70-
let new_url = updatePageUrl(filterName, filterValue, table.ajax.url());
71-
table.ajax.url(new_url);
78+
let currentAjaxUrl = table.ajax.url();
79+
let new_ajax_url = addOrUpdateUriParameter(currentAjaxUrl, filterName, filterValue);
80+
81+
// Update the table's ajax URL
82+
table.ajax.url(new_ajax_url);
83+
84+
let browser_url = updatePageUrl(filterName, filterValue, window.location.href);
7285
7386
// when we are clearing ALL filters, we would not update the table url here, because this is done PER filter
7487
// and we have a function that will do this update for us after all filters had been cleared.
7588
if(update_url) {
76-
// replace the datatables ajax url with new_url and reload it
77-
callFunctionOnce(function() { refreshDatatablesOnFilterChange(new_url, tableId) }, debounce, 'refreshDatatablesOnFilterChange_' + tableId);
89+
// replace the datatables ajax url with new_ajax_url and reload it
90+
callFunctionOnce(function() { refreshDatatablesOnFilterChange(new_ajax_url, tableId) }, debounce, 'refreshDatatablesOnFilterChange_' + tableId);
7891
}
7992
80-
return new_url;
93+
return new_ajax_url;
8194
}
8295
}
8396
@@ -109,6 +122,11 @@ function refreshDatatablesOnFilterChange(url, tableId = 'crudTable')
109122
// Get the table instance based on the tableId
110123
let table = window.crud.tables[tableId] || window.crud.table;
111124
125+
if (!table) {
126+
console.error('No table found for refresh, tableId:', tableId);
127+
return;
128+
}
129+
112130
// replace the datatables ajax url with new_url and reload it
113131
table.ajax.url(url).load();
114132
}
@@ -133,23 +151,27 @@ function refreshDatatablesOnFilterChange(url, tableId = 'crudTable')
133151
return;
134152
}
135153
136-
document.addEventListener('backpack:filter:changed', function(event) {
154+
// Add event listener only once per navbar to avoid duplication
155+
if (!navbar.hasAttribute('data-filter-events-bound')) {
156+
navbar.setAttribute('data-filter-events-bound', 'true');
157+
158+
document.addEventListener('backpack:filter:changed', function(event) {
159+
// check if any of the filters are active
160+
let anyActiveFilters = false;
137161
138-
// check if any of the filters are active
139-
let anyActiveFilters = false;
162+
filters.forEach(function(filter) {
163+
if (filter.classList.contains('active')) {
164+
anyActiveFilters = true;
165+
}
166+
});
140167
141-
filters.forEach(function(filter) {
142-
if (filter.classList.contains('active')) {
143-
anyActiveFilters = true;
168+
if(anyActiveFilters === true) {
169+
navbar.querySelector('.remove_filters_button').classList.remove('invisible');
170+
}else{
171+
navbar.querySelector('.remove_filters_button').classList.add('invisible');
144172
}
145173
});
146-
147-
if(anyActiveFilters === true) {
148-
navbar.querySelector('.remove_filters_button').classList.remove('invisible');
149-
}else{
150-
navbar.querySelector('.remove_filters_button').classList.add('invisible');
151-
}
152-
});
174+
}
153175
154176
filters.forEach(function(filter) {
155177
let initFunction = filter.getAttribute('filter-init-function');
@@ -200,6 +222,26 @@ function refreshDatatablesOnFilterChange(url, tableId = 'crudTable')
200222
}
201223
}));
202224
});
225+
226+
// After clearing filters, re-initialize them to ensure proper state
227+
setTimeout(function() {
228+
filters.forEach(function(filter) {
229+
let initFunction = filter.getAttribute('filter-init-function');
230+
if (window[initFunction]) {
231+
window[initFunction](filter, navbar);
232+
}
233+
});
234+
}, 50);
235+
236+
// Force update the URL to remove all filter parameters after a short delay
237+
// to ensure all filters have processed the clear event
238+
setTimeout(function() {
239+
let currentUrl = window.location.href;
240+
let cleanUrl = URI(currentUrl).search('').toString();
241+
if (window.crud && window.crud.updateUrl) {
242+
window.crud.updateUrl(cleanUrl);
243+
}
244+
}, 100);
203245
});
204246
}
205247

0 commit comments

Comments
 (0)