This is a migrated thread and some comments may be shown as answers.

Add new row to grid dynamically

8 Answers 2185 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Binu
Top achievements
Rank 1
Binu asked on 04 Dec 2019, 11:07 AM

Hi All

         I want to add new row to grid using react. How its possible?

8 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 04 Dec 2019, 11:12 AM

Hello, Binu,

A new row can be added by adding a new item in the array bound to the Grid.

Please check the following demo, where we dynamically add more rows to the Grid on a button click.

The rows can be added on any custom event.

https://www.telerik.com/kendo-react-ui/components/grid/editing/editing-inline/

Regards,
Stefan
Progress Telerik

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Len
Top achievements
Rank 1
answered on 04 Mar 2020, 08:54 AM

I'm stuggling with my own ignorance... so please pardon me

But I can't get it right to add a row to an empty array (lets say the first time..)

This is my addRecord function ( after the 250th attempt to set state...  :-(   )

addRecord = () => {
let tsks = JSON.parse(JSON.stringify(this.state.tasks));
console.log('tsks=',tsks)
if (this.state.dropdownlistPLine === null || this.state.dropdownlistFinProd === null || this.state.dropdownlistSSchema === null) {
this.toggleDialog()
} else {
const newRecord = {
rowid: tsks.length+1,
ProdLine: this.state.dropdownlistPLine,
FinalProduct: this.state.dropdownlistFinProd,
sschema: this.state.dropdownlistSSchema,
StockonHand: 0,
StockonOrder: 0,
StockFree: 0,
TotKg: 0,
Weight: 0,
totpipes: 0,
actid: 0
};
console.log('New tasks', newRecord);
if (tsks.length > 0) {
this.setState({
tasks: [newRecord, ...tsks],
editID: newRecord.rowid
});
} else {
console.log('Adding first tasks', newRecord);
tsks.push(newRecord);
console.log('tsks = ', tsks);
this.setState({
tasks: this.state.tasks.concat(newRecord),
editID: newRecord.rowid
});
}
}
console.log('New tasks', this.state.tasks)
};

 

No matter what I tried to do, it just won't set the state... here's the console.log

tsks= []
New tasks {rowid: 1, ProdLine: "Mixer 1", FinalProduct: "MX-16422O", sschema: 4, StockonHand: 0, …}rowid: 1ProdLine: "Mixer 1"FinalProduct: "MX-16422O"sschema: 4StockonHand: 0StockonOrder: 0StockFree: 0TotKg: 0Weight: 0totpipes: 0actid: 0

Adding first tasks {rowid: 1, ProdLine: "Mixer 1", FinalProduct: "MX-16422O", sschema: 4, StockonHand: 0, …}rowid: 1ProdLine: "Mixer 1"FinalProduct: "MX-16422O"sschema: 4StockonHand: 0StockonOrder: 0StockFree: 0TotKg: 0Weight: 0totpipes: 0actid: 0__proto__: Object
tsks =  [{…}]0: rowid: 1ProdLine: "Mixer 1"FinalProduct: "MX-16422O"sschema: 4StockonHand: 0StockonOrder: 0StockFree: 0TotKg: 0Weight: 0totpipes: 0actid: 0

New tasks []

Any help will be appreciated

 

Len

 

 

0
Stefan
Telerik team
answered on 04 Mar 2020, 10:39 AM

Hello, Len,

Thank you for the code.

I used the same code => copy and paste, add it to a Grid and the row was successfully added:

https://stackblitz.com/edit/react-t6asdt?file=app/main.jsx

If the issue still occurs, please modify the example to replicate the issue and I will be happy to inspect it.

Regards,
Stefan
Progress Telerik

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Len
Top achievements
Rank 1
answered on 05 Mar 2020, 06:22 AM

Two things I must admit in advance ..

1) I honestly do not know how to code in JSX... I just Copy and past from examples

2) I only want things to work and in many cases I don't know what I'm doing...

I attach my entire component. It calls a API from the backend server to populate the relevant json objects

To add a new record you have to choose the prodiction line, the final product and the shiftschema

Choose any from the list

Thank you for assistance

 

Len

 

0
Len
Top achievements
Rank 1
answered on 06 Mar 2020, 05:09 AM

It seems that the problem lies with webpack but I dont know what it can be..

I created a react app (create-react-app) using PyCharm. I then copied my component there, installed all the dependencies and the app works fine.

But in the real world I have to compile a webpack bundle andthen render it from my Django backend html.

Now this works fine for some other components but not this one.

It just does not sound logical

I attach my package.json and webpack.config.js

Any help will be appreciated

Len

Ps: I quote... "A miracle is an event not explicable by natural or scientific laws. The rest have logical explanations"

0
Len
Top achievements
Rank 1
answered on 06 Mar 2020, 05:11 AM

Attached in Zip file

Len

0
Len
Top achievements
Rank 1
answered on 06 Mar 2020, 06:40 AM

Apologies....

Just as I was ready to believe in miracles, I found the logical explanation

My keys array was empty and  therefor no definition for fields of the grid...

Len

Ps:... With help from my friend Jack Daniels

0
Dimo
Telerik team
answered on 06 Mar 2020, 03:37 PM

Hello Len,

I am happy to see that you have managed to get to the root of the case and resolve it! Let us know if any other questions arise.

If only the assistance of Mr. Jack Daniels was always that consistent and predictable ... :)

Have a great weekend!

Best regards,
Dimo
Progress Telerik

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Mahesh
Top achievements
Rank 1
Iron
commented on 21 Mar 2024, 07:24 AM | edited

Hi Len,

I hope you are doing well.

With the help of this, the additional column is added to the Grid without interaction with any custom JSON file.
I need something like the below-attached screenshot. When the Discontinued will be true then only the custom cell buttons will be visible otherwise not.

https://stackblitz.com/run/?file=app%2Fsample-products.jsx

I tried several ways but was not able to succeed.

Please let me know what we can do to achieve the requirement.

Looking for a positive revert.

Thanks,
Mahesh

 

Vessy
Telerik team
commented on 22 Mar 2024, 06:18 PM

Hi, Mahesh,

I have just answer to your other post on the same matter, you can access my reply here:

Regards,

Vessy

Tags
General Discussions
Asked by
Binu
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Len
Top achievements
Rank 1
Dimo
Telerik team
Share this question
or