Your code seems to be logically correct, but there’s a potential issue with the way the dropdown options are being updated. The filterDropdownOptions
function is querying the dataset for items that match the selected value and then setting the options of the other dropdowns to the values of the same field from the filtered items. This means that the dropdowns are being updated with options that are all the same, which is likely not the intended behavior.
To update each dropdown with the unique values of its corresponding field from the filtered items, you’ll need to know the field key for each dropdown. You can modify the filterDropdownOptions
function to accept an additional parameter, fieldKeys
, which is an object that maps dropdown IDs to their corresponding field keys. Then, when updating the dropdown options, use the appropriate field key for each dropdown.
see if this will work, I have not tested.
async function filterDropdownOptions(fieldKey, selectedValue, dropdownsToUpdate, fieldKeys) {
// Build filter object based on selected value
let filter = {};
filter[fieldKey] = selectedValue;
// Clear options for dropdowns to update
dropdownsToUpdate.forEach((dropdownId) => {
$w(dropdownId).options = []; // Clear existing options
});
// Query dataset to get filtered options
let filteredItems = await wixData.query('dynamicDataset')
.eq(fieldKey, selectedValue)
.find();
// Update options for each dropdown based on filter result
dropdownsToUpdate.forEach((dropdownId) => {
let dropdownOptions = filteredItems.items.map(item => item[fieldKeys[dropdownId]]);
dropdownOptions = [...new Set(dropdownOptions)]; // Remove duplicates
// Update dropdown options
$w(dropdownId).options = dropdownOptions.map(option => {
return { label: option, value: option };
});
});
}
And here’s how you would call this function in the onChange
event handlers:
$w('#dropdown1').onChange((event) => {
filterDropdownOptions('category', event.target.value, ['dropdown2', 'dropdown4'], {
'dropdown2': 'description',
'dropdown4': 'artNo'
});
});
$w('#dropdown2').onChange((event) => {
filterDropdownOptions('description', event.target.value, ['dropdown1', 'dropdown4'], {
'dropdown1': 'category',
'dropdown4': 'artNo'
});
});
$w('#dropdown4').onChange((event) => {
filterDropdownOptions('artNo', event.target.value, ['dropdown1', 'dropdown2'], {
'dropdown1': 'category',
'dropdown2': 'description'
});
});