这两天在看flex,关于datagrid,有个需求,就是在一列中,根据某列的数据类型来实现不同的数据类型。
网上有个老外写了个sample,给有需求的朋友看看
三个文件
Main.mxml
==========================================================================
creationComplete="creationCompleteHandler(event)">
import flash.events.MouseEvent;
import mx.collections.ArrayCollection;
import mx.controls.dataGridClasses.DataGridColumn;
import mx.events.FlexEvent;
import mx.controls.AdvancedDataGrid;
[Bindable]
private var ac:ArrayCollection;
private function creationCompleteHandler(event:FlexEvent):void
{
var objList:Array = new Array();
var typeList:Array = [DataVO.TYPE_STRING, DataVO.TYPE_NUMBER];
var valueList:Array = ["string value", 1];
var dataVO:DataVO;
for (var i:int = 0; i < 50; i++)
{
var type:String = typeList[i % typeList.length];
var value:Object = valueList[i % valueList.length];
dataVO = new DataVO();
dataVO.label = "Item " + i;
dataVO.type = type;
dataVO.value = value;
objList.push(dataVO);
}
ac = new ArrayCollection(objList);
}
]]>
variableRowHeight="true">
DataVO.as
===============================================================================
package
{
/**
* ...
* @author DefaultUser (Tools -> Custom Arguments...)
*/
public class DataVO
{
public static const TYPE_STRING:String = "string";
public static const TYPE_NUMBER:String = "number";
public var label:String;
public var value:Object; // e.g. String, Date or custom object.
public var type:String;
}
}
DataEditor.mxml
============================================================================
implements="mx.core.IDataRenderer, mx.controls.listClasses.IListItemRenderer"
verticalScrollPolicy="off" horizontalScrollPolicy="off">
import mx.controls.NumericStepper;
import mx.controls.TextInput;
private var stepper:NumericStepper;
private var textField:TextInput;
private var dataChanged:Boolean = false;
override public function set data(value:Object):void
{
super.data = value;
dataChanged = true;
invalidateProperties();
}
override protected function commitProperties():void
{
super.commitProperties();
if (dataChanged)
{
createEditor();
dataChanged = false;
}
}
private function createEditor():void
{
var dataVO:DataVO = DataVO(data);
removeEditor();
if (dataVO != null)
{
if (dataVO.type == DataVO.TYPE_NUMBER)
{
stepper = new NumericStepper();
addChild(stepper);
stepper.value = Number(dataVO.value);
}
else if (dataVO.type == DataVO.TYPE_STRING)
{
textField = new TextInput();
addChild(textField);
textField.text = String(dataVO.value);
}
}
}
private function removeEditor():void
{
if (stepper != null)
{
removeChild(stepper);
stepper = null;
}
if (textField != null)
{
removeChild(textField);
textField = null;
}
}
public function get newData():Object
{
var newValue:Object = null;
var dataVO:DataVO = DataVO(data);
if (dataVO.type == DataVO.TYPE_NUMBER)
{
var stepper:NumericStepper = stepper;
newValue = stepper.value;
}
else if (dataVO.type == DataVO.TYPE_STRING)
{
newValue = textField.text;
}
//return textDisplay.text;
return newValue;
}
]]>
阅读(1241) | 评论(0) | 转发(0) |