Filters work now #7

This commit is contained in:
Ian Renton
2025-10-03 12:22:06 +01:00
parent f725e0e57b
commit f489c75bb1

View File

@@ -10,7 +10,7 @@ var lastUpdateTime;
// Load spots and populate the table. // Load spots and populate the table.
function loadSpots() { function loadSpots() {
$.getJSON('/api/spots', function(jsonData) { $.getJSON('/api/spots' + buildQueryString(), function(jsonData) {
// Store last updated time // Store last updated time
lastUpdateTime = moment.utc(); lastUpdateTime = moment.utc();
updateRefreshDisplay(); updateRefreshDisplay();
@@ -21,13 +21,38 @@ function loadSpots() {
}); });
} }
// Build a query string for the API, based on the filters that the user has selected.
function buildQueryString() {
var str = "?";
var filterNames = ["dx_continent", "de_continent", "mode_type", "source", "band"];
str = str + filterNames.map(n => { return getQueryStringFor(n); }).join("&");
return str;
}
// For a parameter, such as dx_continent, get the query string for the current filter options.
function getQueryStringFor(parameter) {
return parameter + "=" + encodeURIComponent(getSelectedFilterOptions(parameter));
}
// For a parameter, such as dx_continent, get the filter options that are currently selected in the UI.
function getSelectedFilterOptions(parameter) {
return $(".filter-button-" + parameter).filter(function() {
return this.checked;
}).map(function() {
return this.value;
}).get().join(",");
}
// Update the spots table // Update the spots table
function updateTable() { function updateTable() {
// Populate table with headers // Populate table with headers
let headers = Object.keys(spots[0]);
let table = $('<table class="table table-striped table-hover">').append('<thead><tr class="table-primary"></tr></thead><tbody></tbody>'); let table = $('<table class="table table-striped table-hover">').append('<thead><tr class="table-primary"></tr></thead><tbody></tbody>');
["UTC", "DX", "Frequency", "Mode", "Comment", "Source", "Ref.", "DE"].forEach(header => table.find('thead tr').append(`<th>${header}</th>`)); ["UTC", "DX", "Frequency", "Mode", "Comment", "Source", "Ref.", "DE"].forEach(header => table.find('thead tr').append(`<th>${header}</th>`));
if (spots.length == 0) {
table.find('tbody').append('<tr class="table-danger"><td colspan="100" style="text-align:center;">No spots match your filters.</td></tr>');
}
spots.forEach(s => { spots.forEach(s => {
// Create row // Create row
let $tr = $('<tr>'); let $tr = $('<tr>');
@@ -188,7 +213,7 @@ function generateFilterCard(displayName, filterQuery, options) {
$card_body.append(`<h5 class='card-title'>${displayName}</h5>`); $card_body.append(`<h5 class='card-title'>${displayName}</h5>`);
$p = $("<p class='card-text filter-card-text'>"); $p = $("<p class='card-text filter-card-text'>");
options.forEach(o => { options.forEach(o => {
$p.append(`<input type="checkbox" class="btn-check filter-button-${filterQuery}" name="options" id="filter-button-${filterQuery}-${o}" value="${filterQuery}:${o}" autocomplete="off" checked><label class="btn btn-outline-primary" for="filter-button-${filterQuery}-${o}">${o}</label> `); $p.append(`<input type="checkbox" class="btn-check filter-button-${filterQuery}" name="options" id="filter-button-${filterQuery}-${o}" value="${o}" autocomplete="off" onClick="loadSpots()" checked><label class="btn btn-outline-primary" for="filter-button-${filterQuery}-${o}">${o}</label> `);
}); });
$card_body.append($p); $card_body.append($p);
$card.append($card_body); $card.append($card_body);
@@ -207,7 +232,7 @@ function generateBandsFilterCard(displayName, filterQuery, band_options) {
// CSS doesn't like IDs with decimal points in, so we need to replace that in the same way as when we originally // CSS doesn't like IDs with decimal points in, so we need to replace that in the same way as when we originally
// queried the options endpoint and set our CSS. // queried the options endpoint and set our CSS.
var cssFormattedBandName = o['name'] ? o['name'].replace('.', 'p') : "unknown"; var cssFormattedBandName = o['name'] ? o['name'].replace('.', 'p') : "unknown";
$p.append(`<input type="checkbox" class="btn-check filter-button-${filterQuery}" name="options" id="filter-button-${filterQuery}-${o['name']}" value="${filterQuery}:${o['name']}" autocomplete="off" checked><label class="btn btn-outline" id="filter-button-label-${filterQuery}-${cssFormattedBandName}" for="filter-button-${filterQuery}-${o['name']}">${o['name']}</label> `); $p.append(`<input type="checkbox" class="btn-check filter-button-${filterQuery}" name="options" id="filter-button-${filterQuery}-${o['name']}" value="${o['name']}" autocomplete="off" onClick="loadSpots()" checked><label class="btn btn-outline" id="filter-button-label-${filterQuery}-${cssFormattedBandName}" for="filter-button-${filterQuery}-${o['name']}">${o['name']}</label> `);
}); });
$card_body.append($p); $card_body.append($p);
$card.append($card_body); $card.append($card_body);
@@ -271,15 +296,9 @@ $(document).ready(function() {
}); });
$("#filters-button").click(function() { $("#filters-button").click(function() {
$("#filters-area").toggle(); $("#filters-area").toggle();
// If we have just dismissed the filters panel, reload spots
if (!$("#filters-area").is(":visible")) {
loadSpots()();
}
}); });
$("#close-filters-button").click(function() { $("#close-filters-button").click(function() {
$("#filters-button").button("toggle"); $("#filters-button").button("toggle");
$("#filters-area").hide(); $("#filters-area").hide();
// We have just dismissed the filters panel, reload spots
loadSpots();
}); });
}); });