Skip to content

Override <option></option> template generation to add data-attribute #444

Answered by james-em
james-em asked this question in Q&A
Discussion options

You must be logged in to vote

I found a workaround that works perfectly for me !

onChange: function (value) {
      Object.values(this.options).forEach((ajaxOption) => {
        // Could be duplicated because of how ts behave.
        const originalOptions = this.input.querySelectorAll(`option[value="${ajaxOption.value}"]`);
        originalOptions.forEach((originalOption) => {
          Object.keys(ajaxOption).filter((k) => k.startsWith("data-")).forEach((key) => {
            originalOption.dataset[key.replace("data-", "")] = ajaxOption[key];
          });
        });
      });
    }

Backend usage

render json:

{ items: results.map { |r| { text: r.name, value: r.id, "data-other": r.other? } } }

data-other attribut…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@iamB0rgy
Comment options

Answer selected by james-em
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants