angular - primeng rowStyleClass 不能按预期工作

标签 angular datatable primeng

我的 Angular 2 应用程序中有一个 primeng 数据表 (1.1.0),我正在使用 rowStyleClass 将类设置为展开的行。

问题是,当行被扩展时,它工作正常,但如果我折叠它,那么它仍然与扩展类一起使用。

组件

  rowStyle(rowData: any, rowIndex: number): string {
    if ((this as DataTable).isRowExpanded(rowData)) {
      return 'ui-state-highlight';
    } else {
      return '';
    }
  }

查看

<p-dataTable tableStyleClass="table" *ngIf="model.result.length > 0" [rows]="itemsPerPage" [paginator]="isPaginatorVisible()"
      (onSort)="resetPagination()" [value]="xModel.ergebnis" [(selection)]="selected" expandableRows="true" [rowStyleClass]="rowStyle">
...

如果展开行:

<tr class="ui-datatable-even ui-datatable-odd ui-state-highlight ui-widget-content" ng-reflect-klass="ui-widget-content ui-state-highlight" ng-reflect-ng-class="[object Object]">

然后崩溃了:

<tr class="ui-datatable-even ui-datatable-odd ui-widget-content ui-state-highlight" ng-reflect-klass="ui-widget-content" ng-reflect-ng-class="[object Object]">

如您所见,ui-state-highlight 仅从 ng-reflect-klass 中删除,而没有从类本身中删除。是错误还是我做错了什么?

最佳答案

尝试手动检测更改并在更改后更新 View - 使用 Angular 更改检测:

import {ChangeDetectorRef} from '@angular/core';

constructor(private changeDetectorRef: ChangeDetectorRef) {}

  rowStyle(rowData: any, rowIndex: number): string {
    if ((this as DataTable).isRowExpanded(rowData)) {
      return 'ui-state-highlight';
    } else {
      return '';
    }
    if (!this.changeDetectorRef['destroyed']) {
          this.changeDetectorRef.detectChanges();
    }
  }

关于angular - primeng rowStyleClass 不能按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41168011/

相关文章:

date - 使用 Primeng Datepicker 如何仅启用月份的最后一天?

javascript - 在 Angular 2 的另一个组件中使用复杂类型对象

javascript - 以 Angular 循环嵌套对象

C# WPF DataGrid.ItemsSource 生成新列

c# - 数据源未添加到 ReportViewer

angular - PrimeNG 复选框默认不被选中

angular - PrimeNG 下拉菜单 : Showclear displays clear icon initially

css - 如何将 img 放入另一个 p 标签下的 p 标签中

angular - 错误 : Unexpected value 'undefined' exported by the module 'DynamicFormModule'

c# - 将 DataTable 转换为 LINQ 匿名类型