[英]How to remove selection from table?
我已經為此苦了一段時間了。 我有一個簡單的表,當我選擇一行時,它就會被選中 。 但是我想為按鈕添加一個功能( remove Selection ),所以當我單擊它時,我想從行中刪除選擇。 有人可以告訴我我所缺少的嗎? 在此先多謝!
這是我的代碼: LIVE DEMO
<p-table [value]="tableData" selectionMode="single" metaKeySelection = "true">
<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>
您必須在p-table
中添加[(selection)]="selectedRow"
以便將所選行綁定到您定義的變量:
<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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.