我在使用JSON数据填充DataTable时遇到问题。我无法访问JSON的一个字段,它是一个数组。目前,它返回android
而不是正确的数据。我使用[object Object]
在组件中动态创建cols。
简化代码:
Vehicle.component.ts
ngOnInit
Vehicle.service.ts
export class VehicleComponent implements OnInit {
cols: any[];
cols2: any[];
ngOnInit() {
this.cols = [
{ field: 'vehicle_id', header: "Vehicle ID" },
{ field: 'dallassettings', header: 'Dallas settings' },
{ field: 'dallasupdated', header: 'Dallas updated' },
{ field: 'dallas_list', header: 'Dallas list' }
];
this.cols2 = [
{ field: 'number', header: 'Number' },
{ field: 'auth', header: 'Auth' },
{ field: 'dallas_list', header: 'Dallas list' }
]; //testing out other possibilities
}
public vehicles: GeneralVehicle[];
constructor(private vehicleService: VehicleService, private router: Router) {
this.vehicleService.getVehicle().subscribe(vehicle => {
this.vehicles = vehicle;
});
}
interface GeneralVehicle {
status: number;
dallases: Vehicle[];
}
interface Vehicle {
vehicle_id: number;
dallassettings: string;
dallasupdated: string;
dallas_list: DallasList[];
}
interface DallasList {
number: number;
auth: number;
}
vehicle.html
export class VehicleService {
private defUrl = 'dummy.url';
constructor(private http: Http) { }
getVehicle(username?: string, password?: string) {
const url = (!username || !password) ? this.defUrl : 'dummy.url' + username + '/' + Md5.hashStr(password);
return this.http.get(url)
.map(res => res.json());
当我尝试从 <div *ngIf="vehicles">
<p-dataTable [value]="vehicles.dallases">
<p-column *ngFor="let col of cols" [field]="col.field" [header]="col.header"></p-column>
</p-dataTable>
</div>
<!--Testig out other possibilities-->
<div *ngIf="vehicles">
<p-dataTable [value]="vehicles.dallas_list">
<p-column *ngFor="let col2 of cols2" [field]="col2.field" [header]="col2.header"></p-column>
</p-dataTable>
</div>
数组中获取number
和auth
时:
这就是JSON的样子:
dallas_list
如您所见,我对{
"status": 0,
"dallases": [{
"vehicle_id": 17954,
"dallassettings": "3",
"dallasupdated": "False",
"dallas_list": [{
"number": 666111222,
"auth": 3
}, {
"number": 666777888,
"auth": 4
}, {
"number": 123454321,
"auth": 4
}]
}
}
字段内的数据感兴趣。
预期的cols输出:vehicle_id,dallassettings,dallasupdated,number,auth。
答案 0 :(得分:1)
在json对象中,dallases_list处于dallases下。所以在从该索引访问dallases后,您需要访问dallases_list
public class SearchRequest
{
[JsonProperty("jql")]
public string JQL { get; set; }
[JsonProperty("startAt")]
public int StartAt { get; set; }
[JsonProperty("maxResults")]
public int MaxResults { get; set; }
[JsonProperty("fields")]
public List<string> Fields { get; set; }
[JsonProperty("SearchRequest")]
public SearchRequest()
{
Fields = new List<string>();
}
}
public void ViewCases_Load(object sender, EventArgs e)
{
SearchRequest request = new SearchRequest();
request.JQL = "test ORDER BY UPDATED DESC";
request.MaxResults = 50;
request.StartAt = 0;
string data = JsonConvert.SerializeObject(request);
}
答案 1 :(得分:1)
看来你有一个嵌套数组,所以你想如何在数据表中显示这些信息?
'dallases'数组中的每辆车都有一个包含多个'number / auth'条目的子列表(dallas_list)。
您可以转换列表以使其变平,因此'dallas_list'中的每个条目都有1行。
或者,PrimeNG数据表支持row expansions - 因此您可以将数据保留在当前表单中,并让扩展模板显示数字/身份验证详细信息。