【发布时间】:2020-08-10 03:54:03
【问题描述】:
以下是 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 列表,如您在 JSON 中看到的 Rockey、Jony,并通过使用考试数组,我想在 ag 网格中创建动态标题及其动态数据。 假设用户从下拉列表中选择了 Rockey,那么标题应该是“状态”、“时间戳”和“用户 ID”,当用户从下拉列表中选择 Jony 时,ag 网格会更改标题以及类似的数据 “报告”、“通过”、“报告 ID”。
<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);
}
我附上了我的标题输出截图。它出现在一个地方,带有“,”以及显示“没有要显示的行”的行。
【问题讨论】:
标签: angular7 ag-grid ag-grid-angular