Hi everyone,
I’m working on a Wix website using the Wix Studio Editor and trying to create a form where selecting an option from a radio button group dynamically filters and populates a dropdown menu with the appropriate options.
Here’s the issue I’m facing: When I select a category from the radio buttons, the dropdown menu should only show items related to that category. However, instead of filtering correctly, the dropdown shows all items from all categories. Additionally, if I select an option from outside the chosen category in the dropdown, nothing happens. If I select an option within the chosen category, it correctly loads the related information.
Can anyone help me identify what might be going wrong in my code? I’d appreciate any advice or examples on how to make the dropdown filter properly based on the selected radio button option. Here is my current code:
import wixData from ‘wix-data’;
$w.onReady(function () {
// Initialize the page with default county selection
initPage();
});
// Function to initialize the page
async function initPage() {
const countyCities = {}; // Cache for cities per county
// Set default selection to 'Los Angeles'
$w('#countyRadioGroup').value = "Los Angeles";
// Populate dropdown with cities for the default county
await populateCityDropdown("Los Angeles", countyCities);
// Event handler for county radio button changes
$w('#countyRadioGroup').onChange(async () => {
const selectedCounty = $w('#countyRadioGroup').value;
console.log("Selected County:", selectedCounty); // Debugging: Log selected county
// Clear the dropdown before populating it with new options
$w('#cityDropdown').options = []; // Clear current options
// Populate the dropdown with cities from the selected county
await populateCityDropdown(selectedCounty, countyCities);
});
// Event handler for dropdown changes to enable the search button
$w('#cityDropdown').onChange(() => {
$w('#searchButton').enable();
});
}
// Function to populate the dropdown with cities based on the selected county
async function populateCityDropdown(county, countyCities) {
console.log(“Populating cities for county:”, county); // Debugging: Log county
// Clear the dropdown before populating it with new options
$w('#cityDropdown').options = [];
// Check if cities for the selected county are already cached
if (countyCities[county]) {
console.log("Using cached cities for county:", county); // Debugging: Log cache use
$w('#cityDropdown').options = countyCities[county]; // Use cached options
handleDropdownState(true);
} else {
console.log("Querying cities for county:", county); // Debugging: Log querying
// Query the collection for cities in the selected county
const results = await wixData.query('Communities')
.eq('County', county) // Ensure field name matches exactly ('County')
.ascending('Title')
.find();
// If cities are found, update the dropdown
if (results.items.length > 0) {
console.log("Cities found:", results.items); // Debugging: Log found cities
const cityOptions = results.items.map(city => ({ label: city.Title, value: city._id })); // Use 'Title' for city names
$w('#cityDropdown').options = cityOptions; // Populate dropdown
countyCities[county] = cityOptions; // Cache the city options
handleDropdownState(true);
} else {
console.log("No cities found for county:", county); // Debugging: Log no cities found
$w('#cityDropdown').options = []; // Clear dropdown if no cities found
handleDropdownState(false);
}
}
}
// Function to handle the dropdown state and search button
function handleDropdownState(hasCities) {
if (hasCities) {
$w(‘#cityDropdown’).selectedIndex = undefined;
$w(‘#cityDropdown’).expand();
$w(‘#searchButton’).disable();
} else {
$w(‘#cityDropdown’).collapse();
$w(‘#searchButton’).enable();
}
}
You can also see what’s happening on the live site by following this link:(https://jack5267.wixstudio.io/my-site/communities-item)
Thanks in advance for your help!
Jack