Reputation: 1333
But the source of data is this array.
So I only see projectId and projectName in the grid.
Is it possible assign this array or I might change his structure? Thanks
Upvotes: 1
Views: 400
Reputation: 1465
In order to be able to set the data through the setRowData
method of ag-Grid API, you have to make sure, that either of data entry is consistent between any other.
You can try to consider the following ways to solve this.
Data transformation on back-end side
It might be the case if you are full of control over the back-end API, just send right back to the client the data in the most appropriate way for you.
Splitting the process of setting data from setRowData
You may want to introduce some additional methods for your component with data-grid e.g. one for setting projectId
and projectName
and another for setting calendarValues
. ag-Grid docs offers up to 4 different ways of updating the data.
Defining custom valueGetter
for each column
You can try to modify definitions for columnDefs
by introducing custom valueGetter
which would take care of extracting appropriate value e.g.
const columnDefs = [
{
headerName: "Febrero2019",
field: "Febrero2019",
valueGetter({ data: { calendarValues } }) {
return calendarValues['Febrero2019'];
}
},
];
api.setRowData
method.const colDefs = [
{ headerName: 'Codigo', field: 'projectId' },
{ headerName: 'Nombre', field: 'projectName' },
{ headerName: 'Febrero2019', field: 'Febrero2019' },
{ headerName: 'Marzo2019', field: 'Marzo2019' }
];
const projectData = {
calendarValues: [
{ date: 'Febrero2019', value: 0 },
{ date: 'Marzo2019', value: 0 },
],
projectId: 'SomeId',
projectName: 'SomeName'
};
const processData = ({ projectId, projectName, calendarValues }) => ({
projectId,
projectName,
...calendarValues.reduce((calendar, { date, value }) => ({ ...calendar, [date]: value }), {})
});
so the call processData
will result in the desired data format.
{
Febrero2019: 0
Marzo2019: 0
projectId: "SomeId"
projectName: "SomeName"
}
Upvotes: 1