Flex笔记_格式化数据
发布时间:2020-12-15 03:39:22 所属栏目:百科 来源:网络整理
导读:格式化程序(formatter) 是一类对象,它们可以接收原始数据,并将其转换为可展示的格式。 用法上同验证器在实现机制上比较类似。 应用模式有下列两种: 实时格式化; 脚本式格式化 用法简单,输入原始数据,就会输出结构清晰,容易认读的格式化数据。 内置
格式化程序(formatter)
内置的格式化程序Formatter
NumberFormatter
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> <fx:XML id="myData" xmlns=""> <root> <forsale> <item name="weight" value="32.5x698"/> </forsale> </root> </fx:XML> <mx:NumberFormatter id="fmtNumber" precision="2" rounding="up"/> </fx:Declarations> <s:VGroup> <s:Label text="Weight {fmtNumber.format(myData.forsale.item.@value)} lbs"/> <s:Label text="{fmtNumber.error}"/> </s:VGroup> </s:Application> CurrencyFormatter
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> <fx:XML id="myData" xmlns=""> <root> <forsale> <item name="Laptop" value="599.999"/> </forsale> </root> </fx:XML> <mx:CurrencyFormatter id="fmtCurrency" precision="2"/> </fx:Declarations> <s:VGroup> <s:Label text="Laptop Price {fmtCurrency.format(myData.forsale.item.@value)}"/> <s:Label text="{fmtCurrency.error}"/> </s:VGroup> </s:Application> DateFormatter
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"> <fx:Script> <![CDATA[ [Bindable] public var sDate:String = "12/01/08 12:42"; [Bindable] public var dDate:Date = new Date("12/01/08 12:42"); ]]> </fx:Script> <fx:Declarations> <mx:DateFormatter id="fmtDate" formatString="YY/MM/DD"/> </fx:Declarations> <s:VGroup verticalCenter="0" horizontalCenter="0" horizontalAlign="center"> <s:Label text="Formatting the Date as a String:{fmtDate.format(sDate)}"/> <s:Label text="Formatting the Date as a Date object:{fmtDate.format(dDate)}"/> </s:VGroup> </s:Application> 综合使用DateFormatter和XML<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"> <fx:Declarations> <fx:XML id="myData"> <root> <info> <item lastVisit="12/01/08 12:42"/> </info> </root> </fx:XML> <mx:DateFormatter id="fmtDate" formatString="YY/MM/DD"/> </fx:Declarations> <s:VGroup verticalCenter="0" horizontalCenter="0" horizontalAlign="center"> <s:Label text="{fmtDate.format(myData.info.item.@lastVisit)}"/> </s:VGroup> </s:Application>注意:上述代码没有输出结果是因为Flex内部会把XML转换成一组高级对象,既不是Date也不是String,而format函数只接受这两种对象作为参数,因此代码需要做如下修改: <s:Label text="{fmtDate.format(String(myData.info.item.@lastVisit))}"/> PhoneFormatter
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"> <fx:Declarations> <fx:XML id="myData"> <root> <contactlist> <item name="contact" phone="2016679872"/> </contactlist> </root> </fx:XML> <mx:PhoneFormatter id="fmtNumber" formatString="(###) ###-####"/> </fx:Declarations> <s:VGroup verticalCenter="0" horizontalCenter="0" horizontalAlign="center"> <s:Label text="Contact Phone:{fmtNumber.format(myData.contactlist.item.@phone)}"/> </s:VGroup> </s:Application> ZipCodeFormatter
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"> <fx:Declarations> <fx:XML id="myData"> <root> <contacts> <item name="Leo" zipcode="953763233"/> </contacts> </root> </fx:XML> <mx:ZipCodeFormatter id="fmtZipCode" formatString="#####-####"/> </fx:Declarations> <s:VGroup verticalCenter="0" horizontalCenter="0" horizontalAlign="center"> <s:Label text="Zip Code:{fmtZipCode.format(myData.contacts.item.@zipcode)}"/> </s:VGroup> </s:Application> SwitchSymbolFormatter
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"> <fx:Script> <![CDATA[ import mx.formatters.SwitchSymbolFormatter; public var fmtSymbol:SwitchSymbolFormatter = new SwitchSymbolFormatter("#"); public function formatMe(rawData:String):String { return fmtSymbol.formatValue("####-####",rawData); } ]]> </fx:Script> <fx:Declarations> <fx:XML id="myData"> <root> <workorders> <item name="Fix something" id="99818382"/> </workorders> </root> </fx:XML> </fx:Declarations> <s:VGroup verticalCenter="0" horizontalCenter="0" horizontalAlign="center"> <s:Label text="Workorder:{formatMe(myData.workorders.item.@id)}"/> </s:VGroup> </s:Application> 实时格式化脚本格式化
处理格式化错误
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600"> <fx:Script> <![CDATA[ import mx.controls.Alert; import mx.formatters.*; public function formatThis(plainText:String):String { var fmtPhone:PhoneFormatter = new PhoneFormatter(); var formatedString = fmtPhone.format(plainText); if(fmtPhone.error == "Invalid value") { Alert.show("Invalid..."); } else if(fmtPhone.error == "Invalid format") { Alert.show("Invalid Format"); } return formatedString; } ]]> </fx:Script> <s:VGroup verticalCenter="0" horizontalCenter="0" horizontalAlign="center"> <s:Label text="{formatThis('222')}"/> </s:VGroup> </s:Application> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |