Adobe Flex ItemRenderer:进行计算

时间:2012-05-08 03:43:45

标签: flex date adobe itemrenderer

我有一个数据库,包括用户的出生日期,但我想显示他们的年龄。我有一个计算年龄的函数,但我不能在ItemRenderer中使用它,我不知道为什么。如果我在ItemRenderer之外使用此代码,我认为它不会起作用。 DOB由{data.dob}访问。

这是我的代码:

<s:GridColumn dataField="age" headerText="Age" width="80">
                    <s:itemRenderer>
                        <fx:Component>
                            <s:GridItemRenderer>
                                <fx:Script>
                                    <![CDATA[
                                        // Retrieving the user's DOB
                                        var dateStr:String = "{data.dob}";
                                        //Splitting the DOB up to make it compatible with Flex
                                        var parts:Array = dateStr.split("-");

                                        if(parts == null || parts.length != 3)
                                        {
                                            Alert.show("There is an error retrieving the birthday.");
                                        }

                                        var dobDay:* = parts[2];
                                        var dobMonth:* = parts[1] - 1;
                                        var dobYear:* = parts[0];

                                        var userDOB : Date = new Date(dobYear, dobMonth, dobDay);
                                        var today : Date = new Date();

                                        var diff : Date = new Date();
                                        diff.setTime( today.getTime() - userDOB.getTime() );

                                        var userAge : int = diff.getFullYear() - 1970;
                                    ]]>
                                </fx:Script>
                                <s:Label id="ageLbl" text="userAge" />
                            </s:GridItemRenderer>
                        </fx:Component>
                    </s:itemRenderer>
                </s:GridColumn>`

起初我收到var dateStr:String = {data.dob};的#1084错误,因此我将其更改为var dateStr:String = "{data.dob}";,并将错误迁移到1120: Access of Undefined property Alert。我也遇到了同样的错误:diff, parts, today, userAge, userDOB。 这里有什么想法?就像我说的那样,代码在ItemRenderer之外工作,只使用一个DOB。

2 个答案:

答案 0 :(得分:0)

您正在使用内联项呈示器。可以想象内联渲染器与外部文件中的项呈示器相同。您需要重新导入mx.controls.Alert才能使用它。此外,如果您想从itemRenderer中调用主应用程序变量,则需要在主应用程序中将它们设为公共变量。

答案 1 :(得分:0)

试试这个:

<s:DataGrid x="71" y="58" width="490" height="264" requestedRowCount="4">
    <s:columns>
        <s:ArrayList>
            <s:GridColumn dataField="dataField1" headerText="Column 1"></s:GridColumn>
            <s:GridColumn dataField="dataField2" headerText="Column 2"></s:GridColumn>
            <s:GridColumn dataField="dob" headerText="Age" width="80">
                <s:itemRenderer>
                    <fx:Component>
                        <s:GridItemRenderer>
                            <fx:Script>
                                <![CDATA[
                                    import mx.controls.Alert;

                                    override public function prepare(hasBeenRecycled:Boolean):void {

                                        if(data!=null)
                                        {
                                            trace(data.dob);

                                            // Retrieving the user's DOB
                                            var dateStr:String = data.dob;
                                            //Splitting the DOB up to make it compatible with Flex
                                            var parts:Array = dateStr.split("-") ;

                                            if(parts == null || parts.length != 3)
                                            {
                                                Alert.show("There is an error retrieving the birthday.");
                                            }

                                            var dobDay:* = parts[2];
                                            var dobMonth:* = parts[1] - 1;
                                            var dobYear:* = parts[0];

                                            var userDOB : Date = new Date(dobYear, dobMonth, dobDay);
                                            var today : Date = new Date();

                                            var diff : Date = new Date();
                                            diff.setTime( today.getTime() - userDOB.getTime() );

                                            var userAge : int = diff.getFullYear() - 1970;

                                            ageLbl.text = userAge.toString(); 
                                        }
                                    }

                                ]]>
                            </fx:Script>
                            <s:Label id="ageLbl" text="userAge"  top="7" left="9"/>
                        </s:GridItemRenderer>
                    </fx:Component>
                </s:itemRenderer>
            </s:GridColumn>

        </s:ArrayList>
    </s:columns>
    <s:ArrayList>
        <fx:Object dataField1="data1" dataField2="data1" dob="1990-05-25"></fx:Object>
        <fx:Object dataField1="data2" dataField2="data2" dob="1981-02-15"></fx:Object>
        <fx:Object dataField1="data3" dataField2="data3" dob="1956-05-7"></fx:Object>
        <fx:Object dataField1="data4" dataField2="data4" dob="1994-08-25"></fx:Object>
    </s:ArrayList>
</s:DataGrid>