【发布时间】:2020-05-19 14:21:15
【问题描述】:
我有一个表,用户可以选择两个选项,删除或保留。在第一列标题中,我有一个全部删除并保留所有单选按钮,它将选择dataSource 中的每一行。我希望它只选择当前页面上显示的行,而不是整个dataSource。现在的分页器大小是 5,但这最终会改变。
StackBlitz:https://stackblitz.com/edit/angular-qkx3pc
这是我的 HTML 代码:
<mat-drawer-container>
<mat-drawer-content>
<div class="tbl-container">
<mat-progress-bar mode="query" color="accent" *ngIf="isLoading" mode="indeterminate"></mat-progress-bar>
<table matSort mat-table [dataSource]="dataSource">
<ng-container matColumnDef="header-row-first-group">
<th mat-header-cell *matHeaderCellDef [style.text-align]="center" [attr.colspan]="2">
Review Response
</th>
</ng-container>
<ng-container matColumnDef="review">
<mat-header-cell fxFlex="230px" *matHeaderCellDef>
<mat-radio-button class="retain" (click)="retainAll()" style="padding-right: 5%;" [value]="0"
>Retain All</mat-radio-button
>
<mat-radio-button class="remove" (click)="removeAll()" [value]="1" color="warn"
>Remove All</mat-radio-button
>
</mat-header-cell>
<mat-cell fxFlex="230px" *matCellDef="let item" (click)="$event.stopPropagation()">
<mat-radio-group [value]="item.review">
<mat-radio-button
class="retain"
[checked]="retain.checked"
(change)="retain.checked = !retain.checked"
[value]="0"
>Retain</mat-radio-button
>
<mat-radio-button
class="remove"
[checked]="remove.checked"
(change)="retain.checked = !retain.checked"
style="margin-left: 20px;"
color="warn"
[value]="1"
>Remove</mat-radio-button
>
</mat-radio-group>
</mat-cell>
</ng-container>
<ng-container matColumnDef="mots_id">
<mat-header-cell *matHeaderCellDef mat-sort-header>Mots ID</mat-header-cell>
<mat-cell *matCellDef="let item">{{ item.mots_id }}</mat-cell>
</ng-container>
<ng-container matColumnDef="completed">
<mat-header-cell *matHeaderCellDef mat-sort-header>Completed</mat-header-cell>
<mat-cell *matCellDef="let item">{{ item.completed }}</mat-cell>
</ng-container>
...
<tr mat-header-row *matHeaderRowDef="['header-row-first-group']"></tr>
<mat-header-row *matHeaderRowDef="displayedColumns; sticky: true" class="tbl-header"></mat-header-row>
<mat-row
*matRowDef="let row; columns: displayedColumns"
(click)="helpdrawer.open(); selectedRow(row); highlight(row)"
[ngClass]="{ highlight: selectedRowIndex == row.uid }"
></mat-row>
</table>
...
</div>
...
</mat-drawer-content>
...
</mat-drawer-container>
这是我的 TS 代码:
class Component {
@ViewChild(MatPaginator) gridPaginator: MatPaginator;
@ViewChild(MatSort) gridSort: MatSort;
@ViewChild('ref') ref: any;
gridSettings = [
{ primaryKey: 'review', header: 'Review Response', render: 'both' },
{ primaryKey: 'mots_id', header: 'MOTS ID', render: 'both' },
{ primaryKey: 'app', header: 'App', render: 'both' },
{ primaryKey: 'env', header: 'Environment', render: 'both' },
{ primaryKey: 'profile', header: 'Profile', render: 'both' },
{ primaryKey: 'account_id', header: 'Account ID', render: 'both' },
{ primaryKey: 'owner_id', header: 'Owner ID', render: 'both' },
{ primaryKey: 'ccdr_read_only', header: 'Read Only', render: 'both' },
{ primaryKey: 'sec_layer', header: 'Security Layer', render: 'both' },
{ primaryKey: 'sec_image_id', header: 'Security Image ID', render: 'both' },
{ primaryKey: 'instance', header: 'Instance', render: 'both' },
{ primaryKey: 'data_source', header: 'DataSource', render: 'both' },
{ primaryKey: 'spi', header: 'SPI', render: 'both' },
{ primaryKey: 'mots_sox_indicator', header: 'SOX', render: 'both' },
];
dataSource = new MatTableDataSource<any>();
selection = new SelectionModel<any>(true, []);
displayedColumns = this.gridSettings.filter(a => a.render != 'export').map(b => b.primaryKey);
retain = {
checked: false,
};
remove = {
checked: false,
};
constructor(
private cdRef: ChangeDetectorRef,
private accessReviewDataService: AccessReviewDataService,
private userService: UserService,
public dialog: MatDialog,
) {
this.currentUser = this.userService.currentUser();
}
ngAfterContentInit() {
// init sort
this.gridSort.sort({ id: 'mots_id', start: 'desc', disableClear: false });
this.dataSource.sort = this.gridSort;
// init paging
this.dataSource.paginator = this.gridPaginator;
//this.cdRef.detach();
this.subscriptions.add(
this.accessReviewDataService.getAccessReview(this.currentUser.user_id).subscribe(
results => {
this.dataSource.data = results;
this.addId(results);
this.isLoading = false;
},
error => (this.isLoading = false),
),
);
}
retainAll() {
this.remove.checked = false;
this.retain.checked = true;
}
removeAll() {
this.retain.checked = false;
this.remove.checked = true;
}
}
现在它选择表格中的每一行,我只希望它选择页面上显示的行。任何帮助/方向将不胜感激。谢谢!
【问题讨论】:
-
你能为此创建一个 stackblitz 示例吗?
-
这需要一个从后端获取 id 的服务,并且数据来自后端,所以我不能在 stackblitz 上发布它
-
使用对象数组模拟数据
-
好的,@shashanksharma 我在这里创建了我的项目的 stackblitz 版本:stackblitz.com/edit/angular-qkx3pc 当您点击全部保留或全部删除并单击下一页时,它会选择整个数据源而不是当前页面显示的数据。如何为此添加逻辑?
标签: angular angular-material radio-button angular7 angular-material-table