如何从表中删除选择?

时间:2018-11-20 23:03:23

标签: angular typescript primeng

我已经为此苦了一段时间了。我有一个简单的表格,当我选择一行时,它会选中。但是我想为按钮添加一个功能(删除选择),以便在单击该按钮时希望从该行中删除选择。有人可以告诉我我所缺少的吗?提前非常感谢!

这是我的代码: 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>

1 个答案:

答案 0 :(得分:0)

您必须在[(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;
}