29

I need to do an equivalent of this in Angular2:

<?php
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>?

4
  • use ng-repeat in your html Commented Feb 6, 2016 at 14:01
  • ps you can nest ng-repeat Commented Feb 6, 2016 at 14:17
  • 2
    ng-repeat? There;s no ng-repeat in angular2? Do you propose switching to angular 1.x? Commented Feb 6, 2016 at 15:33
  • 1
    It was edited not by me, but by another person. The initial questions version had Angular2 stated twice as it does now though. Commented Feb 7, 2016 at 21:29

5 Answers 5

24

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 = $event.target.value" (click)="model.selectColumn(col)" (keyup)="navigate($event)" />
        </td>
    </tr>
</table>

I use this to render out a spreadsheet looking grid as seen here: http://www.syntaxsuccess.com/angular-2-samples/#/demo/spreadsheet

Sign up to request clarification or add additional context in comments.

1 Comment

The link is dead
8

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">
        <tr>
            <td>{{clause.name}}</td>
        </tr>
    </template>
</template>    

Comments

5

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: http://plnkr.co/edit/KLJFEQlwelPJfNZYVHrO?p=preview

@Component({
  selector: 'my-app',
  providers: [],
  directives: [],
  template: `
  <table>
    <template ngFor #something [ngForOf]="somethings" #i="index">
      <template ngFor #child [ngForOf]="something.children" #j="index">
      <tr>{{child}}</tr>
      </template>
    </template>
  </table>
  `
})
export class App {
  private somethings: string[][] = [
    {children: ['foo1', 'bar1', 'baz1']},
    {children: ['foo2', 'bar2', 'baz2']},
    {children: ['foo3', 'bar3', 'baz3']},
  ]
}

3 Comments

I'm not sure how Angular handles this but IE removes <template> tags in <table>.
It's only a <template> before compilation. After compilation, and what actually gets written to the DOM, is just the <table> and some child html comments that act as anchors for the templates. But the templates themselves are not inserted at runtime. (At least as far as I understand). So as long as comments are allowed inside the <table> it'll work.
The newer syntax is <template ngFor let-item [ngForOf]="items" let-index="index">
3

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">
        <td>
            {{ child.field1 }}
        </td>
        <td> 
            {{ child.field2 }}
        </td>
        <td> ... and so one ... </td>
    </tr>
</ng-template>

Comments

0

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">
  <thead>
    <tr>
      <th *ngFor="let tableColumn of tables.tableColumns">{{ tableColumn }}</th>
    </tr>
  </thead>
  <tbody>
    <tr *ngFor="let tableData of tables.tableDatas">
      <td *ngFor="let data of tableData">{{ data }}</td>
    </tr>
  </tbody>
</table>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.