I have below form:
And below is the table in which I am adding array elements:
I have a form from which I am adding values in an array using below code:
const {updateMode, lead_id, year, probability, plan2, plan3, fee, addcosts} = this.state;
this.setState(state => ({
lead_plans: [...state.lead_plans, {
year: year,
probability: probability,
plan2: plan2,
plan3: plan3,
fee: fee,
addcosts: addcosts
}]
}));
Upon clicking edit button I am using below code:
let added_lead_plan = this.state.lead_plans.find(lead_plan_rec => lead_plan_rec.year === lead_plan.year);
this.setState({
plan_id: added_lead_plan.id,
year: added_lead_plan.year,
probability: added_lead_plan.probability,
plan2: added_lead_plan.plan2,
plan3: added_lead_plan.plan3,
fee: added_lead_plan.fee,
addcosts: added_lead_plan.addcosts,
LeadPlanSaveUpdateDialogOpen: true,
});
For deleting I am using below code:
this.setState(prevState => ({lead_plans: prevState.lead_plans.filter(lead_plan_rec => lead_plan_rec !== lead_plan_row)}));
Now,
Add Works fine. Edit allows me to show the right values in edit form based on finding by date.
Problems:
I cant update the edited element in the array. Instead, it adds that records in the array again when it should update it. But if someone, will edit the date how I can update the record based on date only - So, thats the other problem.
Requirements:
I need a way in which person can easily add as many values from form. Edit them one by one and update the only those in the array which got updated and delete the respective ones which were clicked delete.
Edit
{
(updated_lead_plans || existing_lead_plans || lead_plans).map((lead_plan, index) => (
<TableRow key={`mi-${index}`}>
<TableCell align="right" colSpan={2}>
<TableCell align="right">
<IconButton>
<EditIcon
onClick={() => this.handleUpdateFields(lead_plan)}/>
</IconButton>
</TableCell>
<TableCell align="right">
<IconButton>
<DeleteIcon
onClick={() => this.launchDeleteLeadPlanDialog(lead_plan)}/>
</IconButton>
</TableCell>
</TableCell>
<TableCell align="right">{moment(lead_plan.year).format("DD.MM.YYYY")}</TableCell>
<TableCell align="right">{this.getFormattedNumber(lead_plan.probability)}</TableCell>
<TableCell align="right">{this.getFormattedNumber(lead_plan.plan2)}</TableCell>
<TableCell align="right">{this.getFormattedNumber(lead_plan.plan3)}</TableCell>
<TableCell align="right">{this.getFormattedNumber(lead_plan.fee)}</TableCell>
<TableCell
align="right">{this.getFormattedNumber(lead_plan.addcosts)}</TableCell>
</TableRow>
))
}
handleUpdateFields = (lead_plan) => {
let added_lead_plan = this.state.lead_plans.find(lead_plan_rec => lead_plan_rec.year === lead_plan.year);
console.log('GGGG', added_lead_plan);
this.setState({
plan_id: added_lead_plan.id,
year: added_lead_plan.year,
probability: added_lead_plan.probability,
plan2: added_lead_plan.plan2,
plan3: added_lead_plan.plan3,
fee: added_lead_plan.fee,
addcosts: added_lead_plan.addcosts,
LeadPlanSaveUpdateDialogOpen: true,
});
}
updateLeadPlanDialog = () => {
const {updateMode, lead_id, plan_id, year, probability, plan2, plan3, fee, addcosts} = this.state;
this.state.lead_plans.filter(lead_plan_rec => lead_plan_rec.year !== year);
this.setState(state => ({
lead_plans: [...state.lead_plans, {
year: year,
probability: probability,
plan2: plan2,
plan3: plan3,
fee: fee,
addcosts: addcosts
}]
}));
}

