【问题标题】:Nested table with unknown amout of elements元素数量未知的嵌套表
【发布时间】:2019-03-07 08:42:59
【问题描述】:

我想用示例数据制作一个表格,但我的对象可以有多个主题,主题内可以有多个帖子。

[  
   {  
      "topicName":"First topic",
      "postInformations":[  
         {  
            "postName":"First post",
            "postNumbers":{  
               "number1":111,
               "number2":222,
               "number3":333
            }
         }
      ],
      "topicNumbers":{  
         "number1":123,
         "number2":456,
         "number3":789
      }
   }
]

这就是我想要达到的目标:

|[topicName | postName ]|[number1]|[number2]|[number3]|
| First topic           |     111 |     222 |     333 |
|    First post         |     444 |     555 |     666 |

通常我会在html中通过一一设置列来轻松地做到这一点,但我不知道有多少帖子主题,以及有多少主题。

知道如何使它适用于未知数量的帖子和主题吗?

【问题讨论】:

    标签: html angular html-table frontend


    【解决方案1】:

    您可以使用嵌套的 ngFor。类似的东西。

    <ul *ngFor="let item of topic>
      <li>{{item.topicName}}
      <ul *ngFor="let info of item.postInformations">
        <li> {{info.postName}}</li>
        <ul *ngFor="let number of info.postNumbers">
          <li>{{number.number1}}</li>
          <li>{{number.number2}}</li>
          <li>{{number.number3}}</li>
        </ul>
      </ul>
    </ul>
    

    【讨论】:

    • 谢谢,如果一切正常,我会尽快检查并接受答复。
    • 太棒了,如果您需要解释,请告诉我(添加到该代码中),所以我将编辑我的答案
    • 您的回答并不能解决问题。它只打印以下值:topicName 和 postNumbers 忽略 postName 和 topicNumbers。您还错过了第一行中的 " 和第一个
    • 的结束标记,但事实并非如此。
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签