Harvest-Dev / ng-select2

A select2 for Angular
MIT License
52 stars 32 forks source link

Reset Value Single Select Undefined #42

Closed DavidSegura closed 1 year ago

DavidSegura commented 1 year ago

Hi :D

I am implementing a dynamic component for some filters and I am using the ng-select 2 library.

The problem I have is the following, I declare a reactive form:

`private ngFormBuild(): void { let formFilter: FormControl[] = [];

this.filters.forEach((filter: FilterTable) => {
  if (filter.type === FILTERS_TYPE.select) {
    formFilter[filter.label as any] = new FormControl(0);
  }

  if(filter.type === FILTERS_TYPE.select_multi) {
    formFilter[filter.label as any] = new FormControl([]);
  }

  if(filter.type === FILTERS_TYPE.text) {
    formFilter[filter.label as any] = new FormControl("");
  }
});

this.form = this.fb.group(formFilter);

} ` Then in the template I implement the selector:

` <ng-container *ngIf="f.type == FiltersEnum.select"> <select2 [attr.id]="f.label" [data]="ngGetSelect(f.label)" [resettable]="true" formControlName="{{ f.label }}" (update)="ngSubmit()"

` When I load the view, the simple selector filter loads me with a default value, so far everything is correct.

imagen

Then I select a value in the selector:

imagen

And now when I delete the selection, the filter changes to undefined.

imagen

I don't know if I'm doing something wrong. But shouldn't it return to the value of the FormControl?

imagen

Thank you very much for your time

Zefling commented 1 year ago

Sorry, but I'm not sure I understand the problem. :(

Zefling commented 1 year ago

After rereading the problem. I understand better. Sorry.

You want an × to reset the value to the original one. But what you are asking is a special case. If the input value is a value like 2, the × will no longer be consistent.

There should be an added option that resets to a given value.