Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

Bind data to checkbox in angular material table using formcontrol. Data is available as list of true,false values

HTML

<ng-container matColumnDef="view">
    <mat-header-cell *matHeaderCellDef mat-sort-header>View</mat-header-cell>
       <mat-cell *matCellDef="let element; let i=index"> 
          <mat-checkbox formControlName="view" (change)=updateCheckedList(element)>
         </mat-checkbox>
       </mat-cell>
</ng-container>  ```

**TS**

view = [true,false,false,true]

createRoleForm(): FormGroup
  {
      return this._formBuilder.group({
          id              : [this.role.id],
          name            : [this.role.name],
         // view : 

      });
  }

I need to check the check boxes which are in the material table(view column) according to values of view list.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

you cant do like this because your form Controller is having only one control fot the view so you need to you ng model it helps to you like this

    <ng-container matColumnDef="view">
        <mat-header-cell *matHeaderCellDef mat-sort-header>View</mat-header-cell>
           <mat-cell *matCellDef="let element; let i=index"> 
              <mat-checkbox [(ngModel)]="element.checked" 
               [ngModelOptions]="{standalone: true}" 
               (change)=updateCheckedList(element)>
             </mat-checkbox>
           </mat-cell>
</ng-container>

inside the element use create a attribute called checked that's it

way 2 - if you want to do with an form controlled you need to use form array that's it


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...