Javascript 使用Angular 5指令从JSON对象创建动态表
是否可以使用Angular 5指令或借助jQuery基于JSON对象创建动态表(动态列)?如果是,怎么做 假设我从REST API获得此JSON响应:Javascript 使用Angular 5指令从JSON对象创建动态表,javascript,html,angular,typescript,html-table,Javascript,Html,Angular,Typescript,Html Table,是否可以使用Angular 5指令或借助jQuery基于JSON对象创建动态表(动态列)?如果是,怎么做 假设我从REST API获得此JSON响应: { name: "Ferrari" country: "Italy", creater: "Enzo Ferrari" cars: [ { modell: "Ferrari 488", price: "215.683€" },
{
name: "Ferrari"
country: "Italy",
creater: "Enzo Ferrari"
cars: [
{
modell: "Ferrari 488",
price: "215.683€"
},
{
modell: "Ferrari Portofino",
price: "189.704€"
}
]
}
现在我想用这些数据创建一个如下所示的表:
+-----------+----------+--------------+--------------+------------+--------------------+-------------+
| Name | Country | Creater | Modell | Price | Modell | Price |
+-----------+----------+--------------+--------------+------------+----------------------------------+
| Ferrari | Italy | Enzo Ferrarie| Ferrarie 488 | 189.704€ | Ferrarie Portofino | 189.704€ |
+-----------+----------+--------------+--------------+------------+--------------------+-------------+
<table>
<thead>
<tr>
<th>Name</th>
<th>Country</th>
<th>Creater</th>
<ng-template ngFor [ngForOf]="data.cars">
<th>Modell</th>
<th>Price</th>
</ng-template>
</tr>
</thead>
<tbody>
<tr>
<td>{{ data.name }}</td>
<td>{{ data.country }}</td>
<td>{{ data.creater }}</td>
<ng-template ngFor let-car [ngForOf]="data.cars">
<th>{{ car.modell }}</th>
<th>{{ car.price }}</th>
</ng-template>
</tr>
</tbody>
</table>
解决这个问题的最佳方法是什么?我就是想不出怎么解决这个问题?非常感谢您的帮助。如果表中只有一个对象(即一个标题行和一个数据行),您可以这样做:
+-----------+----------+--------------+--------------+------------+--------------------+-------------+
| Name | Country | Creater | Modell | Price | Modell | Price |
+-----------+----------+--------------+--------------+------------+----------------------------------+
| Ferrari | Italy | Enzo Ferrarie| Ferrarie 488 | 189.704€ | Ferrarie Portofino | 189.704€ |
+-----------+----------+--------------+--------------+------------+--------------------+-------------+
<table>
<thead>
<tr>
<th>Name</th>
<th>Country</th>
<th>Creater</th>
<ng-template ngFor [ngForOf]="data.cars">
<th>Modell</th>
<th>Price</th>
</ng-template>
</tr>
</thead>
<tbody>
<tr>
<td>{{ data.name }}</td>
<td>{{ data.country }}</td>
<td>{{ data.creater }}</td>
<ng-template ngFor let-car [ngForOf]="data.cars">
<th>{{ car.modell }}</th>
<th>{{ car.price }}</th>
</ng-template>
</tr>
</tbody>
</table>
名称
国家
创造者
模特儿
价格
{{data.name}
{{data.country}
{{data.creater}
{{car.modell}
{{car.price}}
您可以使用ng模板和ngFor指令在车辆上循环
重要提示:如果有多个数据行,则需要确保转换数据,以便始终显示相同数量的列,否则html将被破坏。如果表中只有一个对象(表示一个标题行和一个数据行),则可以执行以下操作:
+-----------+----------+--------------+--------------+------------+--------------------+-------------+
| Name | Country | Creater | Modell | Price | Modell | Price |
+-----------+----------+--------------+--------------+------------+----------------------------------+
| Ferrari | Italy | Enzo Ferrarie| Ferrarie 488 | 189.704€ | Ferrarie Portofino | 189.704€ |
+-----------+----------+--------------+--------------+------------+--------------------+-------------+
<table>
<thead>
<tr>
<th>Name</th>
<th>Country</th>
<th>Creater</th>
<ng-template ngFor [ngForOf]="data.cars">
<th>Modell</th>
<th>Price</th>
</ng-template>
</tr>
</thead>
<tbody>
<tr>
<td>{{ data.name }}</td>
<td>{{ data.country }}</td>
<td>{{ data.creater }}</td>
<ng-template ngFor let-car [ngForOf]="data.cars">
<th>{{ car.modell }}</th>
<th>{{ car.price }}</th>
</ng-template>
</tr>
</tbody>
</table>
名称
国家
创造者
模特儿
价格
{{data.name}
{{data.country}
{{data.creater}
{{car.modell}
{{car.price}}
您可以使用ng模板和ngFor指令在车辆上循环
重要提示:如果有多个数据行,则需要确保转换数据,以便始终显示相同数量的列,否则html将被破坏。您可以使用此选项。您可以在此处查看教程:或使用已实现数据表的现有模块。您可以使用此选项。您可以在此处查看教程:或使用已实现数据表的现有模块。