【发布时间】:2019-01-20 05:15:20
【问题描述】:
需要有关嵌套数据的 Angular 数据表的帮助。
我想对表格中的数据进行排序。
我正在使用来自 - https://www.npmjs.com/package/angular2-datatable 的数据表
数据表适用于单一数组类型的数据。 (用于许多角度应用)
问题:我有嵌套的json(实际上,我有复杂的json,这里很简单)
感谢您对此进行调查。
感谢任何建议或帮助。
JSON
records = [
[
{
"name": "Subject Name",
"type": "text",
"id": "subjectName",
"value": "DavidJ",
"firstName": "David",
"lastName": "John"
},
{
"name": "QC Name",
"type": "hidden",
"id": "qcName",
"value": "JosephT",
"firstName": "Joseph",
"lastName": "Tom"
}
],
[
{
"name": "Subject Name",
"type": "text",
"id": "subjectName",
"value": "TigerC",
"firstName": "Tiger",
"lastName": "Chan"
},
{
"name": "QC Name",
"type": "hidden",
"id": "qcName",
"value": "ThomasR",
"firstName": "Thomas",
"lastName": "Richard"
}
]
]
HTML
<table class="table table-responsive table-hover" [mfData]="this.records | dataFilter : filterQuery" #mf="mfDataTable" [mfRowsOnPage]="rowsOnPage" [(mfSortBy)]="sortBy" [(mfSortOrder)]="sortOrder">
<thead>
<tr>
<th>#</th>
<th>
<mfDefaultSorter by="subjectName">subject Name</mfDefaultSorter>
</th>
<th>
<mfDefaultSorter by="qcPerson">QC Person</mfDefaultSorter>
</th>
</tr>
</thead>
<tbody *ngIf="!isLoading">
<tr class="border" *ngFor="let sample of mf.data; let i='index'">
<td>{{i + 1}}</td>
<ng-container *ngFor="let item of sample">
<td *ngIf="item.id ==='subjectName'">
{{item.firstName}} {{item.lastName}}
</td>
<td *ngIf="item.id ==='qcPerson'">
{{item.firstName}} {{item.lastName}}
</td>
</ng-container>
</tr>
</tbody>
</table>
打字稿文件
import { Component, OnInit } from '@angular/core';
import { OrderBy } from '../all_services/OrderByPipe';
@Component({
selector: 'app-userdashboard',
templateUrl: './userdashboard.component.html',
styleUrls: ['../header-footer/css/external.style.css']
})
export class UserdashboardComponent implements OnInit {
constructor() {}
ngOnInit() {}
/** Sorting functions */
public data;
public filterQuery = "";
public rowsOnPage = 10;
public sortBy = "subjectName";
public sortOrder = "asc";
public toInt(num: string) {
return +num;
}
}
Datafilterpipe.ts
import * as _ from "lodash";
import {Pipe, PipeTransform} from "@angular/core";
@Pipe({
name: "dataFilter"
})
export class DataFilterPipe implements PipeTransform {
transform(array: any[], query: string): any {
if (query) {
return _.filter(array, row=>row.name.indexOf(query) > -1);
}
return array;
}
}
【问题讨论】:
-
您的问题不清楚 - 您想做什么需要帮助?
-
@GabrielDoty,如果我不清楚,请道歉。我想对表格中的数据进行排序。
-
为什么要在数组中使用数组,不能简单地将所有对象放在一个数组中,这样对象数组的排序会容易得多
-
在填充表格之前展平 JSON 怎么样? angular-yfpnk4.stackblitz.io
-
@Prasanna Sasne,恐怕我做不到。我在许多其他组件中使用相同的 json。我有复杂的嵌套 json,在这里我显示非常简单的 json
标签: javascript json angular datatable nested