Skip to content

Commit

Permalink
- Add support for custom callback functions on filter button click
Browse files Browse the repository at this point in the history
  • Loading branch information
elipe17 committed Jul 24, 2024
1 parent 5242b94 commit 4024284
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions admin_interface/static/admin_interface/508/dropdown-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,32 @@ if (typeof (django) !== 'undefined' && typeof (django.jQuery) !== 'undefined') {
'use strict';
$(document).ready(function () {
const filters = document.querySelectorAll('#changelist-filter .list-filter-dropdown select')
let options = ''
let query = ''
// Override the default onchange handler of each filter
for (const filter of filters) {
// This needs to be a function expression so `this` references the filter elements themselves
filter.onchange = function() {
let value = this.options[this.selectedIndex].value
if (options !== '') {
if (query !== '') {
value = value.replace('?', '&')
}
options = options.concat(value)
query = query.concat(value)
};
}

const applyFiltersButton = document.querySelector('#submit-filters');
if (applyFiltersButton) {
applyFiltersButton.onclick = function () {
window.location = options
// The code below allows the 508 filter button handler to call functions with the name
// `custom_filter_callback` to handle the query string logic for filters defined outside of this repo and the
// native Django Admin Console filters.
var custom_filter_query = ""
if (typeof custom_filter_callback === "function") {
custom_filter_query = custom_filter_callback($)
}
////////////////

window.location = query + custom_filter_query
};
}
});
Expand Down

0 comments on commit 4024284

Please sign in to comment.