jongusmoe jongusmoe - 2 months ago 25
Javascript Question

Dojo dgrid updating a row

I have a dgrid that populates from a dojo/request:

var TrackableMem = declare([Memory, Trackable]);
var store = new TrackableMem({
data : resp.results
});


This grid has a few columns that are textboxes. When they are changed, I would like to confirm the user actually wants to edit them:

grid.on("dgrid-datachange", function(evt) {
if(!confirm("change this field?")) {
// revert cell value back to the old one.
grid.get("collection").get(evt.cell.row.id).notes = evt.oldValue;
grid.refresh();
} else {
// do ajax call here.....
}
}


This does not work, the new updated value stays there, but when I run the same exact thing in the developer console it does work:

grid.get("collection").get(0).notes = "testing";
grid.refresh();


Any ideas? evt.cell.row.id & evt.oldValue are correct values.

Answer

There's no need to manually revert the data in the dgrid-datachange handler since the values haven't been persisted yet.

This is done on purpose so that you can cancel the update. In your case you could do:

grid.on("dgrid-datachange", function(evt) { if(!confirm("change this field?")) { evt.preventDefault(); } else { // do ajax call here..... } }

You can read more about this in the offical docs and this Github ticket.