PrimeNG的数据表没有触发onRowSelect事件

时间:2016-07-28 15:19:16

标签: typescript angular primeng

我目前正在测试PrimeNG并尝试使用数据表。除了活动,一切都很好。我选择使用Growl来显示消息(如PrimeNG网站上的Events演示)。 我目前有这个:

<p-growl [value]="msgs"></p-growl>

<form (submit)="addName()" style="margin-bottom:10px;">
  <label>Name:</label>
  <input class="form-control" [(ngModel)]="newName" name="newName" 
     placeholder="Enter new name..."
     style="margin-bottom: 10px;">
  <label>Language:</label>
  <input class="form-control" [(ngModel)]="newLanguage" 
     name="newLanguage" placeholder="Enter new language..."
     style="margin-bottom: 10px;">
  <button class="btn btn-primary" type="submit" 
         *ngIf="newName && newLanguage"><i class="fa fa-plus"></i> Add</button>
</form>

<p-dataTable [value]="names" [(selection)]="selectedName" 
    selectionMode="single">
    <p-column *ngFor="let col of cols" [field]="col.field" 
               [header]="col.header">
    </p-column>
</p-dataTable>

<div *ngIf="selectedName">
  <label>Selected person name:</label><br/>
  <input class="form-control" [(ngModel)]="selectedName? selectedName.name: none" 
     readonly style="margin-bottom: 10px;"/><br/>
  <label>Selected person programming language:</label><br/>
  <input class="form-control" 
     [(ngModel)]="selectedName? selectedName.language: none" 
     readonly style="margin-bottom: 10px;"/><br/>
  <label>Selected person birth year:</label><br/>
  <input class="form-control" 
     [(ngModel)]="selectedName? selectedName.birthYear: none" readonly/>
</div>

仪表板组件模板如下所示:

@word.semantical_categories_ids

但是,当我选择一行时,事件不会触发。它不会在断点处停止,因此它根本不会注册它。有没有解决方案或一些建议,我应该在哪里解决这个问题?

2 个答案:

答案 0 :(得分:11)

您似乎忘了指定selectionMode

<p-dataTable [value]="names" selectionMode="single" (onRowSelect)="onRowSelect($event)">
  ...    
</p-dataTable>

有效值是&#34;单个&#34;和&#34;多个&#34;

<强> Plunker example

<强>更新

您还必须订阅onRowSelect活动

(onRowSelect)="onRowSelect($event)"

其中

  • (onRowSelect) - 来自DataTable组件的事件名称
  • onRowSelect($event) - 组件中方法的名称

答案 1 :(得分:0)

您必须在app.module.ts

中导入FormsModule
import { FormsModule } from '@angular/forms';