【发布时间】:2020-09-30 17:35:33
【问题描述】:
我以 JSON 的形式从服务器中提取数据并将其添加到 HTML 表中。我想将表行修复为 5,并且只有 JSON 中的前 5 个值应该使用 Angular 进入表中。如果可用数据少于 5 个,请用“-”填充表格列。
component.ts
import { HttpClient } from '@angular/common/http';
import { HttpErrorResponse } from '@angular/common/http';
@Component({
selector: 'app-orderbook',
templateUrl: './orderbook.component.html',
styleUrls: ['./orderbook.component.css']
})
export class OrderbookComponent implements OnInit {
title = 'JSON to Table Example';
constructor (private httpService: HttpClient) { }
arrBirds: string [];
ngOnInit() {
this.httpService.get('http://localhost:7000/orders/list').subscribe(
data => {
this.arrBirds = data as string []; // FILL THE ARRAY WITH DATA.
//console.log(this.arrBirds[1]);
},
(err: HttpErrorResponse) => {
//console.log (err.message);
}
);
}
}
component.html
<div style="text-align:left;width:500px;">
<h1>
{{ title }}!
</h1>
<table *ngIf="arrBirds">
<tr>
<th>Order ID</th>
<th>Type</th>
<th>Category</th>
<th>Quantity</th>
<th>Price</th>
</tr>
<tr *ngFor="let bird of arrBirds">
<td>{{bird.orderId}}</td>
<td>{{bird.type}}</td>
<td>{{bird.category}}</td>
<td>{{bird.quantity}}</td>
<td>{{bird.price}}</td>
</tr>
</table>
</div>
component.css
table {
border-collapse: collapse;
width: 95%;
margin:0 0 0 20px;
}
table, th, td {
border: 1px solid black;
}
【问题讨论】:
标签: javascript json angular