Currently, I am working on a PowerApps project that involves multiple dropdown lists (option sets) where users should only be able to select each team once. I am striving to prevent users from reselecting teams that have already been chosen but facing some challenges in the process. Below is an excerpt of the code I'm working with:
let selectedTeams = [];
// Team Codes
function onTeamChange(executionContext, fieldName) {
const formContext = executionContext.getFormContext();
const selectedValue = formContext.getAttribute(fieldName).getValue();
if (selectedValue === null) {
selectedTeams = selectedTeams.filter(team => team !== fieldName);
formContext.ui.clearFormNotification("duplicateTeam");
return;
}
if (selectedTeams.includes(selectedValue)) {
formContext.ui.setFormNotification("This team has already been chosen.", "ERROR", "duplicateTeam");
formContext.getAttribute(fieldName).setValue(null);
} else {
formContext.ui.clearFormNotification("duplicateTeam");
selectedTeams.push(selectedValue);
}
updateDropdownOptions(formContext);
}
function updateDropdownOptions(formContext) {
const allDropdowns = ["teamOptionSet1", "teamOptionSet2", "teamOptionSet3", "teamOptionSet4"];
allDropdowns.forEach(fieldName => {
const dropdown = formContext.getAttribute(fieldName);
const options = dropdown.getOptions();
options.forEach(option => {
option.disabled = selectedTeams.includes(option.value);
});
});
}
I have implemented a JavaScript function that triggers on the onchange event for each dropdown, aiming to prevent re-selection of already chosen teams.
While I store selected values in an array, I encounter difficulty in clearing options post selection.
Current Issue: The items do not vanish from the dropdown list after selection. Questions: How can I enhance my code to ensure successful removal of selected options from the dropdown list?
I would greatly appreciate any insights, examples of code, or guidance provided. Thank you in advance for your assistance!
Although I store selected values in an array, clearing options after selection poses a challenge.