【发布时间】:2021-02-04 04:35:35
【问题描述】:
我在我的应用程序中使用primeng table 在表格中显示数据。我的数据有一些嵌套的 json 对象。我正在尝试根据嵌套的 json 对象对表进行排序和过滤,但无法找到这样做的方法。请帮忙。提前致谢。
这是我的 json:
[
{
"roll-number":"45",
"name":"Ron",
"subject-info":[
{
"subject-marks-":"40",
"subject-name":"English"
}
]
},
{
"roll-number":"46",
"name":"Daryl",
"subject-info":[
{
"subject-marks":"20",
"subject-name":"English"
}
]
}
]
这是我的 cols 数组:
this.cols = [
{ header: 'Student Name', field: 'name' },
{ header: 'Student Roll No', field: 'roll-number' },
{ header: 'Subject name', field: 'subject-info',subfield:'subject-name' },
{ header: 'Subject marks', field: 'subject-info',subfield:'subject-marks' },
];
这是我的模板:
<p-table [columns]="cols" #dt [value]="studentData" >
<ng-template pTemplate="caption">
<div style="text-align: right">
<i class="fa fa-search" style="margin:4px 4px 0 0"></i>
<input type="text" pInputText size="30" placeholder="Search" (input)="dt.filterGlobal($event.target.value, 'contains')" [value]="dt.filters['global']?.value" style="width:auto">
</div>
</ng-template>
<ng-template pTemplate="header" let-columns>
<tr>
<th *ngFor="let col of columns " [pSortableColumn]="col.field " class="row-header ">
{{col.header}}
<p-sortIcon class=" " [field]="col.field " ariaLabel="Activate to sort " ariaLabelDesc="Activate to sort in descending order " ariaLabelAsc="Activate to sort in ascending order ">
</p-sortIcon>
</th>
</tr>
</ng-template>
<ng-template pTemplate="body" let-data let-columns="columns ">
<tr class="center-text ">
<td *ngFor="let col of columns " class="row-cell ">
<div *ngIf="col.subfield && data[col.field].length!=0;then nested_object_content else normal_content"></div>
<ng-template #nested_object_content>
{{data[col.field][0][col.subfield]}}
</ng-template>
<ng-template #normal_content>
{{data[col.field]}}
</ng-template>
</td>
</tr>
</ng-template>
</p-table>
目前,如果我尝试根据主题标记或主题名称列进行排序,则表格将使用主题信息字段进行排序,因为 sortcol 是 col.field。我正在尝试根据用户的主题标记或主题名称对表格进行排序。
使用上述列中的值进行过滤也会返回空响应。
【问题讨论】:
标签: javascript html angular typescript primeng