Author Topic: When is the grid data actually changed after commit()?  (Read 3195 times)

cmcooper

  • Newbie
  • *
  • Posts: 5
    • View Profile
When is the grid data actually changed after commit()?
« on: September 17, 2016, 12:54:23 am »
I'm trying to implement saving of nested grids when the parent grid is saved. The parent row has an identity column that is generated by the db. This column is also a foreign key in the nested grid rows, so I need the value before saving the nested grid. At what point after calling commit() on the parent grid will the identity be populated in the parent row?

Code: [Select]
function saveChanges(){
            //attempt to save editing cell.
            if (grid.saveEditCell() === false) {
                return false;
            }

            if (grid.isDirty() && grid.isValidChange({ focusInvalid: true }).valid) {

                var changes = grid.getChanges({ format: 'byVal' });

                // Save changes to db
                $.ajax({
                    dataType: "json",
                    type: "POST",
                    async: false,
                    beforeSend: function (jqXHR, settings) {
                        grid.showLoading();
                    },
                    url: "SaveChanges",
                    data: { strChanges: JSON.stringify(changes) },
                    success: function (changes) {
                        grid.commit({ type: 'add', rows: changes.addList });
                        grid.commit({ type: 'update', rows: changes.updateList });
                        grid.commit({ type: 'delete', rows: changes.deleteList });

                        grid.history({ method: 'reset' });
                    },
                    complete: function () {
                        grid.hideLoading();
                    }
                });
            }
        }

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6210
    • View Profile
Re: When is the grid data actually changed after commit()?
« Reply #1 on: September 17, 2016, 08:57:14 am »
commit() is a synchronous method, so id of the parent row is populated just after call to commit() method.

cmcooper

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: When is the grid data actually changed after commit()?
« Reply #2 on: September 19, 2016, 05:24:31 pm »
After the calls to commit() above, pdata still does not have the ID populated for new rows. Additionally, I have a saveChanges method on the detail grid.

Code: [Select]
saveChanges: function (thisGrid) {
                    DoSomething(rowData.ID, thisGrid);
                },

When this is called after the call to commit() on the parent grid, rowData.ID is still undefined for new rows.

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6210
    • View Profile
Re: When is the grid data actually changed after commit()?
« Reply #3 on: September 19, 2016, 10:10:11 pm »
You need to check the value of id in changes.addList in the success callback. If it's not there, you need to correct remote script to add id of new rows.

Code: [Select]
success: function (changes) {
  //check whether changes.addList contain id of rows.
                        grid.commit({ type: 'add', rows: changes.addList });
                        grid.commit({ type: 'update', rows: changes.updateList });
                        grid.commit({ type: 'delete', rows: changes.deleteList });

                        grid.history({ method: 'reset' });
                    },

cmcooper

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: When is the grid data actually changed after commit()?
« Reply #4 on: September 20, 2016, 01:14:00 am »
There were actually two issues here.

1. I changed
Code: [Select]
return js.Serialize(dlist);
to
Code: [Select]
return JsonConvert.SerializeObject(dlist, new Newtonsoft.Json.Converters.IsoDateTimeConverter() { DateTimeFormat = "MM/dd/yyyy" });in the SaveChanges() method in the controller.

2. I had to hide the detail column while committing.