简体   繁体   中英

How to create a tree table html file usin below JSON file

I am trying to create tree table html file using primeng. The Html file should able to render below JSON file .

Below is the Json response.

{
      "data": [
        {
          "data": {
            "Briname": "Aamir",
            "aantalPersonen": "122"
          },
          "children": [
            {
              "data": {
                "Vestiging": "Ranchi",
                "aantalPersonen": "102"
              },
              "children": [
                {
                  "data": {
                    "Singalcode": "4",
                    "aantalPersonen": "15"
                  }
                },
                {
                  "data": {
                    "Singalcode": "5",
                    "aantalPersonen": "10"
                  }
                }
              ]
            },
            {
              "data": {
                "Vestiging": "Bangalore",
                "aantalPersonen": "82"
              },
              "children": [
                {
                  "data": {
                    "Singalcode": "6",
                    "aantalPersonen": "15"
                  }
                }
              ]
            }
          ]
        },
        {
          "data": {
            "Briname": "Abhinav",
            "aantalPersonen": "122"
          },
          "children": [
            {
              "data": {
                "Vestiging": "Bangalore",
                "aantalPersonen": "102"
              },
              "children": [
                {
                  "data": {
                    "Singalcode": "4",
                    "aantalPersonen": "15"
                  }
                }
              ]
           }
          ]
        }
      ]
    }

Here the parent having "Briname" as key data element name and children node having "vestging and "Singalcode" as data element.

How we can achieve this using tree table?

HTML :

<p-treeTable [value]="files2" [columns]="cols" selectionMode="single" [(selection)]="selectedNode1" (onNodeSelect)="nodeSelect($event)">
  <ng-template pTemplate="body" let-rowNode let-rowData="rowData" let-columns="columns" >
    <tr [ttSelectableRow]="rowNode">
      <td *ngFor="let col of columns; let i = index">
        <p-treeTableToggler [rowNode]="rowNode" *ngIf="i == 0"></p-treeTableToggler>
        {{rowData[col.field]}}
      </td>
    </tr>
  </ng-template>
</p-treeTable>

So you want to display first property of data

Let declare _object = Object; in ts file

Then display first property on HTML file

{{ rowData[_object.keys(rowData)[0]] }}

Demo here

I think property names need to be remapped and fixed.

like this:

{
  "data": [
    {
      "data": {
        "name": "Briname: Aamir",
        "aantalPersonen": "122"
      },
      "children": [
        {
          "data": {
            "name": "Vestiging: Ranchi",
            "aantalPersonen": "102"
          },
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM