1

我从我的 API 收到 JSON 作为响应,我必须将该数据显示到表结构中。

考虑 JSON 如下:

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

但是并不是固定的key1key2它每次都会改变,所以我必须将从 API 接收到的内容显示为表结构。我提到了很多关于动态键的内容。我提到了这个https://medium.com/codingthesmartway-com-blog/angular-material-part-4-data-table-23874582f23a

但必须显示动态内容。

编辑

我想要以下结构:

key1    key2
value1  value2
value3  value4

使用 angularjs 和 ng-repeat answerBy从 JSON 数据创建表SantoshK我需要我这么认为,但它为我显示空白表。

4

2 回答 2

0

你可以试试这个(仅供参考):

<!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>
于 2018-05-24T11:49:43.350 回答
0

这是一个解决方案,您应该Object.keys获取的dynamic关键值object

零件:

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>

这是一个有效的演示

于 2018-05-24T12:07:01.970 回答