0

I have a list of items. I can add to this list and also edit this(Problem here)
When I click to the element there is dialog box with reactive form.
Adding working correct, but when I try to edit multiple choice items are empty.
Here code when adding :

this.form = this.fb.group({
    lastName: this.fb.control(''),
    firstName: this.fb.control(''),
    secondName: this.fb.control(''),
    fio: this.fb.control(''),
    iin: this.fb.control('', [Validators.required, Validators.pattern(/^(\d{12})$/)]),
    email: this.fb.control('', [Validators.required, Validators.email,]),
    status: this.fb.control(''),
    systems: this.fb.control(''),
    roles: this.fb.control(''),
    organization: this.fb.control(''),
    userName: this.fb.control(''),
    newPassword: this.fb.control(''),
    newPassword2: this.fb.control(''),
  })

Here when editing (user is object where contains all user details) :

this.form = this.fb.group({

    lastName: user.fio[0],
    firstName: this.fb.control(user.fio[1]),
    secondName: this.fb.control(user.fio[2]),
    iin: this.fb.control(user.iin, [Validators.required, Validators.pattern(/^(\d{12})$/)]),
    email: this.fb.control(user.email, [Validators.required, Validators.email,]),
    status: this.fb.control(user.status),
    systems: this.fb.control(user.systems),
    roles: this.fb.control(user.roles),
    organization: this.fb.control(user.organization),
    userName: this.fb.control(user.userName),
    newPassword: this.fb.control(user.newPassword),
    newPassword2: this.fb.control(user.newPassword2),
    id: this.fb.control(user.id),

  })

When I open form lastname firstname fio shows but multiple choice items are not showing.

Notice : When I console log fb.conrols('roles') for example. It has values I set.

enter image description here

Here's my html template :

<mat-form-field class="role__input">
    <mat-label>{{ 'Роль' | translate }}</mat-label>
    <mat-select #mySelect formControlName="roles" required multiple>
      <mat-label class="role_label">{{ 'ЕНСИ' | translate }}</mat-label>
      <mat-option *ngFor="let role of roles.roleEnsi" [value]="role">
        {{role.nameRu}}
      </mat-option>
      <mat-label class="role_label">{{ 'ФОРМИРОВАНИЕ  СРЕЗОВ' | translate }}</mat-label>
      <mat-option *ngFor="let role of roles.roleFs" [value]="role">
        {{role.nameRu}}
      </mat-option>
      <mat-label class="role_label">{{ 'АДМИНИСТРАТОР' | translate }}</mat-label>
      <mat-option *ngFor="let role of roles.roleAdmin" [value]="role">
        {{role.nameRu}}
      </mat-option>
    </mat-select>
  </mat-form-field>
6
  • What do you mean by multiple choice not showing ? Commented Dec 10, 2019 at 11:32
  • multiple choice selects are empty, even if I set them Commented Dec 10, 2019 at 11:52
  • Please provide the .html, because your problem obviously hasn't something to do with your ts file Commented Dec 10, 2019 at 12:10
  • I added html template Commented Dec 10, 2019 at 12:13
  • 1
    In mat-option: What definition has roles.roleFs? Is it the same as user.roles? Commented Dec 10, 2019 at 12:17

6 Answers 6

1

Try using patchValue

this.form.patchValue(user);

Working Demo

Sign up to request clarification or add additional context in comments.

1 Comment

roles: [user.roles] is an array, not a string.
1

Change your formControl roles to roles: this.fb.control([]), because I has an array as data.

You can set the data by:

this.form.setValue(object);

If you use a mat-select, then you can put the multiple options like so:

 <mat-form-field>
    <mat-label>Roles</mat-label>
    <mat-select formControlName="roles">
      <mat-option *ngFor="let role of user.roles" [value]="role">
        {{ role }}
      </mat-option>
    </mat-select>
  </mat-form-field>

9 Comments

No adding new user works perfectly, editing also. But when I edit it doesn't show values of user in multiple selects
I miss the context. What do you mean with edit it? And what do you mean with multiple selects?
I click to my list, form appears again. And this form must be filled with selected user data to edit.
It doesn't show, because your formField is a string type, not array.
How does your .html look like?
|
0

Try like this

this.form.patchValue({
    lastName: user.fio[0],
    firstName: user.fio[1],
    secondName: user.fio[2],
    iin: user.iin
    email: user.email,
    status: user.status,
    systems: user.systems,
    roles: user.roles,
    organization: user.organization,
    userName: user.userName,
    newPassword: user.newPassword,
    newPassword2: user.newPassword2,
    id: user.id
  });

OR if user json and form model structure is same the simply try like this

this.form.patchValue(user);

Comments

0

Set the new values through the form control

roles: this.fb.control(user.roles);

this.form = this.fb.group({

    lastName: [user.fio[0]],
    firstName: this.fb.control(user.fio[1]),
    secondName: this.fb.control(user.fio[2]),
    iin: this.fb.control(user.iin, [Validators.required, Validators.pattern(/^(\d{12})$/)]),
    email: this.fb.control(user.email, [Validators.required, Validators.email,]),
    status: this.fb.control(user.status),
    systems: this.fb.control(user.systems),
    roles: this.fb.control(user.roles),
    organization: this.fb.control(user.organization),
    userName: this.fb.control(user.userName),
    newPassword: this.fb.control(user.newPassword),
    newPassword2: this.fb.control(user.newPassword2),
    id: this.fb.control(user.id),

  })

2 Comments

I've been doing this all the time, but selects are empty
Can you add the HTML
0

Try this way : consider userRoles is your array list of roles with ids.

in .ts

this.form.patchValue({
  roles:user.role_id
});

in .html

<select formControlName="roles" class="form-control">
        <option [ngValue]="role.role_id" *ngFor="let role of userRoles"> {{role.role_name}}</option>
</select>

Comments

0

Thank you for all answers, I solved this question today.
Problem was about objects. I set object to [value] so after retrieving data from database objects were not equal .
Two different objects (even if they both have zero or the same exact properties) will never compare equally. If you need to compare the equality of two object's properties.
Sooo I changed structure to accept id and transform id to object when sending to backend. It worked.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.