0

My angular2 component has an @Input(): data of type [number,number]. The size of the array is should be determined from the input. How can I now bind the content of this array to a table in html, i.e., the table should be something like this:

<table>
    <tr>
        <td> data[0,0] </td>
        <td> data[0,1] </td>
        ...
    </tr>
    <tr>
        <td> data[1,0] </td>
        <td> data[1,1] </td>
        ...
    </tr>
    ...
</table>
6
  • If length of the array is determined from the input, you can define type data like @Input() data: number[];. Can you show example input for easy understand problem? Commented Mar 6, 2017 at 13:26
  • @Jarek: It is a two-dimensional array of size (n,m). Each entry is an integer. By "the length is determined from input" I mean that the component does not know the size intrinsically, only when it get's the data. Commented Mar 6, 2017 at 13:30
  • So you have only problem with display data, yes? Commented Mar 6, 2017 at 13:34
  • @Jarek I want to bind it in the html template using something like *ngFor, yes. Commented Mar 6, 2017 at 13:35
  • 1
    If that's the two-dimensional array using the wrong type period That's a tuple type Commented Mar 6, 2017 at 13:38

2 Answers 2

2

You can do this with two *ngFor-Directives:

<table>
    <tr *ngFor="let row of data">
        <td *ngFor="let value of row">{{value}}</td>
    </tr>
</table>

Working Plunkr

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

1 Comment

I should have looked at arrays in js closer. I didn't know they are interpreted as an array of arrays, i.e, an array of rows containin the array of cells. Then it is obvious, thank you. I was more thinking like one double-indexed data structure.
1

Short example:

Our array:

array = [[1,2],[3,4],[7,7]];

Our template:

<table>
  <tr *ngFor="let rows of array">
    <td *ngFor="let col of rows">{{ col }}</td>
  </tr>
</table>

1 Comment

Thanks, see my comment to @Markai 's answer.

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.