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

[Feature Request] Sync nice-select2 value with source control value, when the source value changed #78

Open
triawarman opened this issue Jan 5, 2024 · 2 comments

Comments

@triawarman
Copy link

There are conditions when changing control value are not based on user interaction, but based on automation code, currently select2 has no methode that can sync the select2 selected value with source value when the source value is changed.

i hope you guys make the synchronization method/function.

NiceSelect.prototype.bindEvent = function() {
  var $this = this;
  
  //INFO: not tested with multiple value, and im sure this one is not the best solution
  this.el.addEventListener("change", function() {
    let sourceVal = this.value;
    if(sourceVal !== $this.selectedOptions[0].data.value) {
      let selectedItem;
      $this.options.forEach(function (item) {
        removeClass(item.element, "selected");
        if(sourceVal == item.data.value) {
          selectedItem = item;
        }
      });
      
      $this.selectedOptions.forEach(function (item) {
        removeClass(item.element, "selected");
      });
      
      selectedItem.element.classList.add("selected");
      $this.selectedOptions = [selectedItem];
      
      $this._renderSelectedItems();
      $this.updateSelectValue();
    }
  });
  
 //nice-select2 codes
 //....

``
@adkinsrs
Copy link

I think a feature like this would be very nice. I had to write a bunch of NiceSelect.prototype.update lines in my code to handle cases where the source value would change in the original select box, such as when the user chooses to load from a preexisting configuration file of data choices.

@ln8711
Copy link

ln8711 commented Sep 4, 2024

I found a workarround: get all the option of orignal element and set the attribute selected accordingly and then call the update() function

            let originalSelectElement = document.getElementById('originalSelectElementId');
            originalSelectElement.value = valueToSelect;  /* the value to update */
            
            /* loop the options and set selected or remove it */
            for (let index = 0; index < originalSelectElement.options.length; index++) {
                let option = originalSelectElement.options[index];

                if (option.value == valueToSelect) {
                    option.selected = true;
                    option.setAttribute("selected", "selected");
                } else {
                    option.selected = false;
                    option.removeAttribute("selected");
                }
            }
            
           /* call update to nice select element */
           niceSelectElement.update();

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

3 participants