Skip to content
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

Resetting the form clears the list of choices #1053

Closed
mscart opened this issue Sep 2, 2022 · 7 comments
Closed

Resetting the form clears the list of choices #1053

mscart opened this issue Sep 2, 2022 · 7 comments
Labels

Comments

@mscart
Copy link

mscart commented Sep 2, 2022

I have a form with some filters that make an ajax request to display a table. When I want to reset the filters ( $("#filter-form").trigger("reset");) all the select elements created with choices.js are empty and I have nothing left to select. I have to refresh the page to repopulate the select forms.
any ideea ?

@mscart mscart added the bug label Sep 2, 2022
@masiorama
Copy link

I'm experiencing the same issue... how can we handle this scenario?

@kumardeepakme
Copy link

Having the same issue. Is there a fix for this?

@Sogl
Copy link

Sogl commented Dec 18, 2022

The same issue when I clear form inputs. Still no fix?

@jamesperrin
Copy link

jamesperrin commented Feb 22, 2023

Ran into the same problem.

I got an idea from issue #1023.

My band-aid is to add an addEventListener() for the form reset.
Then, use Choices setChoices() method to re-populate the select element from the variable pointing to the select element.

It still allows the form normal reset process to work.

document.addEventListener('DOMContentLoaded', function () {
	const choicejsOptions = {
		allowHTML: false,
		removeItemButton: true,
		searchEnabled: true,
		searchChoices: true,
		searchFields: ['label', 'value'],
		placeholder: true,
	};

	const selectList = new Choices('.js-choice__SelectListID', choicejsOptions);

	document.querySelector('form').addEventListener('reset', function () {
		console.log('reset');
		// console.log(selectList);

		selectList.setChoices(function () {
			return selectList.config.choices.map(({ value, label }) => ({
				value,
				label,
				selected: false,
			}));

		});
	});
});

@ceciiiron
Copy link

Even the documentation shows the bug. It removes all of the options when I click the reset button

@bpieres
Copy link

bpieres commented Oct 17, 2023

No news? I can't clean my selections... Thanks

@MatheusRich
Copy link

MatheusRich commented Nov 14, 2023

A work-around (snippet from a Stimulus controller):

const choices = new Choices(this.element, {
  allowHTML: false,
  removeItemButton: this.element.multiple,
});

const form = this.element.closest("form");
if (!form) return;

form.addEventListener("reset", () => {
  choices.destroy();
  choices.init();
});

@Xon Xon mentioned this issue Aug 6, 2024
9 tasks
@Xon Xon closed this as completed Aug 22, 2024
@Xon
Copy link
Collaborator

Xon commented Aug 22, 2024

Implemented as part of #1166

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

9 participants