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

When data changes the value on select gets overriden with the first value in the 'data' list #136

Open
igimenez opened this issue Sep 13, 2018 · 6 comments

Comments

@igimenez
Copy link

igimenez commented Sep 13, 2018

Hi guys,

I am having a problem. When my select2 is populated with countries the valueChanged is being triggered with the first element of the countries list. Doing some digging I found that when the countries collection changes (from [] to a collection of countries), the whole select is distroyed and a new is created. And the newly created loads the first element of the data list as its value.

<select2 #countryInput="ngModel" name="country" [data]="countries" [width]="'100%'" [options]="selectOptionsCountry" (valueChanged)="formObj.countryCode = $event.value" [(ngModel)]="formObj.countryCode" ngDefaultControl required> <option></option> </select2>

Does anybody have found a workaround for this??

Btw, the workaround I find is to make sure the countries are loaded before the formObj I am editing, but that's kinda hackky, looking for a better solution.

Thanks

@hassegawa
Copy link

I have the same problem, some solution?

@senhorlopes
Copy link

I have the same problem, any way to solve?

@LucasLopesr
Copy link

I have the same problem

@hassegawa
Copy link

@hassegawa
Copy link

the pr #134 solved it.

@redcricket
Copy link

redcricket commented Apr 30, 2019

until hassegawa's fix gets merged in I found a workaround solution that may be useful. I set my processResults option to this:

'processResults': function (data) {
          var results = [];
          const epochTime = (new Date).getTime();
          // work around for issue https://github.com/NejcZdovc/ng2-select2/issues/136 (use epoch time to get unique ids.
          for (var i = 0; i < data.results.length; i++ ) { results.push( { "id": i + epochTime, "text": data.results[i]} ); }
          return { 'results': results };
}

The above snippet makes use of epoch time to ensure that each option is given a unique id.

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

5 participants