1

I am receiving a JSON as response from my API, i have to display that data into table structure.

Consider JSON as follows:

[
{
"key1":"value1",
"key2" :"value2"
},
{
"key1":"value3",
"key2" :"value4"
}
]

But key1 and key2 is not fixed, it will change each time, so i have to display the content received from API as Table structure. I referred a lot nothing is regarding dynamic keys. I referred this https://medium.com/codingthesmartway-com-blog/angular-material-part-4-data-table-23874582f23a

But have to display dynamic content.

EDIT

I want the below structure:

key1    key2
value1  value2
value3  value4

Create Table from JSON Data with angularjs and ng-repeat answerBy SantoshK is want i need i think so but its showing blank table for me.

Subburaj
  • 5,114
  • 10
  • 44
  • 87

2 Answers2

0

You can try this (It is just reference only):

<!DOCTYPE html>
<html>
   <title>Web Page Design</title>
   <head>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>    
   </head>
   <table>
      <thead>
         <tr>
            <th>Value</th>
         </tr>
      </thead>
      <tbody id="tableId">

      </tbody>
   </table>
   <script>
      function sayHello() {
         var sample = [{"key1":"value1","key2" :"value2"},{"key1":"value1","key2":"value2"}]


         for (key in sample){
             for (currVal in sample[key]){
                $('#tableId').append('<tr><td>'+sample[key][currVal]+'</td></tr>')
             }
         }

      }
      sayHello();
   </script>
   <body></body>
</html>
udayakumar
  • 639
  • 1
  • 7
  • 16
0

Here is a solution, you should take Object.keys to get the dynamic key values of object

Component:

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ]
})
export class AppComponent  {
  candidature = "test";
  data =[
{
"key1":"value1",
"key2" :"value2"
},
{
"key1":"value3",
"key2" :"value4"
}
]

getKeys(schedule_data){
  return Object.keys(schedule_data);

}
 }

HTML:

<table>  
<th *ngFor="let sch of getKeys(data[0])">
  {{sch}}
      <div *ngFor="let schedule_data of data">
        {{schedule_data[sch]}}
    </div>
</th>
</table>

Here is a working DEMO

Sravan
  • 18,467
  • 3
  • 30
  • 54