【问题标题】:Why doesnt *ngFor display the html table rows in the right order?为什么 *ngFor 不以正确的顺序显示 html 表格行?
【发布时间】:2020-01-14 23:18:00
【问题描述】:

我想通过表格显示数据结构。数据结构如下:

table-st.component.ts

import { Component, OnInit, Input, Inject } from "@angular/core";
import { DTO } from "@core/dto";

@Component({
  selector: "table-st",
  templateUrl: "./table-st.component.html",
  styleUrls: ["./table-st.component.scss"],
})
export class TableStComponent implements OnInit {
  table: DTO.Table = = JSON.parse(`{ 
    "properties": [
      {
        "type": "java.lang.String",
        "id": "Project"
      },
      {
        "type": "java.lang.String",
        "id": "Description"
      },
      {
        "type": "java.lang.Double",
        "id": "Amount of..."
      }
    ],
    "tree": {
          "children": [
            {
              "children": null,
              "data": [
                "Lorem Ipsum",
                "Lorem Ipsum",
                1,
                0,
                "Lorem Ipsum",
                "2421123",
                "Lorem Ipsum",
                null
              ],
              "itemId": "2135324123"
            },
            {
              "children": null,
              "data": [
                "23412321",
                "Lorem Ipsum",
                1,
                0,
                "Lorem Ipsum",
                "42412313",
                "Lorem Ipsum",
                null
              ],
              "itemId": "432351312"
            },
            {
              "children": null,
              "data": [
                "3412312421",
                "Lorem Ipsum",
                1,
                14,
                "Lorem Ipsum",
                "75634532",
                "Lorem Ipsum",
                null
              ],
              "itemId": "98765334"
            },
            {
              "children": null,
              "data": [
                "7463525",
                "Lorem Ipsum",
                1,
                0,
                "Lorem Ipsum",
                "6874645",
                "Lorem Ipsum",
                null
              ],
              "itemId": "363452345"
            }
          ],
          "data": [
            "34523234",
            "Lorem Ipsum",
            4,
            14,
            null,
            null,
            null,
            "Lorem Ipsum"
          ],
          "itemId": "345345"
        }
    }
  `);
  ngOnInit(){
  }
}

我想在我的 html 中显示这个结构

table-st.component.html

<table>
        <thead>
            <tr>
                <th *ngFor="let property of table.properties">{{property.id}}</th>
            </tr>
        </thead>
        <tbody>

            <!-- Iterate over the parents and differ in 2 cases -->
            <ng-container *ngFor="let entry of table.tree.children">

                <!-- Case 1: Element has no children. Display just the parent -->
                <ng-template *ngIf="!entry.children; else hasChildren">
                    <tr *ngFor="let entryData of entry.data">
                        <td>{{entryData}}</td>
                    </tr>
                </ng-template>

                <!-- Case 2: Element has children. Display parent element and children -->
                <ng-template #hasChildren>
                    <tr>
                        <td *ngFor="let entryData of entry.data">{{entryData}}</td>
                    </tr>
                        <tr *ngFor="let children of entry.children" class="child">
                            <td *ngFor="let child of children.data">{{child}}</td>
                        </tr>
                </ng-template>
            </ng-container>
        </tbody>
    </table>

但顺序是相反的。首先是孩子,然后是父母。像这样:

  • 儿童 1.1
  • 儿童 1.2
  • 父 1
  • 子 2.1
  • 子 2.2
  • 父 2

如何才能按顺序正确显示?

【问题讨论】:

  • 您能提供一个最小的可重现示例吗?也许在 StackBlitz 上? stackoverflow.com/help/minimal-reproducible-example
  • 您的第一个代码块有一些语法问题,可能来自部分复制粘贴。你能编辑清楚吗?
  • 我想在 StackBlitz 中重现最小的可重现示例,但在这样做时我实际上发现了错误。事实上,这都是关于插入数据的。我将关闭这个问题。但是非常感谢您提供帮助:)

标签: html angular typescript


【解决方案1】:

我想在 StackBlitz 中重现最小的可重现示例,但在这样做时我实际上发现了错误。事实上,这都是关于插入数据的。我将关闭这个问题。但是非常感谢您提供帮助:)

【讨论】:

    猜你喜欢
    • 2019-10-14
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多