Angular 7 and Angular Material table with data source error when generating drop list form control for each row

大憨熊 提交于 2019-12-25 03:34:34

问题


Based on this question on stack, I need to bind a drop down list, for each row generated from angular material table dataSource. The drop down list should select automatically the correspondent value, as other values.

This is table:

<form [formGroup]="formGroup">
    <table mat-table [dataSource]="dataSource" matSort>


      <!-- <ng-container matColumnDef="ind_id">
        <th mat-header-cell *matHeaderCellDef> Ind. ID </th>
        <td mat-cell *matCellDef="let element"> {{element.iid}} </td>
      </ng-container> -->


      <ng-container matColumnDef="name">
        <th mat-header-cell *matHeaderCellDef> Name </th>
        <td mat-cell *matCellDef="let element" [matTooltip]="element.iid" matTooltipPosition="left"> <b>{{element.far}} {{element.lar}}</b> </td>

      </ng-container>


      <ng-container matColumnDef="head_hh">
        <th mat-header-cell *matHeaderCellDef> Head Of HH </th>
        <td mat-cell *matCellDef="let element"> {{element.hhead}} </td>
      </ng-container>


      <ng-container matColumnDef="fr">
        <th mat-header-cell *matHeaderCellDef> Family Rel. </th>
        <td mat-cell *matCellDef="let element; let index = index;" [formGroupName]="index">
            <mat-form-field color="warn" appearance="outline">
                <mat-label>Family Relation</mat-label>
                <mat-select id="family_relation" formControlName="fr" placeholder="Family Relation">                 
                      <mat-option *ngFor="let familyRelation of familyRelationArray; let i = index" [value]="frid">
                    {{familyRelation.family_relation_type}}
                  </mat-option>
                </mat-select>
              </mat-form-field>&nbsp;
              {{element.fr}}
        </td>
      </ng-container>

      <ng-container matColumnDef="ms">
        <th mat-header-cell *matHeaderCellDef> Marital Sts. </th>
        <td mat-cell *matCellDef="let element"> {{element.ms}} </td>
      </ng-container>
      <ng-container matColumnDef="status">
        <th mat-header-cell *matHeaderCellDef> Status </th>
        <td mat-cell *matCellDef="let element"> {{element.sts}} </td>
      </ng-container>
      <ng-container matColumnDef="age">
        <th mat-header-cell *matHeaderCellDef mat-sort-header> Age </th>
        <td mat-cell *matCellDef="let element"> {{element.age}} </td>
      </ng-container>

      <tr mat-header-row *matHeaderRowDef="displayedColumns; sticky: true"></tr>
      <tr mat-row *matRowDef="let row; columns: displayedColumns;"></tr>
    </table>
</form>

Here is the typescript code, where I generate a FormArray based on this working stackblitz from the question attached above:

constructor(private fb: FormBuilder, 
    private activatedRoute: ActivatedRoute, 
    private auth: AuthApiService) {
      this.formGroup = this.fb.group({
        'fr': this.createArray()
      })
}

Now for each formControl of the array, I need to bind the data to drop list:

createArray(): FormArray{
    return new FormArray(this.dataSource.data.map(item=> new FormGroup({
      fr: new FormControl(item.frid)
    })));
}

Where frid is the value of each selected option of drop list, and fr is the name displayed for user.

I am having the following error:

[ts] Property 'frid' does not exist on type '{}'. [2339]

Here a snippet of the returned array:

0: {0: "xyzzz", 1: "xyz", 2: 57073, 3: 2, 4: "Parent", 5:

"Married", 6: "Active", 7: "no",…}

0: "xyz"

1: "zyz"

2: 57073

3: 2

4: "Parent"

5: "Married"

6: "Active"

7: "no"

8: 61

age: 61

far: "xyz"

fr: "Parent"

frid: 2

hhead: "no"

iid: 57073

lar: "xyz"

ms: "Married"

sts: "Active"

Here is my stackblitz.

How to generate form control in each row of data source in angular material 6 table ?


回答1:


You should pass the Type to MatTableDataSource.

dataSource = new MatTableDataSource<any>(); //<-- any which accept all kind of classes.

OR : recommended one

dataSource = new MatTableDataSource<Type>(); //<--- Type will be class or interface



回答2:


Or you can simply

createArray(): FormArray{
    return new FormArray(this.dataSource.data.map(item=> new FormGroup({
      fr: new FormControl(item['frid'])
    })));
}


来源:https://stackoverflow.com/questions/53610111/angular-7-and-angular-material-table-with-data-source-error-when-generating-drop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!