-
Notifications
You must be signed in to change notification settings - Fork 618
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
Custom attributes striped from select options #738
Comments
@DieterGribnitz there is no way to keep them with the current version of Choices, but you can recreate them by overriding |
Thanks I will give it a try. |
Is there still no easy way to do this ? Thanks. |
We ran into this issue and got an "easy" fix : new Choices(select, {
callbackOnCreateTemplates: function () {
return {
option: ({ label, value, customProperties, active, disabled, }: Item) => {
const opt: HTMLOptionElement = Choices.defaults.templates.option.call(
this,
{ label, value, customProperties, active, disabled }
);
// We get the original <option> from choicejs
const originalOption: HTMLOptionElement = this._presetOptions.filter(
(option: HTMLOptionElement) => option.value === value
)[0];
/**
* Let's iterate over original <option> data- attribute to reassign them to the <option>
* which choicejs generates
*/
for (const [dataProperty, dataValue] of Object.entries(originalOption.dataset)) {
opt.dataset[dataProperty] = typeof dataValue === 'string' ? dataValue : undefined;
}
return opt;
}
}
}
} as any); |
How can I re-write this in JS, not Typescript? Thank you |
I've tried this workaround on version
// Needed because by default Choices does not transfer data attributes
// to the javascript elements Choices-js/Choices#738
callbackOnCreateTemplates: function () {
return {
option: ({ label, value, customProperties, active, disabled }) => {
const opt = Choices.defaults.templates.option.call(this, { label, value, customProperties, active, disabled })
const originalOption = this._presetOptions.filter((option) => option.value === value)[0]
if (originalOption) {
const dataset = originalOption.dataset || {}
for (const [dataProperty, dataValue] of Object.entries(dataset)) {
opt.dataset[dataProperty] = typeof dataValue === 'string' ? dataValue : undefined
}
}
return opt
}
} |
I have a select list that requires custom data attributes to be read from the selected option on change.
If an item is selected a function runs using the custom option data.
If I apply the lib to a select list all options are striped out and all data attributes are removed.
Is there a way to keep the data attributes intact on the options?
If not, can you point me to where the option in being constructed?
Thanks.
The text was updated successfully, but these errors were encountered: