简体   繁体   English

如何使用 ngTemplateOutlet 实现 Primeng 可重用表

[英]How to implement Primeng Reusable Table using ngTemplateOutlet

I'm trying to create reusable component which have primeng-table.我正在尝试创建具有 primeng 表的可重用组件。 This reusable component will accept input as data and col to display in table.这个可重用的组件将接受输入作为数据和列显示在表格中。

In user component, I'm just using prime-table component to display data by passing the data and cols as input.在用户组件中,我只是使用 prime-table 组件通过将数据和 cols 作为输入传递来显示数据。

Now, I want to have custom-users component which will pass <ng-template pTemplate="body" let-rowData let-columns="columns"> to replace the content of table body with transformed data(Any customization like hyperlink for user) in reusable component using ngTemplateOutlet and @ContentChild.现在,我想要自定义用户组件,它将传递<ng-template pTemplate="body" let-rowData let-columns="columns">用转换后的数据替换表体的内容(任何自定义,如用户的超链接) 在可重用组件中使用 ngTemplateOutlet 和 @ContentChild。 So that users components will not be affected.这样用户组件就不会受到影响。 Basically, if we pass ng-template body, that should be used to display table body.基本上,如果我们传递 ng-template body,那应该用来显示表体。 Or else it should use default implementation in prime-table component.否则它应该在 prime-table 组件中使用默认实现。 Can anyone pls help me to achieve this?谁能帮我实现这个目标?

Here is the base code setup which is having users component and primetable component -https://github.com/chandru-kumar/primeng-reusable-table-example .这是具有用户组件和 primetable 组件的基本代码设置 -https://github.com/chandru-kumar/primeng-reusable-table-example Now want to customize the data in custom-users table data by passing the ng-template body.现在想通过传递 ng-template 主体来自定义 custom-users 表数据中的数据。

users.component.html用户.component.html

<app-prime-table
    [data]="data"
    [cols]="cols"
></app-prime-table>

prime-table.component.html prime-table.component.html

<p-table [columns]="cols" [value]="data" responsiveLayout="scroll">
    <ng-template pTemplate="header" let-columns>
        <tr>
            <th *ngFor="let col of columns">
                {{col.header}}
            </th>
        </tr>
    </ng-template>
    <ng-template pTemplate="body" let-rowData let-columns="columns">
        <tr>
            <td *ngFor="let col of columns">
                {{rowData[col.field]}}
            </td>
        </tr>
    </ng-template>
</p-table>

prime-table.component.ts素表.component.ts

import { Component, Input, OnInit } from '@angular/core';

@Component({
  selector: 'app-prime-table',
  templateUrl: './prime-table.component.html',
  styleUrls: ['./prime-table.component.scss']
})
export class PrimeTableComponent implements OnInit {

  @Input()
  data: any[] = [];

  @Input()
  cols: any[] = [];

  constructor() { }
  ngOnInit() {
      
  }
}

Implemented with Template as Input to child component.使用模板作为子组件的输入实现。

Full solution is here -https://github.com/chandru-kumar/primeng-reusable-table-example完整的解决方案在这里 -https://github.com/chandru-kumar/primeng-reusable-table-example

prime-table.component.html prime-table.component.html

<ng-template pTemplate="body" let-rowData let-columns="columns">
    <ng-template #defaultTemplate>
        <tr>
            <td *ngFor="let col of columns">
                {{rowData[col.field]}}
            </td>
            
        </tr>
    </ng-template>
        
    <ng-container *ngTemplateOutlet="customBodyTemplate ? customBodyTemplate : defaultTemplate; context: {$implicit: rowData}" ></ng-container>

From Parent component, Implement the custom template to show the table body data like below: custom-users.component.html从父组件,实现自定义模板以显示表体数据,如下所示: custom-users.component.html

<app-prime-table
    [data]="data"
    [columns]="columns"
    [customBodyTemplate] = "customBodyTemplate"
></app-prime-table>

<ng-template #customBodyTemplate let-rowData>
    <tr>
        <td *ngFor="let col of columns">
            <a href="">{{rowData[col.field]}}</a>
        </td>
    </tr>
</ng-template>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM