Expert wanna be
Expert wanna be

Reputation: 10624

Extjs4, how to restore edited grid cell value in edit function?

I need to restore(set value to before edited value) edited grid cell value in edit function, not in validateedit function.

"orderList": {
    validateedit: function (plugin, edit) {
      //validate...
    },
    edit: function (plugin, edit) {
        Ext.MessageBox.confirm('Confirm', 'Are you sure to change this order status?', function (btn) {
        if (btn == 'yes') {
            //update
        } else {
            // I want to rollback!
            edit.cancel = true;
            edit.record.data[edit.field] = edit.originalValue; //it does not work
        }
        });
    }
}

How to change the grid cell value (editor)?

Thanks!

Upvotes: 3

Views: 4884

Answers (2)

CTS_AE
CTS_AE

Reputation: 14783

If you bind to the afteredit event on the grid can do something like the following, depending on the granularity you would like to reset.

Note: I didn't add any logic to keep things quick and straight to the point.

Reset Only the Current Change/Cell

grid.on('afteredit', function(e) {
  e.record.set(e.field, e.originalValue);
}

Reset the Whole Record/Row

grid.on('afteredit', function(e) {
  e.record.reject();
}

Reset the Whole Grid

grid.on('afteredit', function(e) {
  e.grid.store.rejectChanges();
}

Upvotes: 0

egerardus
egerardus

Reputation: 11486

How about the reject method:

"orderList": {
    validateedit: function (plugin, edit) {
      //validate...
    },
    edit: function (plugin, edit) {
        Ext.MessageBox.confirm('Confirm', 'Are you sure to change this order status?', function (btn) {
            if (btn == 'yes') {
                //update
            } else {
                edit.record.reject(); // this should revert all changes
            }
        });
    }
}

Also note that the second argument (the one you named "edit") of the edit event does not contain a cancel property, that is a property of the beforeedit event. So this line edit.cancel = true won't do anything for you.

I was also curious why you aren't using the beforeedit event, it seems more ideally suited for this kind of thing - that it why it does have that cancel property.

Upvotes: 4

Related Questions