I’m currently working on a PowerApps project where I have several dropdown lists (option sets) that should only allow users to select each team once. I want to prevent users from reselecting teams after they’ve been chosen. However, I’m running into a few issues.
Here’s a Snippet of My Code:
let selectedTeams = [];
// Codes pour les équipes
function onTeamChange(executionContext, fieldName) {
const formContext = executionContext.getFormContext();
const selectedValue = formContext.getAttribute(fieldName).getValue();
// Si une valeur a été désélectionnée
if (selectedValue === null) {
// Retirer l'élément désélectionné de selectedTeams``
selectedTeams = selectedTeams.filter(team => team !== fieldName);
formContext.ui.clearFormNotification("duplicateTeam");
return;
}
// Vérifie si l'équipe est déjà dans la liste des équipes sélectionnées.
if (selectedTeams.includes(selectedValue)) {
formContext.ui.setFormNotification("Cette équipe a déjà été choisie.", "ERROR", "duplicateTeam");
formContext.getAttribute(fieldName).setValue(null); // Réinitialise la sélection pour éviter le doublon.
} else {
formContext.ui.clearFormNotification("duplicateTeam");
selectedTeams.push(selectedValue); // Ajoute la nouvelle sélection dans selectedTeams`.`
}
// Met à jour les options des dropdowns
updateDropdownOptions(formContext);
}
function updateDropdownOptions(formContext) {
const allDropdowns = ["teamOptionSet1", "teamOptionSet2", "teamOptionSet3", "teamOptionSet4"]; // Noms des champs
allDropdowns.forEach(fieldName => {
const dropdown = formContext.getAttribute(fieldName);
const options = dropdown.getOptions(); // Récupère toutes les options de l'option set
// Réinitialise les options de l'option set
options.forEach(option => {
// Si l'option est dans la liste selectedTeams`, désactivez-la`
option.disabled = selectedTeams.includes(option.value);
});
// Met à jour le dropdown avec les options modifiées
// Note: Dans PowerApps, vous ne pouvez pas appeler setOptions. Vous devez le gérer par logique utilisateur.
});
}
I have a JavaScript function that runs on the onchange event for each dropdown. The goal is to check if a selected team has already been chosen and, if so, prevent re-selection.
I’m storing the selected values in an array, but I’m having trouble clearing options after a selection is made.
Issue I’m Facing: The items do not disappear from the dropdown list after selection.
Questions:
How can I modify my code to ensure that selected options are properly removed from the dropdown list?
Any insights, code examples, or pointers would be greatly appreciated! Thanks in advance for your help!
I’m storing the selected values in an array, but I’m having trouble clearing options after a selection is made.
2
Answers
To create a dynamic dropdown list where previously selected teams are disabled and prevent re-selection, we need to adjust your JavaScript logic to update dropdowns effectively. Since PowerApps does not allow us to remove or disable options directly in dropdown lists, we’ll need to filter the options in each dropdown based on the
selectedTeams
array and then set only the filtered list as available.Here’s a revised approach with explanations for each part:
Updated Code
Below is a modified version of your code that implements these principles:
Explanation of Key Changes
onTeamChange
Logic: When a team is selected, the function checks if it’s already in selectedTeams.updateDropdownOptions
loops through each dropdown and filters the options to exclude any value in selectedTeams, except the currently selected option for that dropdown.The
clearOptions()
andaddOption()
methods are used to reset options.Notes
clearOptions()
andaddOption()
are necessary because PowerApps doesn’t support direct modification of existing options in the dropdown.If a dropdown’s current selection is allowed to stay, even if it’s part of selectedTeams, so it doesn’t get unintentionally disabled.
You cannot directly set options as "disabled" in option sets that you can set with HTML shutters.
Here’s an updated version of your code with some adjustments:
Explanation:
1.The
updateDropdownOptions
function now filters out options that are in theselectedTeams
array.2.Instead of trying to disable the options, it updates each dropdown to only display options that haven’t been selected yet.
3.
setOptions
may not work in all PowerApps environments, so if this doesn’t apply to your setup, another approach would be to handle it through conditional logic and user feedback, resetting selections if duplicates are detected.