Main.mxl
<s:DataGrid dataProvider="{employeeData}"> // employeeData is an Arraycollection with predefined data
<s:typicalItem>
<s:DataItem firstName="Christopher"
lastName="Winchester"
hireDate="22/12/2013"/>
</s:typicalItem>
<s:columns>
<s:ArrayList>
<s:GridColumn labelFunction="employeeName"
headerText="Name"/>
<s:GridColumn dataField="hireDate"
headerText="Hire Date"
labelFunction="dateFormat"/>
</s:ArrayList>
</s:columns>
</s:DataGrid>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.dataGridClasses.DataGridColumn;
import mx.rpc.events.ResultEvent;
[Bindable]
private var employeeData: ArrayCollection;
private function employeeName(item: Object, column: GridColumn): String
{
return item.firstName+" "+item.lastName;
}
]]>
</fx:Script>A),能不能请你解释一下,数据格力内部是如何与employeeName函数工作的?我的意思是,我甚至没有为labelFunction传递两个参数,但是它是如何被调用的?
B)为什么要使用s:ArrayList标签(在中):列标记?
发布于 2013-08-08 15:25:40
( A)请任何人解释一下,数据格力内部是如何与employeeName功能一起工作的?我的意思是,我甚至没有为labelFunction传递两个参数,但是它是如何被调用的?
labelFunction是GridColumn类上的一个属性。在Gridcolumn中有一个itemToString()函数,用于确定该列的特定实例的标签应该是什么。在框架代码之外:
/**
* @private
* Common logic for itemToLabel(), itemToDataTip(). Logically this code is
* similar to (not the same as) LabelUtil.itemToLabel().
*/
private function itemToString(item:Object, labelPath:Array, labelFunction:Function, formatter:IFormatter):String
{
if (!item)
return ERROR_TEXT;
if (labelFunction != null)
return labelFunction(item, this);
var itemString:String = null;
try
{
var itemData:Object = item;
for each (var pathElement:String in labelPath)
itemData = itemData[pathElement];
if ((itemData != null) && (labelPath.length > 0))
itemString = (formatter) ? formatter.format(itemData) : itemData.toString();
}
catch(ignored:Error)
{
}
return (itemString != null) ? itemString : ERROR_TEXT;
}( B)为什么要在s:columns中使用s:ArrayList标记?
因为DataGrid上列属性的数据类型是IList;而ArrayList实现了IList接口。您所看到的是创建和定义ArrayList的MXML方法。如果要在ActionScript中创建列,则可以使用稍微不同的方法。
发布于 2013-08-08 13:10:00
要回答第一个问题:"labelFunction“属性是对将由DataGrid调用以格式化列中单元格文本的函数的引用。函数将被动态调用,DataGrid将传递所需的参数。DataGrid期望label函数始终具有此签名。如果您没有这样做,您将得到一个运行时错误。
从技术上讲,可以使用以下语法动态调用函数:
var anObject:MyType;
var methodName:String = "myMethod";
anObject[methodName](param1, param2);或者如果您有一个函数对象
var myFunction:Function;
myFunction(param1, param2);https://stackoverflow.com/questions/18124517
复制相似问题