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

Kendo UI - Insert record in the grid datasource.

1 Answer 913 Views
Grid
This is a migrated thread and some comments may be shown as answers.
aditya
Top achievements
Rank 1
aditya asked on 17 Mar 2019, 04:04 PM

I have used Kendo Grid in my project to display data. Here to insert grid data in my datasource, i have used the bellow code. But in the bellow code in insert statement i have provided field name of grid and have values assigned to it.
For example(AMOUNT: objGridData[idx].AMOUNT)

Hence my requirement is, that i need to do insert in my datasource without specifying field name as in future columns of grid might change for example can we use "datasource.add()".

// Insert Records to the Grid
 for (var idx = 0; idx < objGridData.length; idx++) {
 var newrec= grid.dataSource.insert(idx, {
            AMOUNT: objGridData[idx].AMOUNT,
            id: objGridData[idx].id,
            PERCENT: objGridData[idx].PERCENT,
            PRODUCT1: objGridData[idx].PRODUCT1,
            PRODUCT2: objGridData[idx].PRODUCT2,
            PRODUCT3: objGridData[idx].PRODUCT3,
            PRODUCT4: objGridData[idx].PRODUCT4,
 
         });
}

Any suggestion would be helpful.

Thanks

1 Answer, 1 is accepted

Sort by
0
Accepted
Alex Hajigeorgieva
Telerik team
answered on 19 Mar 2019, 04:12 PM
Hello, Aditya,

If I understand the inquiry correctly, you would like to create new objects with the same properties as the old objects. If this is the case, why not just add them as they are?

for (var idx = 0; idx < objGridData.length; idx++) {
   grid.dataSource.insert(idx, objGridData[idx]);
}

In case I am misunderstanding the situation and you do need to create new objects for any reason you can use the "for..in" loop and the square brackets notation to describe the new object as well access it, e.g.:

var sampleObject = objGridData[0];
 
for (var idx = 0; idx < objGridData.length; idx++) {
  var newRec = {};
  for(var prop in sampleObject){
    newRec[prop] = objGridData[idx][prop];
  }
  grid.dataSource.insert(idx, newRec);
}

Here is a runnable example using this approach. In case you needed something different than that, please elaborate so I can alter my suggestion accordingly.

https://dojo.telerik.com/@bubblemaster/USuxUxoL

Kind Regards,
Alex Hajigeorgieva
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
aditya
Top achievements
Rank 1
Answers by
Alex Hajigeorgieva
Telerik team
Share this question
or