简体   繁体   中英

Column not showing - mat-table

I am attempting to add row-deletion into my <mat-table> . Most of the table is working fine, except the checkbox column is not showing up. Previously, the code did not contain the <!-- Checkbox/Selection Column --> section. I added it in using one of the examples on angular.material.io.

Im a bit confused, because it looks to me like the pattern should be a <th> & <td> wrapped inside an <ng-container> . I seem to be missing something here, because the checkbox/selection column isn't showing up, there aren't any errors in the browser console, and there aren't any errors being thrown at ng serve in powershell.

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

    <!-- Checkbox/Selection Column -->
    <ng-container matColumnDef="select">
        <th mat-header-cell *matHeaderCellDef>
            <mat-checkbox [checked]="false" [aria-label]="checkboxLabel()">
            </mat-checkbox>
        </th>
        <td mat-cell *matCellDef="let row">
            <mat-checkbox [checked]="true"></mat-checkbox>
        </td>
    </ng-container>

    <!-- Working Columns -->
    <ng-container *ngFor="let field of Fields" matColumnDef="{{field.name}}">
        <th class="w-134" mat-header-cell *matHeaderCellDef mat-sort-header>{{field.name}}</th>
        <td class="w-130" mat-cell *matCellDef="let row">
            <input *ngIf="field.editType == 'free'" matInput [(ngModel)]="row[field.name]" placeholder="{{row[field.name]}}" required>
            <mat-select *ngIf="field.editType == 'spinner'" [(ngModel)]="row[field.name]">
                <mat-option *ngFor="let option of field.options" [value]="option">{{option}}</mat-option>
            </mat-select>
            <div class="checkbox-cell" (click)="toggleBoolean(row, field.name)">
                <mat-checkbox *ngIf="field.editType == 'checkbox'" [(ngModel)]="row[field.name]" (click)="toggleBoolean(row, field.name)"></mat-checkbox>
            </div>
        </td>
    </ng-container>

    <tr mat-header-row *matHeaderRowDef="ColumnHeaders"></tr>
    <tr mat-row *matRowDef="let row; columns: ColumnHeaders;"></tr>
</table>

Seems like you did not added the column in your ColumnHeaders array inside TS file.

ColumnHeaders = ['select'] //ColumnHeaders should have an entry of select(your checkbox column defintion)

yes you have to add select inside displayedColumns

  displayedColumns: string[] = ['select','position', 'name','gender', 'weight', 'symbol'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM