下面是JSON数据
var dataset =
[{
"studentname": "Rockey",
"age": 13,
"average": 8.2,
"exam": [{"Status": "Pass", "TimeStamp": "2019-03-12 08:19:50", "UserId": "123"},
{"Status": "Pass", "TimeStamp": "2019-03-12 08:19:50", "UserId": "456"},
{"Status": "Fail", "TimeStamp": "2019-03-12 08:19:50", "UserId": "789"}]
},
{
"studentname": "Jony",
"age": 13,
"average": 8.2,
"exam": [{"Report": "Pass", "TimeofReport": "2019-03-12 08:19:50", "ReportId": "333"},
{"Report": "Pass", "TimeofReport": "2019-03-12 08:19:50", "ReportId": "444"},
{"Report": "Fail", "TimeofReport": "2019-03-12 08:19:50", "ReportId": "555"}]
}]
使用上述数据,我有一个下拉列表,其中包含所有StudentName列表,如Rockey和Jony,如您在JSON中看到的那样,并且我想通过使用检查数组在ag网格中创建动态标头及其动态数据。 假设用户从下拉列表中选择了Rockey,则标题应为“ status”,“ TimeStamp”和“ Userid”,当用户从下拉列表中选择Jony时,ag网格将更改标题以及诸如 “报告”,“通过”,“ ReportId”。
<ag-grid-angular "
class="ag-theme-balham"
#agGrid
[columnDefs]="columnDefs"
id="newGrid"
[enableSorting]="true"
[enableFilter]="true"
[modules]="modules"
[paginationAutoPageSize]="true"
[rowData]="rowData"
[rowSelection]="rowSelection"
(gridReady)="onGridReady($event)"
[pagination]="true">>
</ag-grid-angular>
变量声明部分
tableData: string[] = [];
tableList: string[] = [];
tableName: string;
pushHeader:any=[{}];
exam: any;
tableColumns: [{headerName: string, field: string}] = this.pushHeader;
tableRecord: {};
构造函数逻辑:
constructor() {
this.GetGridData();
this.gridOptions = <GridOptions>{
rowData: this.tableData,
columnDefs: this.tableColumns, //Collecting headers and pushing to grid
onGridReady:(event: any) => {
this.gridOptions.api = event.api;
this.gridOptions.api.setColumnDefs(this.tableColumns);
this.gridOptions.api.setRowData(this.rowData);
console.log("Headers",this.tableColumns);
}
}
}
收集动态标头及其关联数据的方法
public GetGridData() {
//This is commented code I was trying with gridApi method but no output generated so ignore below commented code
//var params = { force: true };
// this.gridApi = params["api"];
// this.gridcolumnApi = params["columnApi"];
// params["api"] = this.exam;
//I have applied logic to process on exam array if user select studentname like Rockey from drop
down then its corresponding data should display in grid
this.exam=[{"Status": "Pass", "TimeStamp": "2019-03-12 08:19:50", "UserId": "123"},
{"Status": "Pass", "TimeStamp": "2019-03-12 08:19:50", "UserId": "456"},
{"Status": "Fail", "TimeStamp": "2019-03-12 08:19:50", "UserId": "789"}]
let header: any = [];
Object.keys(this.exam[0]).forEach(function (key) {
header.push(key)
});
this.tableColumns.push({ "headerName": header, "field": header });
this.gridOptions.api.setColumnDefs(this.tableColumns);
this.gridOptions.rowData = this.exam;
this.gridOptions.api.setRowData(this.rowData);
}
我附上了标题的输出屏幕截图。它以“,”和一个显示“没有要显示的行”的行出现在一个地方。
答案 0 :(得分:1)
您的标头是一个数组。然后您将整个数组分配给tableColumns
相反,您应该执行以下操作-
let headers: any = [];
Object.keys(this.exam[0]).forEach(function (key) {
header.push(key)
});
//iterate through headers array, construct colDef and push to tableColumns
headers.forEach((header) => {
this.tableColumns.push({ "headerName": header, "field": header });
});