gpt4 book ai didi

angular - 复选框未出现在表数据 Angular 7 中

转载 作者:行者123 更新时间:2023-12-05 08:40:08 26 4
gpt4 key购买 nike

我将 Angular 7 与数据可以扩展的 Angular Material 表一起使用。我试图在表格左侧的每行中放置一个复选框,但它没有出现:

<table mat-table
[dataSource]="dataSource" multiTemplateDataRows
class="mat-elevation-z8">
<!-- Checkbox Column -->
<ng-container matColumnDef="select">
<th mat-header-cell *matHeaderCellDef>
<mat-checkbox (change)="$event ? masterToggle() : null"
[checked]="selection.hasValue() && isAllSelected()"
[indeterminate]="selection.hasValue() && !isAllSelected()">
</mat-checkbox>
</th>
<td mat-cell *matCellDef="let row">
<mat-checkbox (click)="$event.stopPropagation()"
(change)="$event ? selection.toggle(row) : null"
[checked]="selection.isSelected(row)">
</mat-checkbox>
</td>
</ng-container>
<ng-container matColumnDef="{{column}}" *ngFor="let column of columnsToDisplay">
<th mat-header-cell *matHeaderCellDef> {{column}}</th>
<td mat-cell *matCellDef="let element"> {{element[column]}} </td>
</ng-container>

<!-- Expanded Content Column -->
<ng-container matColumnDef="expandedDetail">
<td mat-cell *matCellDef="let element" [attr.colspan]="columnsToDisplay.length">
<div class="example-element-detail"
[@detailExpand]="element == expandedElement ? 'expanded' : 'collapsed'">
<div class="example-element-diagram">
<div class="example-element-position"> {{element.position}} </div>
<div class="example-element-symbol"> {{element.symbol}} </div>
<div class="example-element-name"> {{element.name}} </div>
<div class="example-element-weight"> {{element.weight}} </div>
</div>
<div class="example-element-description">
{{element.description}}

</div>
</div>
</td>
</ng-container>

<tr mat-header-row *matHeaderRowDef="columnsToDisplay"></tr>
<tr mat-row *matRowDef="let element; columns: columnsToDisplay;"
class="example-element-row"
[class.example-expanded-row]="expandedElement === element"
(click)="expandedElement = expandedElement === element ? null : element || selection.toggle(row)">
</tr>
<tr mat-row *matRowDef="let row; columns: ['expandedDetail']" class="example-detail-row"></tr>
</table>

作为引用,您可以在此处找到完整代码 Stackblitz .

最佳答案

您的复选框列未包含在 columnsToDisplay 数组中。将它添加到那里,它就会显示出来。

注意:当您遍历 columnsToDisplay 时,只需添加 'select' 列就会引发错误(您已经在模板中单独定义了它)。您还需要将表定义更改为如下内容:

组件

columnsToDisplay = ['select', 'name', 'weight', 'symbol', 'position'];

模板

<ng-container matColumnDef="{{column}}" *ngFor="let column of columnsToDisplay">
<ng-container *ngIf="column === 'select'; else notSelect">
<th mat-header-cell *matHeaderCellDef>
<mat-checkbox (change)="$event ? masterToggle() : null"
[checked]="selection.hasValue() && isAllSelected()"
[indeterminate]="selection.hasValue() && !isAllSelected()">
</mat-checkbox>
</th>
<td mat-cell *matCellDef="let row">
<mat-checkbox (click)="$event.stopPropagation()"
(change)="$event ? selection.toggle(row) : null"
[checked]="selection.isSelected(row)">
</mat-checkbox>
</td>
</ng-container>
<ng-template #notSelect>
<th mat-header-cell *matHeaderCellDef> {{column}}</th>
<td mat-cell *matCellDef="let element"> {{element[column]}} </td>
</ng-template>
</ng-container>

Check out the edited stackblitz here.

关于angular - 复选框未出现在表数据 Angular 7 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57049197/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com