0

主文件.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)谁能解释一下 Datagrid 在内部如何与employeeName函数一起工作?我的意思是,我什至没有为 labelFunction 传递 2 个参数,但它仍然是如何被调用的?

B)为什么我应该在s:columns标签内使用s:ArrayList标签?

4

2 回答 2

0

A) Can anyone please explain me how does Datagrid internally works with employeeName function? I mean, Iam not even passing 2 parameters for labelFunction, BUT still how does it get called?

The labelFunction is a property on the GridColumn class. Inside the Gridcolumn there is an itemToString() function which is used to determine what the label should be for that specific instance of the column. right out of the framework code:

/**
 *  @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) Why should I use s:ArrayList tag inside s:columns tag?

Because the data type of the columns property on the DataGrid is an IList; and the ArrayList implements the IList interface. What you're looking at is the MXML way to create and define an ArrayList. You'd use a slightly different approach if you wanted to create the columns in ActionScript.

于 2013-08-08T15:25:40.557 回答
0

要回答您的第一个问题:“labelFunction”属性是对将由 DataGrid 调用以格式化列中单元格文本的函数的引用。该函数将被动态调用,DataGrid 将传入所需的参数。DataGrid 期望标签函数始终具有此签名。如果你不这样做,你会得到一个运行时错误。

从技术上讲,可以使用以下语法动态调用函数:

var anObject:MyType;
var methodName:String = "myMethod";

anObject[methodName](param1, param2);

或者如果你有一个 Function 对象

var myFunction:Function;
myFunction(param1, param2);
于 2013-08-08T13:10:00.900 回答