5

I have an array which contains other arrays inside like that:

array = [
           ["element A", "element B"],
           ["YES", "NO"]
        ]

And I want to loop through this array of object in an HTML table using ngFor:

   <table>
     <thead>
       <tr>
         <th>#</th>
         <th>COLUMN 1</th>
         <th>COLUMN 2</th>
       </tr>
     </thead>

     <tbody>
       <template *ngFor="let row of csvContent; let in = index">
         <th scope="row">{{in}}</th>
            <template *ngFor="let c of row; let in = index">
              <td>
               {{c[0]}}
              </td>
            </template>
       </template>
     </tbody>
  </table>

I want to display each inner array list below COLUMN1 and COLUMN2 respectively:

 COLUMN1   | COLUMN2
 --------------------
 element A | YES
 element B | NO

I can't figure it out how to use *ngFor properly in order to list an array of arrays (Simple list of strings). At the moment, it's either an empty array or a shifted & messed up Table presentation.

This is how looks the Table:

shifted HTML Table using *ngFor

Or this wrong presentation because Element A and B should be below COLUMN 1 and YES, NO should be below COLUMN2:

enter image description here

4
  • your example doesnt have a valid array syntax Commented Jan 8, 2018 at 12:52
  • @Jota.Toledo The array is perfectly fine, I just tried to represent how the Table should look like. I add now a screenshot to make it clear. Commented Jan 8, 2018 at 12:53
  • no, remove the 1: and 2: from your example and that would be fine Commented Jan 8, 2018 at 12:55
  • @Jota.Toledo Thanks for your suggestion, I made the array looks correct now. Commented Jan 8, 2018 at 12:56

3 Answers 3

10

Your data is not arrays in arrays; it's two connected arrays. You need to treat it as such:

   <tbody>
     <tr *ngFor="let column of csvContent[0]; let in = index">
       <td>
         {{csvContent[0][in]}}
       </td>
       <td>
         {{csvContent[1][in]}}
       </td>
     </tr>
   </tbody>

This is not really a good way of organizing your data, as stuff is not really related. What if csvContent[0] gets a new entry, but 1 doesn't? Right now, your data doesn't represent a table, and I'd recommend transforming it in your controller to be tabluar, and then printing.

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

7 Comments

What do you mean by "stuff is not really related"? it's a simple Array which contains other arrays with several strings. I believe *ngFor has a way to show multi-dimensional array. Don't worry about organizing the data, it's coming from a static CSV file.
@NizarBsb, your array is not organized by rows, it's organized by columns.
That's right, does it means I need to rethink the structure of my array?
RhoVisions has the only correct answer here, surprised Adeeb even got an upvote. I would've restructured the array, but RhoVisions answer is completely acceptable
@lancovivi I also recommend restructuring, thus the last line of my answer. Inverting the structure relationship will cause the least headaches over time.
|
4

Try this:

<table>
     <thead>
       <tr>
         <th>#</th>
         <th>COLUMN 1</th>
         <th>COLUMN 2</th>
       </tr>
     </thead>

     <tbody>
       <tr *ngFor="let row of csvContent;let i = index;">
          <td>{{i}}</td>
          <td *ngFor="let c of row">
              {{c}}
          </td>
       </tr>
     </tbody>
  </table>

I wasn't sure how your data looked like, but seems like this would help. You don't really need to use <template> tags (they're deprecated anyway in favor of <ng-template> tags.

Also, no need for index tracking if you're gonna access the array at that index anyway.

8 Comments

I've attached a screenshot, your snippet does not show the Table properly, I still have the same issue.
I updated now with the first column showing the index
I have added a second screenshot to show you the result of your snippet. As you can see, the presentation is there but the rows are not below the right column.
This is not going to work that way, think that element A and element B need to be below COLUMN1 and finally YES, NO need to be below COLUMN2. At the moment this is showing inline as the screenshot of a wrong example.
Please notice I added a first <td>{{i}}</td> to every <tr> in the loop.
|
3

Simply loop like this

<table>
  <thead>
    <tr>
      <th>#</th>
      <th>COLUMN 1</th>
      <th>COLUMN 2</th>
    </tr>
  </thead>

  <tbody>
    <tr *ngFor="let row of csvContent;let i = index;">
      <td>{{i}}</td>
      <td *ngFor="let c of row">{{c}}</td>
    </tr>
  </tbody>
</table>

1 Comment

My answer exactly

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.