Skip to content

Commit 013ad87

Browse files
committed
fix the filter navbar
1 parent d982c9d commit 013ad87

File tree

2 files changed

+79
-9
lines changed

2 files changed

+79
-9
lines changed

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

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -783,21 +783,49 @@ function resizeCrudTableColumnWidths() {
783783
// Support for multiple tables with filters
784784
document.addEventListener('backpack:filters:cleared', function (event) {
785785
// Get the table ID from the event detail or default to the current table ID
786-
const tableId = event.detail && event.detail.tableId ? event.detail.tableId : 'crudTable';
786+
let tableId = event.detail && event.detail.tableId ? event.detail.tableId : 'crudTable';
787787
788-
if (!window.crud.tableConfigs[tableId]) return;
788+
// If the specific table config doesn't exist, try to find the first available table
789+
if (!window.crud.tableConfigs[tableId]) {
790+
// Get the first available table config
791+
const availableTableIds = Object.keys(window.crud.tableConfigs);
792+
793+
if (availableTableIds.length > 0) {
794+
tableId = availableTableIds[0];
795+
} else {
796+
return;
797+
}
798+
}
789799
790800
const config = window.crud.tableConfigs[tableId];
791801
792-
// behaviour for ajax table
793-
var new_url = `${config.urlStart}/search`;
802+
// Get the table instance first
794803
var ajax_table = window.crud.tables[tableId];
804+
if (!ajax_table) {
805+
// Try to get the first available table if the specific one doesn't exist
806+
const availableTableIds = Object.keys(window.crud.tables);
807+
if (availableTableIds.length > 0) {
808+
tableId = availableTableIds[0];
809+
ajax_table = window.crud.tables[tableId];
810+
} else {
811+
return;
812+
}
813+
}
814+
815+
// behaviour for ajax table - get the current URL and remove query parameters
816+
let currentAjaxUrl = ajax_table.ajax.url();
817+
818+
// Parse the URL and remove all query parameters except essential ones
819+
let urlObj = new URL(currentAjaxUrl);
820+
let new_url = urlObj.origin + urlObj.pathname;
795821
796822
// replace the datatables ajax url with new_url and reload it
797823
ajax_table.ajax.url(new_url).load();
798824
799825
// remove filters from URL
800-
config.updateUrl(new_url);
826+
if (config.modifiesUrl) {
827+
config.updateUrl(new_url);
828+
}
801829
});
802830
803831
document.addEventListener('backpack:filter:changed', function (event) {

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

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,19 +193,61 @@ function refreshDatatablesOnFilterChange(url, tableId = 'crudTable')
193193
let closestTable = null;
194194
let navbarParent = navbar.parentElement;
195195
196-
// Look for the datatable in the DOM
196+
// Look for the datatable in the DOM - search in the entire document if needed
197197
if (navbarParent) {
198-
// First try to find a table with an ID that starts with the specified prefix
199-
closestTable = navbarParent.querySelector('table[id^="datatable"]');
198+
// First try to find a table with class crud-table
199+
closestTable = navbarParent.querySelector('table.crud-table');
200+
201+
// If not found, try to find a table with an ID that starts with "crudTable"
202+
if (!closestTable) {
203+
closestTable = navbarParent.querySelector('table[id^="crudTable"]');
204+
}
200205
201206
// If not found, try to find any table that might be the datatable
202207
if (!closestTable) {
203208
closestTable = navbarParent.querySelector('table.dataTable');
204209
}
210+
211+
// If still not found, search in the whole document
212+
if (!closestTable) {
213+
closestTable = document.querySelector('table.crud-table');
214+
}
215+
216+
// Last resort - any crudTable in the document
217+
if (!closestTable) {
218+
closestTable = document.querySelector('table[id^="crudTable"]');
219+
}
205220
}
206221
207222
// Get the table ID if found, otherwise use the default 'crudTable'
208-
let tableId = closestTable ? closestTable.id : 'crudTable';
223+
let tableId = 'crudTable'; // Default fallback
224+
225+
if (closestTable) {
226+
// Try to get the ID attribute first
227+
tableId = closestTable.getAttribute('id') || '';
228+
229+
// If no ID found, try to get it from the DataTable instance
230+
if (!tableId && $.fn.DataTable.isDataTable(closestTable)) {
231+
try {
232+
const dt = $(closestTable).DataTable();
233+
if (dt && dt.table && dt.table().node && dt.table().node().id) {
234+
tableId = dt.table().node().id;
235+
}
236+
} catch (e) {
237+
// Silently continue if error getting ID from DataTable
238+
}
239+
}
240+
241+
// If still no ID, check the navbar's data-component-id
242+
if (!tableId) {
243+
tableId = navbar.getAttribute('data-component-id') || '';
244+
}
245+
246+
// Last resort - use default
247+
if (!tableId) {
248+
tableId = 'crudTable';
249+
}
250+
}
209251
210252
document.dispatchEvent(new CustomEvent('backpack:filters:cleared', {
211253
detail: {

0 commit comments

Comments
 (0)