Skip to content

Instantly share code, notes, and snippets.

@pzelnip
Last active August 29, 2015 14:04
Show Gist options
  • Save pzelnip/f642b206d300dc6eba8b to your computer and use it in GitHub Desktop.
Save pzelnip/f642b206d300dc6eba8b to your computer and use it in GitHub Desktop.
Complete example of conditional datagrid editing dependent upon associated value in row
<?xml version="1.0"?>
<!-- itemRenderers\events\EndEditEventPreventEdit.mxml -->
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
initialize="init()">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.collections.ListCollectionView;
import mx.events.DataGridEvent;
import mx.events.ListEvent;
[Bindable]
private var initDG:ArrayCollection = new ArrayCollection([
{ID: 0, Artist:'Joe Blow', Album:'Some Record',
Price:5.99},
{ID: 1, Artist:'Pavement', Album:'Brighten the Corners',
Price:11.99},
{ID: 2, Artist:'Pavement', Album:'Brighten the Corners',
Price:11.99}
]);
private function disableEditing(event:DataGridEvent):void
{
// get the value in the row that has been triggered
var item:Object = ListCollectionView(myGrid.dataProvider)
.getItemAt(event.rowIndex);
// get the name of the column being edited
var colName:String = myGrid.columns[event.columnIndex].dataField;
if(colName == 'Price' && item.ID == 0)
{
event.preventDefault();
}
}
]]>
</fx:Script>
<mx:DataGrid id="myGrid"
dataProvider="{initDG}"
editable="true"
itemEditBeginning="disableEditing(event)" >
<mx:columns>
<mx:DataGridColumn dataField="ID" editable="false"/> <!-- never editable -->
<mx:DataGridColumn dataField="Artist"/> <!-- always editable -->
<mx:DataGridColumn dataField="Album"/> <!-- always editable -->
<mx:DataGridColumn dataField="Price"/> <!-- conditionally editable if associated ID is 0 -->
</mx:columns>
</mx:DataGrid>
</s:Application>
@pzelnip
Copy link
Author

pzelnip commented Aug 1, 2014

Example of making a value in a row conditionally editable depending upon an associated value in the row. In this case, we want the "Price" value to be editable only when the associated ID value is 0. As well, the ID value itself is never editable, and all other columns are always editable.

@pzelnip
Copy link
Author

pzelnip commented Aug 1, 2014

Also note that if you reorder the columns, the editability rules remain intact.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment