Ralfeus
Ralfeus

Reputation: 815

Kendo Grid saveChanges() doesn't work

I'm trying to set status field of the row with a custom command button. Grid data source transport is defined like this:

transport: {
  read: {
    url: "/agent/AgentList",
    type: "POST",
    dataType: "json",
    data: addAntiForgeryToken
  },
  update: {
    url: "/agent/UpdateAgent",
    type: "POST",
    dataType: "json",
    data: addAntiForgeryToken
  }
},

Custom command is defined as following:

{
  command: [
    {
      name: "accept",
      visible: function(dataItem) {
        return dataItem.StatusId === 1;
      },
      text: "@T("Ralfeus.Agent.Accept")",
      click: function(e) {setOfferResponse(this, e, 2);}
    }
  ]
}

The function actually setting the field is defined as following:

function setOfferResponse(grid, sender, response) {
  sender.preventDefault();
  var dataItem = grid.dataItem($(sender.currentTarget).closest("tr"));
  dataItem.StatusId = response;
  grid.editRow($(sender.currentTarget).closest("tr"));
  grid.saveChanges();
}

I expected saveChanges() call to trigger update method of datasource transport. However it neither causes any requests to server nor reports errors.

I also tried to replace grid.saveChanges() with grid.dataSource.sync() (by the way - what's the difference?) as was suggested here but result was same: no HTTP request, no errors.

Upvotes: 2

Views: 3110

Answers (1)

Ralfeus
Ralfeus

Reputation: 815

Ok, found the reason. Programmatic changing of dataItem fields doesn't make them dirty. So I had to make it dirty:

dataItem.dirty = true;
grid.saveChanges();

Then it worked

Upvotes: 6

Related Questions