您必须添加
[(selection)]="selectedRow"
在
p-table
要将所选行绑定到已定义的变量,请执行以下操作:
<p-table [value]="tableData" selectionMode="single" metaKeySelection = "true" [(selection)]="selectedRow">
<ng-template pTemplate="header">
<tr>
<th>Company</th>
<th>First Name</th>
<th>Email</th>
</tr>
</ng-template>
<ng-template pTemplate="body" let-rowData let-rowIndex="rowIndex">
<tr [pSelectableRow]="rowData">
<td>{{rowData.first_name}}</td>
<td>{{rowData.last_name}}</td>
<td>{{rowData.email}}</td>
</tr>
</ng-template>
</p-table>
<button (click)= "removeSelection()">Remove Selection</button>
并添加
this.selectedRow = null;
在
removeSelection()
方法:
removeSelection() {
this.selectedRow = null;
}