I had been working on DataGrid that displays heterogenous data: any two rows may share all or just some of the properties, so the DataGrid appears sparse. Then during “protecting” non-editable items per specific row this casting example popped up.
The easiest way to row-by-row disable editing is via itemEditBeginning event:
mx:DataGrid itemBeginning=”onItemEditBeginning(event)” …
public function onItemEditBeginning(event:DataGridEvent):void {
if (event.itemRenderer) {
// do something to determine whether item is editable, preventDefault otherwise
}
}
Now let’s fantasize that items populating your dataProvider might be implementing IEditableItem, which mandates method isFieldEditable():
function isFieldEditable(dataField:String):Boolean;
Then you can write something like
Variant1:
var item:Object = event.itemRenderer.data ;
if (item is IEditableItem) {
if (!item.isFieldEditable(dataField)) {
event.preventDefault();
}
}
OR – Variant2:
var item:Object = event.itemRenderer.data ;
if (item is IEditableItem) {
if (!IEditableItem(item).isFieldEditable(dataField)) {
event.preventDefault();
}
}
OR – Variant3:
var item:IEditableItem = event.itemRenderer.data as IEditableItem;
if (item) {
if (!item.isFieldEditable(dataField)) {
event.preventDefault();
}
}
Variant1 looks simple, but may cost you some sleep when another developer redefines IEditableItem, which won’t be discovered during compilation. Variant2 applies regular casting to take care of that potential re-factoring and … gets annoying with IEditableItem/IEditableItem. Variant 3 combines clarity of 1 with reliability of 3: when item is not IEditableItem “as” casting returns null, otherwise we are already strongly typed. Call it hair splitting and I won’t argue much. After all, this does the job too:
if (typeof item["isFieldEditable"]==”function”) {
if (!item["isFieldEditable"](dataField)) {
event.preventDefault();
}
}
![]()
Victor


Anatole Tartakovsky said,
December 7, 2006 @ 2:27 am
Hmm. I thought that would cause focus moving away upon click on non-editable field – have you considered cross of http://samples.faratasystems.com/AdvancedDataGrid/ComputedEditorDemo.html and http://samples.faratasystems.com/AdvancedDataGrid/PropertyBagDemo.html with itemEditor as itemRenderer to bypass the focus/editing issues?
Regards,
Anatole