Javascript 如果ng.for小于表头(th),如何在表数据(td)中显示空消息?
我有这个代码在表中显示数据。迭代时我只有3辆车和3辆自行车,如何在表头(th)#4和#5中显示表数据(td)的空消息 我想找到一个动态的解决方案。因此,如果我的汽车或自行车少了,仍然显示空消息,或者如果我拥有所有的汽车或自行车,则不显示空消息Javascript 如果ng.for小于表头(th),如何在表数据(td)中显示空消息?,javascript,html,angular,typescript,Javascript,Html,Angular,Typescript,我有这个代码在表中显示数据。迭代时我只有3辆车和3辆自行车,如何在表头(th)#4和#5中显示表数据(td)的空消息 我想找到一个动态的解决方案。因此,如果我的汽车或自行车少了,仍然显示空消息,或者如果我拥有所有的汽车或自行车,则不显示空消息 <table class="table table-bordered table-ranking text-center"> <thead class="thead-light">
<table class="table table-bordered table-ranking text-center">
<thead class="thead-light">
<tr>
<th class="no-bg"></th>
<th>#1</th>
<th>#2</th>
<th>#3</th>
<th>#4</th>
<th>#5</th>
</tr>
</thead>
<tbody class="thead-light">
<tr>
<th>Car</th>
<td *ngFor="let car of ranking?.totals?.car">
{{ car?.property?.name }}<br /><span
class="muted small">{{ car?.total / millionDivisor | number: '1.0-2' }} MM EUR
| {{ car?.percentage | number: '1.0-2' }}%</span>
</td>
</tr>
<tr>
<th>Bike</th>
<td *ngFor="let bike of ranking?.totals?.bike">
{{ bike?.property?.name }}<br /><span
class="muted small">{{ bike?.total / millionDivisor | number: '1.0-2' }} MM EUR
| {{ bike?.percentage | number: '1.0-2' }}%</span>
</td>
</tr>
</tbody>
</table>
您可以使用索引和检查值是否未定义
{{排名?.totals?.car[index]?.property?.name}}
{{排名?.totals?.car[index]?.total/millionDivisor |编号:'1.0-2'}}毫米欧元
|{{排名?.总数?.汽车[指数]?.百分比|数字:'1.0-2'}}
没有可用的数据
演示:你能提供一个用于排名的样本数据集吗?@Arctezy我在帖子中添加了数据样本。非常感谢@Arctezy。这个解决方案有效!
ranking:{
"totals": {
"car": [
{
"property": {
"_id": "5f8ffa5e4106274",
"name": "porsche",
"hash": "aCTGdtRUhy",
"createdAt": "2020-10-21T09:07:42.524Z",
"updatedAt": "2021-02-01T09:59:01.571Z",
"__v": 0
},
"total": 89687422,
"percentage": 99.99993756096029,
"ranking": 1
},
{
"property": {
"_id": "5fe413938125ad06272",
"name": "seat",
"hash": "UOMTGBb5L",
"createdAt": "2020-10-21T09:07:42.425Z",
"updatedAt": "2021-02-01T09:59:01.529Z",
"__v": 0
},
"total": 56,
"percentage": 0.00006243903970630104,
"ranking": 2
},
{
"property": {
"_id": "6069e612f1686a672c201",
"name": "mercedes",
"hash": "aCTGhh",
"createdAt": "2020-10-21T09:07:42.524Z",
"updatedAt": "2021-02-01T09:59:01.571Z",
"__v": 0
},
"total": 0,
"percentage": 0,
"ranking": 3
}
],
"bike": [
{
"property": {
"_id": "5f8ffa5e413938125ad",
"name": "A",
"hash": "UGBb5L",
"createdAt": "2020-10-21T09:07:42.425Z",
"updatedAt": "2021-02-01T09:59:01.529Z",
"__v": 0
},
"total": 1286553.2196000002,
"percentage": 50.14333136951876,
"ranking": 1
},
{
"property": {
"_id": "538125ad06274",
"name": "B",
"hash": "GdtRUhy",
"createdAt": "2020-10-21T09:07:42.524Z",
"updatedAt": "2021-02-01T09:59:01.571Z",
"__v": 0
},
"total": 1279198.1664,
"percentage": 49.856668630481266,
"ranking": 2
},
{
"property": {
"_id": "12f1686a672c201",
"name": "C",
"hash": "aCRUhh",
"createdAt": "2020-10-21T09:07:42.524Z",
"updatedAt": "2021-02-01T09:59:01.571Z",
"__v": 0
},
"total": 0,
"percentage": 0,
"ranking": 3
}
]
}
}
```