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

前端之家收集整理的这篇文章主要介绍了typescript – PrimeNG的数据表没有触发onRowSelect事件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在测试PrimeNG并尝试使用数据表.除了活动,一切都很好.我正在尝试使用Growl在选择行时显示消息(如PrimeNG网站上的Events演示).
我目前有这个:

import { Component,OnInit,EventEmitter } from '@angular/core';
import { REACTIVE_FORM_DIRECTIVES } from '@angular/forms';
import { DataTable,Column,Schedule,Growl,Message } from 'primeng/primeng';
import { NameListService } from '../shared/index';

@Component({
  moduleId: module.id,selector: 'dash',templateUrl: 'dashboard.component.html',styleUrls: ['dashboard.component.css'],directives: [REACTIVE_FORM_DIRECTIVES,DataTable,Schedule]
})
export class DashboardComponent implements OnInit {

  newName:string = '';
  newLanguage:string = '';
  errorMessage:string;
  names:any[] = [];
  selectedName:any;
  events:any[];
  cols:any[];
  msgs:Message[] = [];

  constructor(public nameListService:NameListService) {
  }

  ngOnInit() {
    this.getNames();
    this.cols = [
      {field: 'id',header: 'ID'},{field: 'name',header: 'Name'},{field: 'language',header: 'Language'}
    ];
}

  onRowSelect(event) {
    this.msgs = [];
    this.msgs.push({severity: 'info',summary: 'Selected',detail:event.data.name + ' - ' + event.data.language});
}

  getNames() {
    this.nameListService.get()
      .subscribe(
        names => this.names = names,error =>  this.errorMessage = <any>error
);
  }

  addName():boolean {
    this.names.push({"name": this.newName,"language": this.newLanguage});
    this.newName = '';
    this.newLanguage = '';
    return false;
  }

}

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

<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>

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

解决方法

看起来你忘了指定selectionMode:

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

有效值是“单个”和“多个”

Plunker example

更新:

您还必须订阅onRowSelect事件

(onRowSelect)="onRowSelect($event)"

哪里

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

猜你在找的Angularjs相关文章