0

I've tried so many things and failed that my code ended up being a total mess. I'm open to anything but, in case you want a base:

The most promising approach I found was the following, but failed with the exception "templateRef.createEmbeddedView is not a function":

app.component.html

<br>

<mt-table [fields]="userFields" [(rows)]="users" [pageSize]="10" [searchString]="searchString"
          class="round">

  <template let-row="row">
    <td>
      {{ row.name }}
    </td>
    <td>
      <i class="fa fa-{{ row.gender === 'male' ? 'mars' : 'venus' }}"></i>
    </td>
    <td>
      {{ row.email }}
    </td>
    <td>
      {{ row.phone }}
    </td>
    <td>
      {{ row.address.number }}
    </td>
    <td>
      {{ row.address.street }}
    </td>
    <td>
      {{ row.address.city }}
    </td>
    <td>
      {{ row.address.state }}
    </td>
    <td align="center">
      <span class="actions show-on-tr-hover">
        <i class="fa fa-pencil-square opacity-on-hover"></i>
        <i class="fa fa-minus-square opacity-on-hover"></i>
      </span>
    </td>
  </template>

</mt-table>

mtTable.component.html

  <thead *ngIf="fields?.length">
    <tr>
      <th
        *ngFor="let field of fields"
        [ngClass]="{ 'sortable': field.isSortable }"
        (click)="activateColumn(field)">
        {{ field.label }}

        <span *ngIf="field.isSortable" class="sorting">
          <div class="arrow" [ngClass]="{ 'active-sorting': field === activeColumn && !field.reverseOrder }">&#9650;</div>
          <div class="arrow" [ngClass]="{ 'active-sorting': field === activeColumn && field.reverseOrder }">&#9660;</div>
        </span>
      </th>
    </tr>
  </thead>

  <tbody *ngIf="rows?.length">
    <tr *ngFor="let row of getPaginatedRows()"
        (click)="onRowClick(row)" class="clickable">
      <template [ngTemplateOutlet]="rowTemplate" [ngOutletContext]="{ row: row }"></template>
    </tr>
  </tbody>

  <tfoot *ngIf="pageSize">
    <tr>
      <td [attr.colspan]="fields?.length">
        <mt-table-pagination
          [rows]="getProcessedRows()"
          [pageSize]="pageSize"
          [(output)]="pagination">
        </mt-table-pagination>
      </td>
    </tr>
  </tfoot>

</table>

Do any of you have an idea on why this fails or know any possible alternative approaches?

Jesús Cruz
  • 192
  • 2
  • 18
  • Everything else not posted here is working fine and directly copying the template contents into mtTable.component.html populates the table as expected. Regardless, feel free to request any additional piece of code that you might found interesting to look at (or check the [github repo](https://github.com/jesuscc1993/angular2-table-demo/tree/dev) directly). – Jesús Cruz Jun 16 '17 at 18:41
  • Thank you for the github repo – yurzui Jun 16 '17 at 19:44

1 Answers1

2

I would replace

@ViewChildren(TemplateRef) rowTemplate: TemplateRef<any>;

with

@ContentChild(TemplateRef) rowTemplate: TemplateRef<any>;

because i want to use template from Light DOM

See also

yurzui
  • 205,937
  • 32
  • 433
  • 399
  • So I take it that ViewChildren only looks for rendered html but ContentChild looks for html regardless of whether it's being rendered? In any case, I still get the same exact exception. – Jesús Cruz Jun 16 '17 at 21:17
  • 1
    Yes, `ContentChild` looks on the content between component tags in parent component – yurzui Jun 16 '17 at 21:26
  • My apologies. I had changed it to ContentChild*ren* by mistake. It's now properly working. You have my deepest gratitude. – Jesús Cruz Jun 16 '17 at 22:10