PDA

View Full Version : [3.0] Improved grid editor validation



JamesC
17 Sep 2009, 2:02 AM
Code below is an override to editor grid panel to better handle values being changed. Currrently the grid panel compares the string values of the original & new values which in some cases incorrectly reports changes (or not). This extension takes into account the type of editor field and parses the value correctly to work out if there has been a change.



Ext.override(Ext.grid.EditorGridPanel, {
onEditComplete : function(ed, value, startValue) {
this.editing = false;
this.activeEditor = null;
ed.un("specialkey", this.selModel.onEditorKey, this.selModel);
var r = ed.record;
var field = this.colModel.getDataIndex(ed.col);
value = this.postEditValue(value, startValue, r, field);
var e = {
grid: this,
record: r,
field: field,
originalValue: startValue,
value: value,
row: ed.row,
column: ed.col,
cancel:false
};
// check values changed
var valuesChanged = this.valueChanged(e);
if ((this.forceValidation === true || valuesChanged) && this.fireEvent("validateedit", e) !== false && !e.cancel) {
r.set(field, e.value);
delete e.cancel;
this.fireEvent("afteredit", e);
}
this.view.focusCell(ed.row, ed.col);
},
valueChanged: function(e) {
var equalValues;
var newEmpty = Ext.isEmpty(e.value);
var origEmpty = Ext.isEmpty(e.originalValue);
if (newEmpty && origEmpty) { // if both empty, they are equal
equalValues = true;
} else if ((newEmpty && !origEmpty) || (!newEmpty && origEmpty)) { // if one empty, they are not equal
equalValues = false;
} else { // both populated, use data type conversions to calculate difference
var field = e.grid.getColumnModel().getCellEditor(e.column, e.row).field;
if (field.getXType() == "numberfield") {
equalValues = e.value == field.parseValue(e.originalValue);
} else if (field.getXType() == "datefield") {
equalValues = e.value.getTime() == field.parseDate(e.originalValue).getTime();
} else {
equalValues = String(e.value) == String(e.originalValue);
}
}
return !equalValues;
}
});


In a future version of Ext it might be worth all fields implementing a convertOriginalValue method to convert the held original value to the same datatype as is held by the getValue method. Then to compare the original value to the current value could use this method in the onEditComplete as a better check for if the values have changed.

mystix
17 Sep 2009, 3:21 AM
IMHO you should really be drilling down to the Record level instead:
http://www.extjs.com/forum/showthread.php?p=381985#post381985