Source:  Twitter logo

I need to do an equivalent of this in Angular2:

foreach ($somethings as $something) {
    foreach ($something->children as $child) {
        echo '<tr>...</tr>';

Can this be achieved with ngFor and not adding new elements between <table> and <tr>?

I have a sample that might be similar to what you want:

<table id="spreadsheet">
    <tr *ngFor="let row of visibleRows">
        <td class="row-number-column">{{row.rowIndex}}</td>
        <td *ngFor="let col of row.columns">
            <input  data-id="{{col.rowIndex}}-{{col.columnIndex}}" [value]="col.cellValue" (input)="col.cellValue = $" (click)="model.selectColumn(col)" (keyup)="navigate($event)" />

I use this to render out a spreadsheet looking grid as seen here:

23 users liked answer #0dislike answer #023
TGH profile pic

If you need 2 or more foreach loops to draw rows of a table you need to do similar to the following.

<template ngFor let-rule [ngForOf]="model.rules" let-ruleIndex="index">
    <template ngFor let-clause [ngForOf]="rule.clauses" let-clauseIndex="index">
8 users liked answer #1dislike answer #18
Chris Snowden profile pic
Chris Snowden

Use the 'template' form of the ngFor syntax, seen here. It's a bit more verbose than the simpler *ngFor version, but this is how you achieve looping without output html (until you intend to). One exception: you'll still get html comments within your <table> but I'm hoping that's ok. Here's a working plunkr:

  selector: 'my-app',
  providers: [],
  directives: [],
  template: `
    <template ngFor #something [ngForOf]="somethings" #i="index">
      <template ngFor #child [ngForOf]="something.children" #j="index">
export class App {
  private somethings: string[][] = [
    {children: ['foo1', 'bar1', 'baz1']},
    {children: ['foo2', 'bar2', 'baz2']},
    {children: ['foo3', 'bar3', 'baz3']},
5 users liked answer #2dislike answer #25
Tim Kindberg profile pic
Tim Kindberg

template does not work for me but ng-template with ngForOf do the trick:

<ng-template ngFor let-parent [ngForOf]="parent" >
    <tr *ngFor="let child of parent.children">
            {{ child.field1 }}
            {{ child.field2 }}
        <td> ... and so one ... </td>
3 users liked answer #3dislike answer #33
jspassov profile pic

I am just trying display data for any table in my db. I did it like this:

My TypeScript Ajax call to API in table.component.ts:

http.get<ITable>(url, params).subscribe(result => {
  this.tables = result;
}, error => console.error(error));

My ITable

 interface ITable {
  tableName: string;
  tableColumns: Array<string>;
  tableDatas: Array<Array<any>>;

My table.component.html

<table class='table' *ngIf="tables">
      <th *ngFor="let tableColumn of tables.tableColumns">{{ tableColumn }}</th>
    <tr *ngFor="let tableData of tables.tableDatas">
      <td *ngFor="let data of tableData">{{ data }}</td>
0 users liked answer #4dislike answer #40
Stephen Himes profile pic
Stephen Himes

Copyright © 2022 QueryThreads

All content on Query Threads is licensed under the Creative Commons Attribution-ShareAlike 3.0 license (CC BY-SA 3.0).