【发布时间】:2019-12-30 14:47:40
【问题描述】:
我正在使用<mat-radio-button> 列表。当我首先单击然后单击第二个而不是单击第一个返回时。当我单击第一个单选按钮时,它不起作用。然后当我点击删除按钮时,它会删除第一个和第二个。
我提供了下面的代码和Demo 链接供您参考。
HTML
<div class="example-container mat-elevation-z8">
<mat-chip class="pointer" mat-raised-button color="primary" (click)="removeSelectedRows()">
Remove Selected Rows
</mat-chip>
<mat-table #table [dataSource]="dataSource">
<!-- Checkbox Column -->
<ng-container matColumnDef="select">
<mat-header-cell *matHeaderCellDef>
<!-- <mat-checkbox (change)="$event ? masterToggle() : null"
[checked]="selection.hasValue() && isAllSelected()"
[indeterminate]="selection.hasValue() && !isAllSelected()">
</mat-checkbox> -->
</mat-header-cell>
<mat-cell *matCellDef="let row">
<mat-radio-button (click)="$event.stopPropagation()"
(change)="$event ? selection.toggle(row) : null"
[checked]="selection.isSelected(row)">
</mat-radio-button>
</mat-cell>
</ng-container>
<!-- Position Column -->
<ng-container matColumnDef="position">
<mat-header-cell *matHeaderCellDef> No. </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.position}} </mat-cell>
</ng-container>
<!-- Name Column -->
<ng-container matColumnDef="name">
<mat-header-cell *matHeaderCellDef> Name </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.name}} </mat-cell>
</ng-container>
<!-- Weight Column -->
<ng-container matColumnDef="weight">
<mat-header-cell *matHeaderCellDef> Weight </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.weight}} </mat-cell>
</ng-container>
<!-- Symbol Column -->
<ng-container matColumnDef="symbol">
<mat-header-cell *matHeaderCellDef> Symbol </mat-header-cell>
<mat-cell *matCellDef="let element"> {{element.symbol}} </mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns;"
(click)="selection.toggle(row)">
</mat-row>
</mat-table>
<mat-paginator #paginator
[pageSize]="3"
[pageSizeOptions]="[5, 10, 20]"
>
</mat-paginator>
</div>
组件
displayedColumns = ['select', 'position', 'name', 'weight', 'symbol'];
data = Object.assign( ELEMENT_DATA);
dataSource = new MatTableDataSource<Element>(this.data);
selection = new SelectionModel<Element>(true, []);
@ViewChild(MatPaginator) paginator: MatPaginator;
ngAfterViewInit() {
this.dataSource.paginator = this.paginator;
}
constructor(){
console.log(this.data);
}
/** Whether the number of selected elements matches the total number of rows. */
isAllSelected() {
const numSelected = this.selection.selected.length;
const numRows = this.dataSource.data.length;
return numSelected === numRows;
}
removeSelectedRows() {
console.log(this.selection.selected)
this.selection.selected.forEach(item => {
let index: number = this.data.findIndex(d => d === item);
console.log(this.data.findIndex(d => d === item));
this.dataSource.data.splice(index,1);
this.dataSource = new MatTableDataSource<Element>(this.dataSource.data);
});
this.selection = new SelectionModel<Element>(true, []);
console.log(this.selection)
}
/** Selects all rows if they are not all selected; otherwise clear selection. */
masterToggle() {
this.isAllSelected() ?
this.selection.clear() :
this.dataSource.data.forEach(row => this.selection.select(row));
}
}
感谢和问候。
【问题讨论】:
-
试试 this.dataSource.data = this.dataSource.data.splice(index,1); / splice 返回一个值,它不会修改你的实际对象
-
无论如何,最好不要直接对dataSource进行操作,您可以创建一个新的obj并最终将其分配给您的dataSource。:let source = this.dataSource.data;让 index = source.findIndex(d => d === item); source = source.splice(index, 1); this.dataSource.data = 源;
-
感谢您的评论...我会尝试..
-
您不应该改用
mat-checkbox吗?是否要限制用户同时删除多行?你到底为什么使用mat-radio-button? -
@nash11 是的..我想限制用户同时删除多行..一次只能选择一行
标签: angular typescript angular-material radio-button