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

Custom attributes striped from select options #738

Closed
DieterGribnitz opened this issue Nov 4, 2019 · 6 comments
Closed

Custom attributes striped from select options #738

DieterGribnitz opened this issue Nov 4, 2019 · 6 comments

Comments

@DieterGribnitz
Copy link

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.

@tinovyatkin
Copy link
Contributor

@DieterGribnitz there is no way to keep them with the current version of Choices, but you can recreate them by overriding option property on callbackOnTemplateCreate

@DieterGribnitz
Copy link
Author

Thanks I will give it a try.

@MatthieuWinalist
Copy link

Is there still no easy way to do this ? Thanks.

@cedricdo
Copy link

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);

@DuyTr
Copy link

DuyTr commented Aug 8, 2022

How can I re-write this in JS, not Typescript? Thank you

@tagliala
Copy link
Contributor

tagliala commented Dec 15, 2022

I've tried this workaround on version 10.2.0 but I wasn't able to make it work

It looks like that the _presetOptions do not keep the original data attributes (anymore?), and I can't find a way to access them once choices.js is initialized it is working 🤷🏻‍♂️

How can I re-write this in JS, not Typescript? Thank you

  // 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
      }
    }

@Xon Xon closed this as completed Aug 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants