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

Add Row Programatically

2 Answers 1833 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 10 Apr 2012, 12:50 PM
My solution requires us to add rows programatically. There is a complication in that the rows need to be added to a subgrid. Looks like it works but the data is now being saved. To test our problem, I have added a button which runs the function below on your grid-odata-crud example which I downloaded from github.

            function AddRow() {
                var productsGrid = $('#grid').data('kendoGrid');
                var dataSource = productsGrid.dataSource;
                dataSource.add({ ProductID: 10002, ProductName: 'A Johns Product 1', UnitPrice: 10.00, UnitsInStock: 5, Discontinued: false });
                dataSource.sync();
                console.log('Done');
            }

Should the above work. No rows are added to the database using this technique although I can add rows in the usual manner via the 'Add new record' button.

Hope you can help.

2 Answers, 1 is accepted

Sort by
0
Mahaveer
Top achievements
Rank 1
answered on 11 Apr 2012, 08:50 AM
Hi,

yes your solution will work.
You can add row like this also ,
                dataSource.insert(0,{ ProductID: 10002, ProductName: 'A Johns Product 1', UnitPrice: 10.00, UnitsInStock: 5, Discontinued: false}); This will row add on particular position in table.

but to add or insert one important thing : You have to add schema of columns in datasource. So when you add a new row datasourse will  verify it against with it. otherwise you will get the following error on console.
this._set[undefined] is not an object.

dataSourse schema :
var   dataSource = new kendo.data.DataSource({
        data: jsonObj,
           batch:true,
            schema: {
                       model: {
                          fields: {
                           "ProductID": { editable: true, nullable: true },
                           "ProductName":{ editable: true, nullable: true },
                           "UnitPrice":{ editable: true, nullable: true },
                           "UnitsInStock":{ editable: true, nullable: true },
                           "Discontinued":{ editable: true, nullable: true }
                           }
                      }
                }
        });
0
Sanket
Top achievements
Rank 1
answered on 11 Apr 2013, 05:35 AM
Thanks that worked for me Mahaveer
Tags
Data Source
Asked by
John
Top achievements
Rank 1
Answers by
Mahaveer
Top achievements
Rank 1
Sanket
Top achievements
Rank 1
Share this question
or