Reputation: 155
below is JSON data
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"}]
}]
Using Above data i have one drop-down where i have all list of StudentName like Rockey , Jony as you see in JSON and by using exam array i want to create dynamic header along with its dynamic data in ag grid. suppose user has selected Rockey from drop-down then header should be "status", "TimeStamp" and "Userid" and when user select Jony from drop-down then ag grid change header along with data like "Report","Pass","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>
Variable declaration part
tableData: string[] = [];
tableList: string[] = [];
tableName: string;
pushHeader:any=[{}];
exam: any;
tableColumns: [{headerName: string, field: string}] = this.pushHeader;
tableRecord: {};
constructor logic:
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);
}
}
}
Method for collecting dynamic headers and its associate data
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);
}
I have attached my output screenshot of headers. it coming in one place with taking "," and about rows showing "no rows to show".
Upvotes: 0
Views: 1234
Reputation: 7614
Your header is an array. And you are assigning the entire array to tableColumns
Instead you should do something like this -
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 });
});
Upvotes: 1