Complex Editor

来源:互联网 发布:淘宝小鸭软件 编辑:程序博客网 时间:2024/05/09 02:40

Complex Editor

Now suppose you want to do something a little more complex that doesn't have a ready-made Flex control available. Here is one which allows a credit card number to be entered using 4 separate 4-digit fields:

<mx:DataGrid x="46" y="463" editable="true" dataProvider="{accountDB}" width="302"><mx:columns><mx:DataGridColumn headerText="Account" dataField="account" width="100"/><mx:DataGridColumn headerText="Credit Card" dataField="ccard" editorDataField="value"><mx:itemEditor><mx:Component><mx:HBox><mx:Script><![CDATA[public function get value() : String{return part1.text+part2.text+part3.text+part4.text;}override public function set data(value:Object):void{super.data = value;part1.text = value.ccard.substr(0,4);part2.text = value.ccard.substr(4,4);part3.text = value.ccard.substr(8,4);part4.text = value.ccard.substr(12,4);}]]></mx:Script><mx:TextInput id="part1" maxChars="4" restrict="[0-9]" width="40" /><mx:TextInput id="part2" maxChars="4" restrict="[0-9]" width="40" /><mx:TextInput id="part3" maxChars="4" restrict="[0-9]" width="40" /><mx:TextInput id="part4" maxChars="4" restrict="[0-9]" width="40" /></mx:HBox></mx:Component></mx:itemEditor></mx:DataGridColumn></mx:columns></mx:DataGrid>

This inline itemEditor follows the same rules as other itemEditors and names the editorDataField as "value". The component chosen for the itemEditor is the HBox - which does not have a "value" property. To make this itemEditor work, a getter function named value is created to return the concatenation of the 4 input fields. Now when editing for the cell completes, the DataGrid can call upon the value property of the itemEditor and it will receive the combined fields.

You can also see that I have overridden the data setter function. In that function I split up the credit card number among the four TextInput fields. This is the technique you use to display the data to be edited. The editorDataField is the property used to retrieve the new value.


原文:http://weblogs.macromedia.com/pent/