【问题标题】:Angular PrimeNG table set up pipe per column using cols arrayAngular PrimeNG 表使用 cols 数组为每列设置管道
【发布时间】:2018-11-29 09:00:16
【问题描述】:

我正在尝试使用 PrimeNG 学习角度。这是链接https://primefaces.org/primeng/#/table

是否还可以使用管道数组为每列包含管道?

在 cols 数组中,我想添加另一个这样的字段。

this.cols = [
    { field: 'vin', header: 'Vin', type: 'date' },
    { field: 'year', header: 'Year', type: 'number' },
    { field: 'brand', header: 'Brand', type: 'number' },
    { field: 'color', header: 'Color'}
];

然后在模板中,我会这样使用它

<tr>
    <td *ngFor="let col of columns">
        {{ col.type? rowData[col.field] | col.type : rowData[col.field]}}
    </td>
</tr>

我已经尝试过了,它给了我模板解析错误。

【问题讨论】:

    标签: angular primeng-turbotable


    【解决方案1】:

    您可以尝试以下操作:

    1. 在您的 ts 文件中导入您需要的管道:

      import { 
        CurrencyPipe,
        LowerCasePipe,
        UpperCasePipe
      } from '@angular/common';
      
    2. 将它们添加到组件的 providers 属性中

      providers: [
        CurrencyPipe, 
        LowerCasePipe,
        UpperCasePipe
      ]
      
    3. 通过构造函数传递管道为private

      constructor(private cp: CurrencyPipe, 
                  private lcp: LowerCasePipe,
                  private ucp: UpperCasePipe) {
      }
      
    4. 通过cols将管道传递给您的HTML:

      this.cols = [
        { field: 'vin', header: 'Vin', type: this.ucp },
        { field: 'startYear', header: 'Year', type: this.cp, arg1: 'CAD'},
        { field: 'brand', header: 'Brand', type: this.lcp },
        { field: 'color', header: 'Color' }
      ];
      

      我没有找到将参数数组传递给 HTML 的好方法(pipe(val, ...args) 在 HTML 中不起作用),所以我添加了 arg1arg2arg3,我们可以传递和使用.

    5. 在您的 HTML 中使用它:

      <td *ngFor="let col of columns">
        {{ col.type ? col.type.transform(rowData[col.field], col.arg1, col.arg2, col.arg3) : rowData[col.field] }}
      </td>
      

    Stackblitz 示例:https://stackblitz.com/edit/angular-4x6q9b?file=app%2Fapp.module.ts

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-14
      • 2017-11-20
      • 1970-01-01
      • 1970-01-01
      • 2018-03-24
      • 2020-04-25
      • 2019-03-07
      • 1970-01-01
      相关资源
      最近更新 更多