Grid Editing Inline component inserts two rows when I add a new row

0 Answers 59 Views
Grid
Rebeca
Top achievements
Rank 1
Iron
Rebeca asked on 30 Nov 2021, 10:22 AM | edited on 30 Nov 2021, 10:26 AM

Hi telerik team

I'm trying to implement the "add new row" function in a Grid >Editing >inline component.

I have followed the steps as you have in the documentation, but somehow, every time I trigger the "add" function it inserts new two rows with the same values and same id.

this is my code. Any ideas what I'm doing wrong? Thanks!

const GridComponent= () => {
const editField = "inEdit";
const [data, setData] = React.useState([]);

 

const functionToGetInfoInFromAPI{ setData(result.data)}

useEffect(()=>{
functionToGetInfoInFromAPI()
}, [])
const generateId = (data) => {
let highestId = 0;
data.forEach((item) => {
if(item.casystem_id > highestId) {
highestId = item.casystem_id;
}
})
return highestId+1;
}

 

const insertItem = (item) => {
item.inEdit = false;
item.casystem_id = generateId(data);
const newDataArray = [item, ...data];
return newDataArray;


};

const add = (dataItem) => {
dataItem.
inEdit = true;
const newData = insertItem(dataItem);
setData(newData);

}
;

const itemChange = (event) => {
const field = event.field || "";
const newData = data.map((item) =>
item.casystem_id === event.dataItem.casystem_id
? { ...item, [field]: event.value }
: item
);
setData(newData);
};

const addNew = () => {
const newDataItem = {
inEdit: true,
Discontinued: false,
};
setData((oldData) => [newDataItem, ...oldData]);

};

 

export const MyCommandCell = props => {
const {
dataItem
} = props;
const inEdit = dataItem[props.editField];
const isNewItem = dataItem.casystem_id === undefined;
return inEdit ? <td className="k-command-cell">
<button className="k-button k-grid-save-command" onClick={() => isNewItem ? props.add(dataItem) : props.update(dataItem)}>
{isNewItem ? "Add" : "Update"}
</button>
<button className="k-button k-grid-cancel-command" onClick={() => isNewItem ? props.discard(dataItem) : props.cancel(dataItem)}>
{isNewItem ? "Discard" : "Cancel"}
</button>
</td> : <td className="k-command-cell">
<button className="k-primary k-button k-grid-edit-command" onClick={() => props.edit(dataItem)}>
Edit
</button>
<button className="k-button k-grid-remove-command" onClick={() => window.confirm("Confirm deleting: " + dataItem.comp_nickname) && props.remove(dataItem)}>
Remove
</button>
</td>;
};
<Grid
data={data}
onItemChange={itemChange}
editField={editField}
dataItemKey={"casystem_id"}
>
<GridToolbar>
<button title="Add new" className="k-button k-primary" onClick={addNew}>
Add new
</button>
</GridToolbar>
<Column field="casystem_id" title="id" editable={false} />
<Column field="nickname" title="Nickname" />
<Column cell={CommandCell} width="240px" />
</Grid>

 

 

 

Stefan
Telerik team
commented on 30 Nov 2021, 02:31 PM

Hello, Rebeca,

Thank you for the code.

Could be please clarify if this occurs when we click the `Add new` button in the GridToolbar or the `Add` for the row? In general, I see that we add a new item to the array inside the `addNew` method and then we add a new item in the `insertItem` method.

Rebeca
Top achievements
Rank 1
Iron
commented on 01 Dec 2021, 02:26 PM

Hi Stefan,

I did another approach to modify the item, always inside the "setData" function and it worked.

I think that accessing and modifying the item from outside that function was messing with React State and it was giving inconsistency when inserting the new item. That way it worked fine.

No answers yet. Maybe you can help?

Tags
Grid
Asked by
Rebeca
Top achievements
Rank 1
Iron
Share this question
or