Flex的Datagrid中的labelFunction查询Flex、Datagrid、labelFunction

2023-09-08 15:04:01 作者:心哇凉哇凉的

Main.mxl

 < S:数据网格的dataProvider ={EmployeeData工作}> // EmployeeData工作是$ P $一个ArrayCollection pdefined数据


        &所述氏:typicalItem>
            < S:DataItem的的firstName =克里斯托弗
                        的lastName =温彻斯特
                        HIREDATE =22/12/2013​​/>
        &所述; /秒:typicalItem>


        < S:列>

            < S:ArrayList的>

                < S:的GridColumn的labelFunction =employeeName
                              HEADERTEXT =名称/>

                < S:的GridColumn的dataField =雇用日期
                              HEADERTEXT =聘用日期
                              的labelFunction =日期格式/>
            < / S:ArrayList的>

        < / S:列>


    < / S:数据网格>


< FX:脚本>
        <![CDATA [
            进口mx.collections.ArrayCollection;
            进口mx.controls.dataGridClasses.DataGridColumn;
            进口mx.rpc.events.ResultEvent;

            [可绑定]
            私人VAR EmployeeData工作:ArrayCollection的;


            私有函数employeeName(项目:对象,列:的GridColumn):字符串
            {
                返回item.firstName ++ item.lastName;
            }


        ]]≥
    < / FX:脚本>
 

A)任何人能请解释我怎么Datagrid的内部是 employeeName 函数的工作?我的意思是,荫甚至没有经过2参数的labelFunction,但还是它是如何被调用?

B)为什么要使用 S:ArrayList的代码在 S:列标记

解决方案   

A)任何人能请解释我如何Datagrid的内部工作原理   与employeeName功能?我的意思是,荫甚至没有传递2个参数   为的labelFunction,但还是它是如何被调用?

在了labelFunction是对的GridColumn类的属性。里面的GridColumn有其用于确定标签应该是什么的列的该特定实例的itemToString()函数。右出的框架code:

  / **
 *  @私人
 *为itemToLabel普通逻辑(),itemToDataTip()。逻辑上,这code是
 *类似于(不一样)LabelUtil.itemToLabel()。
 * /
私有函数itemToString(项目:对象,labelPath:阵列,的labelFunction:功能,格式:IFormatter):字符串
{
    如果(!项)
        返回ERROR_TEXT;

    如果(的labelFunction!= NULL)
        返回的labelFunction(项目,这一点);

    VAR itemString:字符串= NULL;
    尝试
    {
        VAR的ItemData:对象=项目;
        每个(VAR pathElement:字符串中labelPath)
            的ItemData =的ItemData [pathElement]

        如果((的ItemData = NULL)及!及(labelPath.length大于0))
            itemString =(格式化)? formatter.format(的ItemData):itemData.toString();
    }
    赶上(忽略:错误)
    {
    }

    返回(itemString!= NULL)? itemString:ERROR_TEXT;
}
 

  

B)我为什么要使用S:内部小号ArrayList的标签:?列标签

flex DataGrid 标题上放 CheckBox 实现全选

由于在DataGrid中列属性的数据类型是一个IList;和ArrayList的实现IList接口。你看什么是MXML方式来创建和定义一个ArrayList。你会使用一个稍微不同的方法,如果你想在ActionScript中创建的列。

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) 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?

B) Why should I use s:ArrayList tag inside s:columns tag?

解决方案

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.

 
精彩推荐
图片推荐