primeng icon indicating copy to clipboard operation
primeng copied to clipboard

Dropdown preselection might fail if options initialized later

Open cagataycivici opened this issue 10 years ago • 13 comments

If options are initialized/changed later then the ngModel value, then preselection fails.

cagataycivici avatar Jul 23 '16 17:07 cagataycivici

I am still experiencing this issue with PrimeNG 4.2.0-rc.1. The object bound to the ngModel is set before the list is built. This results in the dropdown not showing the set value.

The scenario is that a user selects a ticket from a datatable on another tab. When this ticket is selected the full ticket data is loaded from the Database and set to the object bound to the dropdowns ngModel. The active tab is then changed to the tab that contains the form with the dropdown, and the component that handles this form's ngOninit initializes the dropdown list. If the tab that contains the form is not set to lazy the dropdown loads fine, and the selected value is displayed, but if it is lazy, meaning the list gets initialized when the tab is selected after the ngModel value has been set, the value is not displayed.

rhornrrs avatar Sep 08 '17 11:09 rhornrrs

What is the solution? Why was this closed?

broweratcognitecdotcom avatar May 04 '18 10:05 broweratcognitecdotcom

I believe @cagataycivici stated that it is a change detection issue, and he was unable to reproduce. I have yet to look back into the project I was having this issue to verify. Try installing loadash into your project, and use either clone(), or cloneDeep() on the collection bound to the dropdown.

rhornrrs avatar May 04 '18 10:05 rhornrrs

I was able to hack it to work.

broweratcognitecdotcom avatar May 04 '18 12:05 broweratcognitecdotcom

I am still experiencing this issue, any info on how to solve it?

TipsyFingers avatar Jul 27 '18 15:07 TipsyFingers

@broweratcognitecdotcom How did you fixed it?

NishantH-Data avatar Aug 06 '18 11:08 NishantH-Data

@NishantJava switch to reactive forms

broweratcognitecdotcom avatar Aug 06 '18 13:08 broweratcognitecdotcom

I managed to fix this by initializing "selected ngModel" in execution-complete of subscribe for Observable of "list ngModel". This will delay initializing selected value until list is fully initialized.

this.currencyList = [];
this.currencyService.getCurrencies().subscribe(
  currency => this.currencyList.push({ label: currency.code, value: currency }),
  error => MessageUtil.addError(this.messageService, 'Error in loading Currencies', error),
  () => this.getSelectedCurrency()
);

NishantH-Data avatar Aug 07 '18 15:08 NishantH-Data

this.currencyService.getAll().subscribe( (res: Array) => { this.currencyList = res.map(i => { return { label: i.currency, value: i.id } }); }, error => console.log);

amuratgencay avatar Feb 18 '20 13:02 amuratgencay

The array reference needs to change as @amuratgencay did. You should not initialize and push the array. You should set each time with the value from the service to change reference value to detect the changes.

this.currencyService.getCurrencies().subscribe(
  currency => this.currencyList.map(i => { return { label: currency.code, value: currency } }),
  error => MessageUtil.addError(this.messageService, 'Error in loading Currencies', error),
  () => this.getSelectedCurrency()
);

Here is an example https://stackblitz.com/edit/angular6-primeng-eqttey

gokhancelik avatar Feb 18 '20 20:02 gokhancelik

Work for me. maybe help u. ////ts categories: any []; ..... this.itemService.getAllCategories().subscribe((res: any) => { this.categories = res.map((i: any) => ({label: i.category_nm, value : i.id}) ); ////// html <p-dropdown [options]="categories" formControlName="category_id" showClear='true' placeholder="Select a cotegory" [ngClass]="{ 'is-invalid': submitted && f.category_id.errors }">

RembertoP avatar Mar 20 '20 00:03 RembertoP

Maybe it's too late, but... Check if you are getting a mismatch problem. Ex: previously selected: {value: '1', label: 'Test'} (value is a string) arrayOfOptions: {value: 1, label: 'Test'} (value is an integer)

deusmar avatar Oct 06 '20 19:10 deusmar

Maybe it's too late, but... Check if you are getting a mismatch problem. Ex: previously selected: {value: '1', label: 'Test'} (value is a string) arrayOfOptions: {value: 1, label: 'Test'} (value is an integer)

@deusmar Lifesaver. Thank you so much!

MladenBogomirov avatar Sep 21 '21 14:09 MladenBogomirov