-
Notifications
You must be signed in to change notification settings - Fork 617
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
clearChoice remove placeholder too #803
Comments
Any way you can clear it with |
@ch3rn1k You mean, |
I'll try what you said, in fact it works, but I understand I have miss something to explain. At leat I my trick is this for the moment: const placeholder = Choice.setChoiceByValue('').getValue()
// retrieve data from Ajax
Choice.clearChoices().setChoices(ajaxData, 'id', 'fullName', true).setChoices([placeholder]).setChoiceByValue('') |
although this works what would happen of a choice has a value of |
@agar23 this will be useless because not really usable on a real form. |
@kl3sk fair enough, I was thinking from more of a lib perspective. As a developer I expect the I should not have to use You make a fair point, its probably not very practical to have an option with a value of |
In a As a lib perspective, everything could be considered as anything. So it is still available, but |
but a |
Yes this is possible, but unless reading the textValue it reamins useless ^^. Anyway, without any jokes a choice should be done for the placeholder 😉 |
Hard fix: selectElement.addEventListener('removeItem', function () {
if (selectElement.length === 0) {
choicesInstance
.setChoices([{value: '', label: 'None', placeholder: true}], 'value', 'label', true)
.setChoiceByValue('');
}
}); |
@bupy7 this doesn’t keep the original placeholder. |
@kl3sk if you add placeholder programmatically (I recommend that) is no matter. |
That’s I did in a previous comment. |
@kl3sk, not the same. Your solution has many side effects:
My solution solve the problem in two step:
|
I want to keep the original placeholder, generated by (in my case) Symfony formbuilder. |
This workaround worked for me (when getting data using ajax). It preserved the placeholder, without needing to do if (select.value) choices.clearStore();
choices.setChoices(async () => {
const data = await fetch('/states');
return data.json();
}); |
I combined some of the previous mentioned workarounds, but rather used newOptions.push(choicesInstance.config.choices.find(choice => choice.placeholder));
choicesInstance.setChoices(newOptions, 'value', 'label', true)
.setChoiceByValue(''); |
Very intersting way @FelixWienberg , I use your solution instead of mine. At least @jshjohnson should implement something for this. |
To avoid repetition of the same code, I modified the solution provided by @FelixWienberg if(typeof Choices !== 'undefined'){
Choices.prototype.clearOptions = function(){
const placeholder = this.config.choices?.find(choice => choice.placeholder);
if(placeholder){
this.setChoices([placeholder], 'value', 'label', true).setChoiceByValue('');
} else {
this.clearChoices();
}
};
} choicesInstance.clearOptions(); |
Describe the bug
Clearing the choices will remove all ! Placeholder too.
To Reproduce
Steps to reproduce the behavior:
Expected behavior
Have a choice to keep or not the placeholder
The text was updated successfully, but these errors were encountered: