Extend the DataGrid to make a selectedValue.

Note: This is very much just a modified version of what Ben Forta’s Fix to this issue is the ColdFusions CFForm Combobox having a selectedValue. Here is his post

I did find use of the selectedValue very appealing, and wanted the datagrid to have the same functionality… the thing is, with the combobox you have by default a ‘label’ field, and a ‘data’ field, that the combobox uses. With the datagrid, you may not… you may have a field that is named anything… so I added something to the mix, called ‘dataField’, and now you can supply a ‘selectedValue=”Item3″ dataField=”label”‘ to the data grid… here is the code…

<?xml version=“1.0″ encoding=“utf-8″?>
<mx:DataGrid xmlns:mx=http://www.adobe.com/2006/mxml> <mx:Script>
<![CDATA[
private var _selectedValue:String;
private var _dataField:String = 'data';
private var bSelectedValueSet:Boolean = false;
private var bDataProviderSet:Boolean = false;

// Override committ, this may be called repeatedly
override protected function commitProperties():void
{
// invoke ComboBox version
super.commitProperties();

// If value set and have dataProvider
if (bSelectedValueSet && bDataProviderSet)
{
// Set flag to false so code won't be called until selectedValue is set again
bSelectedValueSet=false;
// Loop through dataProvider
for (var i:int=0;i<this.dataProvider.length;i++)
{
// Get this item's data
var item:String = this.dataProvider[i][dataField];

// Check if is selectedValue
if(item == _selectedValue)
{
// Yes, set selectedIndex
this.selectedIndex = i;
break;
}
}
}
}

// Trap dataProvider being set
override public function set dataProvider(o:Object):void
{
// invoke ComboBox version
super.dataProvider = o;

// This may get called before dataProvider is set, so make sure not null and has entries
if (o!=null && o.length)
{
// Got it, set flag
bDataProviderSet = true;
}
}

// set for selectedValue
public function set selectedValue(s:String):void
{
// Set flag
bSelectedValueSet = true;
// Save value
_selectedValue = s;
// Invalidate to force commit
invalidateProperties();
}
// set for dataField
public function set dataField(s:String):void
{
// Save value
_dataField = s;
// Invalidate to force commit
invalidateProperties();
}

public function get dataField():String
{
// return value
return _dataField;
}
]]>
</mx:Script>

</mx:DataGrid>

<?xml version=“1.0″ encoding=“utf-8″?>
<mx:Application xmlns:mx=http://www.adobe.com/2006/mxml xmlns:ns1=“*” width=“50%” height=“50%”
backgroundGradientAlphas=“[1.0, 1.0]“ backgroundGradientColors=“[#FFFFFF, #FFFFFF]“ backgroundColor=“#FFFFFF”>

<mx:Script>
<![CDATA[
[Bindable]
private var comboData:Array=
[{label:"Item1", data:"1"},
{label:"Item2", data:"2"},
{label:"Item3", data:"3"}];
]]>
</mx:Script>
<mx:VBox width=“100%” height=“100%”>
<mx:Label text=“DataGrid with selectedValue”/>
<ns1:CustomDataGrid dataProvider=“{comboData}” selectedValue=“Item1″ dataField=“label” />
<mx:Label text=“ComboBox with selectedValue”/>
<ns1:CustomComboBox dataProvider=“{comboData}” selectedValue=“3″ />
</mx:VBox>
</mx:Application>

ps. I apologize for the nonIndention of the code, i did a copy and paste from my code, and it’s just how it shows up… sorry, i’m running short on time, and can’t fix it this time… but i did a copy and paste and the code works… so happy coding ;)