-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
NbSelect doesn't display the [selected] option if data is dynamic #2145
Comments
Same here. If I use the standard HTML select, it updates without problems. |
until it´s fixed: setting "selected" in |
I was facing exactly the same issue also yesterday. If you are using FormControls you can set the value of the nb-select in your component and it will work. |
any news on this? |
Can you please your code snippet?? because I have tried anything and it still doesn't work |
Are you using nb-select & nb-option with dynamic data ? |
Is there any workaround on this? |
@michabbb that works when initially setting the select right after component loads, however after a while I also have to change its value from outside (from another server request) and at that time the component doesn't show the updated value (even if the correct item is selected when opening the dropdown) |
The only workaround that worked for me is using the protected member selectOption which takes an instance of NbOptionComponent object, look at the following example:
this will work for both single and multiple select as follows: The weirdest thing is that the only working function is protected! and to overcome this issue you can use dynamic property access with bracket notation, like so |
I have used @shireefadel's workaround as follows:
This is extremely dirty, but it will work until this bug is fixed. |
thanks, it helped me a lot to create a generic function
I hope it works for someone else. |
The issue is still present in nebular 8. Any news? |
Still no solution? The workarounds don't seem to work. |
This will work in the html, add a #select on the nb-select element.
in the ts, add a ViewChild to you variables.
add this function
and call the function giving it the default value when you subscribe
|
@ettouzany That does not work (or at least it did not for me) if the data is set using Angular's |
The issue seems to be due to the component not re-evaluating the selected value when the options are updated dynamically after the value is assigned somehow. My workaround was to clear the value and then set it again with a deferred call (setTimeout with 0 duration) and calling changeDetectorRef.markForCheck(). Something like this:
|
I ran into this issue when I was dynamically updating the underlying <nb-select formControlName="selectedOption">
<nb-option *ngFor="let option of options" [value]="option">{{ option }}</nb-option>
</nb-select> this.options = newOptions;
this.formGroup.controls.selected.setValue(newOptions[0]); Solution this.options = newOptions;
setTimeout(() => this.formGroup.controls.selected.setValue(newOptions[0])); Explanation |
Thank you jrasm91, I think this is the best explanation of the problem, took me a while to reach the same conclusion.
|
Works for me, many thanks |
My hero ! this works! setTimeout(() => {
let correctFormField: AbstractControl = this.userSchemaDataForm.controls['role'];
correctFormField.setValue("admin")
}); |
Issue type
I'm submitting a ... (check one with "x")
Issue description
Current behavior:
I have a NbSelect populated with options from the database. Neither using [selected] or ([ngModel]) makes it display the selected value. As soon as I pass static data it works as intended.
Expected behavior:
It should automatically select the selected option.
Steps to reproduce:
Put NbSelect on page, have a simple MySQL database to support the exchange of data, take the data and put them as options.
Related code:
Unfortunately since it requires my localhost mySQL here, I can't provide a stackblitz, but I can include some snippets.
header.component.html
header.component.ts
header.service.ts
server.service.ts
Now, in the last snippet, the two rows return the same Observable. In the caso of the second (static data) it displays the [selected] value correctly, in the first it doesn't.
I'm not sure it's related to the issue 2088, so I started a new issue
#2088
Other information:
npm, node, OS, Browser
Angular, Nebular
The text was updated successfully, but these errors were encountered: