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.

8
  • can u please add some of your code? Commented May 24, 2018 at 11:19
  • So you would like to iterate through your json key/value with index is that right ? Like json[0].key = "key1" and json[0].value = "value1" Commented May 24, 2018 at 11:25
  • stackblitz.com/edit/… Commented May 24, 2018 at 11:27
  • @Sravan In your code suppose name: string; position: number; weight: number; symbol: string; is not fixed means if you dont know the keys , is my case Commented May 24, 2018 at 11:36
  • in html there is no any key.. it is dynamic Commented May 24, 2018 at 11:39

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

1 Comment

whether i have to use jquery must here??
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

4 Comments

i found a link stackoverflow.com/questions/43864288/… and its upto what i expected
please check my demo, it also creates dynamic keys
@Subburaj, is that fine?
@Subburaj, I changed my answer to make it in the format you needed. Please check

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.