简体   繁体   English

如何在 angular 的primeng 表中显示嵌套的 json 数据

[英]How To display nested json data in primeng table in angular 7

I want to display my nested json data into my prime ng table, but I am facing an issue that when I am calling the data through the http service, its an object form and when I display it directly into table it gives me [object][object].我想将嵌套的 json 数据显示到我的prime ng表中,但我面临一个问题,当我通过 http 服务调用数据时,它的 object 将其直接显示到表中[目的]。

The way I want to display the data is that suppose I have a json like this, in a single row I want to print the whole nested json with key and value seperate我想显示数据的方式是假设我有一个像这样的 json,在一行中我想打印整个嵌套的 json,键和值分开

{{item.key}} and {{item.value}}. {{item.key}} 和 {{item.value}}。

Anything we can do make a function and return the key and value and print it in table.我们可以做的任何事情都可以创建 function 并返回键和值并将其打印在表中。 Something like this像这样的东西

    {"hits":[
  {
    "cardetails": {
    "carname":"abc",
    "cartype":"metal",
    "carnumber": "4444"
    }
  },
  {
    "cardetails": {
    "carname":"cde",
    "cartype":"alumnium",
    "carnumber": "1212"
  }
  }
]}

my.ts file我的.ts 文件


  ngOnInit() {
     this.carservice.getcardata().subscribe(carservice => {
     this.tables = carservice
     console.log(this.tables);
     this.formatted=(this.tables.hits)
     console.log(this.formatted)//here this is my json object

  }

    this.cols = [
      { field: ''},
      { field: 'cardetails'},
      { field: ''},

  ];


  this.data.length < this.rows ? this.temDataLength = this.data.length : this.temDataLength = this.rows;
  }

  expandAll() {
    if(!this.isExpanded){
      this.data.forEach(data =>{
        this.expandedRows[data.time] = 1;
      })
    } else {
      this.expandedRows={};
    }
    this.isExpanded = !this.isExpanded;
  }
  onRowExpand() {

    console.log("row expanded", Object.keys(this.expandedRows).length);
    if(Object.keys(this.expandedRows).length === this.temDataLength){
      this.isExpanded = true;
    }
  }

  onRowCollapse() {

    console.log("row collapsed",Object.keys(this.expandedRows).length);
    if(Object.keys(this.expandedRows).length === 0){
     this.isExpanded = false;
    }
  }
}

.html file .html文件

<p-table  [columns]="cols" dataKey="speed" [value]="cars" (onPage)="onPage($event)"
            (onRowExpand)="onRowExpand()" (onRowCollapse)="onRowCollapse()" 
            [expandedRowKeys]="expandedRows" [rowExpandMode]='single' [resizableColumns]="true">
        <ng-template pTemplate="header" let-columns>
            <tr>
                <th style="width: 24px;background-color:#2F4A6A;">
                </th>
                <th style="width:7em;background-color:#2F4A6A;color:white;width:85px;height:33px;font-size:12px;text-align:left"><span>cars ranking</span>
                 </th>
                <th style="background-color:#2F4A6A;color:white;width:600px;height:33px;padding:8px;font-size:12px;text-align:left"><span>Car Detail in a single row</span>
                </th>
                <th style="width: 24px;background-color:#2F4A6A;" ></th>
            </tr>
        </ng-template>
        <ng-template let-rowIndex="rowIndex"  pTemplate="body" let-rowData let-expanded="expanded" let-columns="columns" let-index>
            <tr>
                <td>
                    <a href="#" [pRowToggler]="rowData">
                        <i  style="color:black;font-size:14px;"[ngClass]="expanded ? 'pi pi-caret-down ': 'pi pi-caret-right'"> </i></a>
                </td>
                <td *ngFor="let col of cols">
                <div *ngIf="col.field==source;then nested_object_content else normal_content"></div>
                    <ng-template #nested_object_content>
                            {{rowData[col.field.key]}} -- {{rowData[col.field.value]}}
                    </ng-template>
                    <ng-template #normal_content>
                            {{rowData[col.field]}}
                    </ng-template>
                </td>
            </tr>
        </ng-template>

      </p-table>

I would recommend you to parse the JSON in-plane object format before assigning it to the table.我建议您在将 JSON 平面内 object 格式分配给表之前对其进行解析。 With that JSON structure, even if you achieve with nested object, you may have to override a few table functionality like searching, sorting, etc. That will lead to unnecessary issues and cause performance issues.使用 JSON 结构,即使您使用嵌套的 object 实现,您也可能必须覆盖一些表功能,如搜索、排序等。这将导致不必要的问题并导致性能问题。 Better convert your data into flat formate.更好地将您的数据转换为平面甲酸盐。 It will be easy and plain implementation.这将是简单而简单的实施。

Please read the below question thread, similar problems cause some of the basic implementation issues on T-table.请阅读下面的问题线程,类似的问题会导致 T-table 上的一些基本实现问题。

T-table With Nested object 带嵌套的 T 型表 object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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