gpt4 book ai didi

angular - 提供了重复的列名 : Angular Material Data Table

转载 作者:行者123 更新时间:2023-12-05 01:13:45 24 4
gpt4 key购买 nike

出于某种原因,我在为我的数据表创建复选框列时遇到错误。

我了解控制台的错误,但我该怎么做才能修复它?

view.component.html

<mat-card-content>
<div class="view-container mat-elevation-z8">
<table mat-table [dataSource]="dataSource" matSort>

<ng-container matColumnDef="column" *ngFor="let column of displayedColumns">
<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 action">
<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 displayedColumns">
<th mat-header-cell *matHeaderCellDef mat-sort-header>
{{ column }}
<mat-icon aria-hidden="false" aria-label="filter icon">more_horiz</mat-icon>
</th>
<td mat-cell *matCellDef="let action">{{ action[column] }}
</td>
</ng-container>

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

<mat-paginator [pageSizeOptions]="pageSizeOptions" showFirstLastButtons></mat-paginator>
</div>

view.component.ts

displayedColumns: string[] = [];

const displayedColumns = this.viewData.Columns.map((c: { Name: any; }) => c.Name);
displayedColumns[2] = 'Folder1';
this.displayedColumns = displayedColumns;
// tslint:disable-next-line: max-line-length
const fetchedData = this.viewData.DataRows.map((r: { slice: (arg0: number, arg1: number) => { forEach: (arg0: (d: any, i: string | number) => any) => void; }; }) => {
const row = {};
r.slice(0, 9).forEach((d: any, i: string | number) => (row[this.displayedColumns[i]] = d));
return row;
});

最佳答案

我认为您的问题是您在 matColumnDef 中多次使用 displayedColumns 中的每个值首先在这里:

...
<ng-container matColumnDef="column" *ngFor="let column of displayedColumns">
<th mat-header-cell *matHeaderCellDef>
<mat-checkbox (change)="$event ? masterToggle() : null"
[checked]="selection.hasValue() && isAllSelected()"
[indeterminate]="selection.hasValue() && !isAllSelected()">
</mat-checkbox>
...

然后在这里:

...
<ng-container [matColumnDef]="column" *ngFor="let column of displayedColumns">
<th mat-header-cell *matHeaderCellDef mat-sort-header>
{{ column }}
<mat-icon aria-hidden="false" aria-label="filter icon">more_horiz</mat-icon>
</th>
<td mat-cell *matCellDef="let action">{{ action[column] }}
</td>
</ng-container>
...

事实上,如果您需要每行一个复选框,则不需要为每个 displayedColumns 创建一个 mat-checkbox

这里我们有 Angular Material 的例子: https://material.angular.io/components/table/overview#selection

因此,您应该进行以下更改:

1) 将select列添加到displayedColumns:

view.component.ts

displayedColumns: string[] = [];

const displayedColumns = this.viewData.Columns.map((c: { Name: any; }) => c.Name);
displayedColumns[2] = 'Folder1';
this.displayedColumns = ['select'].concat(displayedColumns);
// tslint:disable-next-line: max-line-length
const fetchedData = this.viewData.DataRows.map((r: { slice: (arg0: number, arg1: number) => { forEach: (arg0: (d: any, i: string | number) => any) => void; }; }) => {
const row = {};
r.slice(0, 9).forEach((d: any, i: string | number) => (row[this.displayedColumns[i]] = d));
return row;
});

2)第一列(checkbox)应该是select并且它不应该包含在其他列的*ngFor中:

view.component.html

<mat-card-content>
<div class="view-container mat-elevation-z8">
<table mat-table [dataSource]="dataSource" matSort>

<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 displayedColumns | slice:1 ">
<th mat-header-cell *matHeaderCellDef mat-sort-header>
{{ column }}
<mat-icon aria-hidden="false" aria-label="filter icon">more_horiz</mat-icon>
</th>
<td mat-cell *matCellDef="let action">{{ action[column] }}
</td>
</ng-container>

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

<mat-paginator [pageSizeOptions]="pageSizeOptions" showFirstLastButtons></mat-paginator>
</div>

现在它应该可以正常工作了。

关于angular - 提供了重复的列名 : Angular Material Data Table,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59901088/

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