2

I´m develop an application that call a microservices and these return de next JSON:

{
  "result": "OK",
  "message": null,
  "columns": [
    {
      "name": "Id-1",
      "order": 0,
      "values": [
        {
          "value": "cf"
        },
        {
          "value": "13"
        },
        {
          "value": "9c"
        },
        {
          "value": "5a"
        },
        {
          "value": "1c"
        },
        {
          "value": "45"
        },
        {
          "value": "b"
        }
      ]
    },
    {
      "name": "Name",
      "order": 1,
      "values": [
        {
          "value": "prueba"
        },
        {
          "value": "TEST"
        },
        {
          "value": "Op"
        },
        {
          "value": "Op"
        },
        {
          "value": "P"
        },
        {
          "value": "PruebaTest"
        },
        {
          "value": "nal"
        }
      ]
    }
  ]
}

The idea is that in my table show in <th> the columns name and in <td> the values. Something like this:

enter image description here

I tried to do it with several *ngFor but i can´t get the right result How i can do this?

2
  • Is it possible to change the structure so that you can make rows in top and columns below that? In tables rows(tr) comes first before columns(td). Commented Sep 25, 2019 at 7:42
  • You can get column names with Array.map(). And perhaps you want to transpose values to have rows instead of columns Commented Sep 25, 2019 at 7:47

3 Answers 3

1

Try this

<table>
  <tr>
     <td *ngFor="let d of data.columns">
     <table>
<tbody>
  <tr>
    {{d.name}}
    </tr>

<tr *ngFor="let main of d.values">

   <td>
   {{main.value}} 
     </td>
  </tr>

  </tbody>
</table>
    <td>

  </tr>
  </table>

Working link

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

Comments

1

Try this

<table>
  <tr >
  <th  *ngFor="let c of data.columns">{{c.name}}</th>
  </tr>
  <tr>
      <td  *ngFor="let c of data.columns">
       <div *ngFor="let v of c.values">
         <span>
          {{v.value}}
         </span>
        </div>
      </td>
  </tr>
</table>

Demo

Comments

0

Assuming you have control over API: I would make the API return a more readable JSON array of objects.

Having multiple *ngFor will make your application slower - It's better to put this load on server side.

JSON Example: [ { 'id': 'id-1', 'name': 'Name' }, { 'id': 'cf', 'name': 'prueba' }, etc.. ]

Then you can do;

<table>
<thead>
    <tr>
        <th>Id</th>
        <th>Name</th>
    </tr>
</thead>
<tbody>
    <tr *ngFor="let row of dataArray">
        <td>{{row.id}}</td>
        <td>{{row.name}}</td>
    </tr>
</tbody>

2 Comments

Yes but, the final user can execute SELECT * FROM X or execute SELECT X.NAME FROM X
So the user has access to modify and perform their own SQL Queries?

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.