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

Dropdown preselection might fail if options initialized later #651

Closed
cagataycivici opened this issue Jul 23, 2016 · 13 comments
Closed

Dropdown preselection might fail if options initialized later #651

cagataycivici opened this issue Jul 23, 2016 · 13 comments
Assignees
Labels
Type: Bug Issue contains a bug related to a specific component. Something about the component is not working
Milestone

Comments

@cagataycivici
Copy link
Member

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

@cagataycivici cagataycivici added the Type: Bug Issue contains a bug related to a specific component. Something about the component is not working label Jul 23, 2016
@cagataycivici cagataycivici added this to the 1.0.0-beta.11 milestone Jul 23, 2016
@cagataycivici cagataycivici self-assigned this Jul 23, 2016
@rhornrrs
Copy link

rhornrrs commented Sep 8, 2017

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.

@broweratcognitecdotcom
Copy link

What is the solution? Why was this closed?

@rhornrrs
Copy link

rhornrrs commented May 4, 2018

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.

@broweratcognitecdotcom
Copy link

I was able to hack it to work.

@TipsyFingers
Copy link

TipsyFingers commented Jul 27, 2018

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

@NishantJava
Copy link

@broweratcognitecdotcom
How did you fixed it?

@broweratcognitecdotcom
Copy link

@NishantJava switch to reactive forms

@NishantJava
Copy link

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

@amuratgencay
Copy link

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

@gokhancelik
Copy link

gokhancelik commented Feb 18, 2020

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

@RembertoP
Copy link

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

@deusmar
Copy link

deusmar commented Oct 6, 2020

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)

@MladenBogomirov
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Issue contains a bug related to a specific component. Something about the component is not working
Projects
None yet
Development

No branches or pull requests

10 participants