I am trying to update every current value based on the previous value of another property.
Actually, I need to do the task with dates, but just to keep it simple, I'll explain my problem with simple numbers.
I have
Start, which is1Stop, which is already definedEndDuration, which is already defined
My goal
Start=Durationof previous index +Endof previous indexEnd=Start+Stopof current value
To illustrate, I added the screenshot of the table, how it should look like:
Current view:
Code below and sandbox link
import React from "react";
import { AgGridReact } from "ag-grid-react";
import "./styles.css";
import "ag-grid-community/dist/styles/ag-grid.css";
import "ag-grid-community/dist/styles/ag-theme-alpine.css";
function App() {
let start = 1;
const [gridApi, setGridApi] = React.useState(null);
const [gridColumnApi, setGridColumnApi] = React.useState(null);
const onGridReady = (params) => {
setGridApi(params.api);
setGridColumnApi(params.columnApi);
};
const defaultColDef = {
flex: 1,
editable: true
};
const columnDefs = [
{
headerName: "Name",
field: "name"
},
{
headerName: "start",
field: "updatedStart"
},
{ headerName: "stop", field: "stop" },
{
headerName: "end",
field: "end"
},
{
headerName: "duration",
field: "duration",
colId: "duration"
}
];
const rowData = React.useMemo(
() => [
{
name: "John",
stop: 10,
duration: 5
},
{
name: "David",
stop: 15,
duration: 8
},
{
name: "Dan",
stop: 20,
duration: 6
}
],
[]
);
const rowDataWithStart = React.useMemo(() => {
return (
rowData &&
rowData.map((row, i) => ({
...row,
start: start
}))
);
}, [start, rowData]);
const rowDataWitEnd = React.useMemo(() => {
return (
rowDataWithStart &&
rowDataWithStart.map((row, i) => ({
...row,
end: row.start + row.stop
}))
);
}, [rowDataWithStart]);
//this function(rowDataWitUpdatedStart) should get the previous value of end and duration and get the total
const rowDataWitUpdatedStart = React.useMemo(() => {
return (
rowDataWitEnd &&
rowDataWitEnd.map((row, i) => ({
...row,
updatedStart: row.end + row.duration
}))
);
}, [rowDataWitEnd]);
return (
<div className="App">
<h1 align="center">React-App</h1>
<div className="ag-theme-alpine">
<AgGridReact
columnDefs={columnDefs}
rowData={rowDataWitUpdatedStart}
defaultColDef={defaultColDef}
domLayout={"autoHeight"}
onGridReady={onGridReady}
></AgGridReact>
</div>
</div>
);
}
export default App;
I have tried to do the task making a use of valueGetter from the library I use: AG Grid, but seems to be not the best approach. See my previous question
Any help will be appreciated


