Requirement: We want to validate user input in editable datagrid, and revert the original value(undo) if necessary.
Solution: Handle the ‘itemEditEnd’ Event dispatched by the datagrid.
Codes:
/** * validate user input, revert the original value if necessary. */ protected function datagirdTest_itemEditEndHandler(event:DataGridEvent):void { if(event.dataField != "Name") { // chech the field; return; } var input:String = (datagirdTest.itemEditorInstance as TextInput).text; // get the user input data. if(input == null || input == "") { event.preventDefault(); // prevent default behavior // var filed:String = (datagirdTest.columns[event.columnIndex] as DataGridColumn).editorDataField; // trace(datagirdTest.itemEditorInstance[filed]); (datagirdTest.itemEditorInstance as TextInput).text = (event.itemRenderer.data as XML).Name;// Undo: revert the original data by the selected item. Alert.show(errorMesg); return; } }
Screen caputre:
User input is empty, we got the Event, prevent the default behavior, and revert the value form the modeling.
Ref:
1. itemEditEnd called multiple times http://forums.adobe.com/message/2459209
2. Using cell editing events – http://livedocs.adobe.com/flex/3/html/help.html?content=celleditor_7.html
20111120 <->
// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.