Last active
August 29, 2015 14:04
-
-
Save pzelnip/f642b206d300dc6eba8b to your computer and use it in GitHub Desktop.
Complete example of conditional datagrid editing dependent upon associated value in row
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
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
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.