使用Flex itemEditors – Part 1: Inline itemEditors
转自: http://www.adobe.com/cn/devnet/flex/articles/itemeditors_pt1.html必备知识Prior experience working with Flex Builder to create applications is needed. 用户级别中级 必需产品
范例文件
I recently completed a series on itemRenderers ― customizations to list controls that format the display of the list contents. Displaying and rendering content is a very effective UI technique and with Flex you can do nearly anything you can imagine. This is Part 1 of a new series covering itemEditors,which allow data to be changed directly inside of a list control. This first article covers inline itemEditors,which are very simple,though quite useful,components you write directly inside your MXML files. Later articles in the series will cover more complex editing,validation,events,and using itemRenderers as itemEditors. The TextInput editorEditing??? directly in list controls is convenient. Imagine a DataGrid of warehouse??? inventory where you can adjust the content right in the grid without needing a??? special pop-up (see Figure 1). The list controls have a built in editor,a??? TextInput control,that appears whenever the user clicks in an editable area,??? either a row (for a List),a branch (for a Tree),or a cell (for a DataGrid).??? All you need to do is set the list control's
??????? Figure 1. itemEditors allow editing directly within a DataGrid
itemEditors differ from itemRenderers in that only one instance of the itemEditor is seen,just on the cell being edited. The itemEditor is not seen until the cell to be edited receives input focus. Then the itemRenderer is hidden and the itemEditor is moved to that position,sized to fit the area,and given the data for the record. When editing is finished (by moving focus to another location),the list control copies the new value from the editor to the dataProvider record. In the application shown in Figure 1,when the user clicks in a cell of the "Part #" column,the While the default TextInput control makes a fine editor,it really only works for the most simple of cases. It works fine for String values,for example,such as a book title,author name,or product number. If you need more control or want to validate the user's input,then you need to take matters into your own hands. Flex Controls as itemEditorsHere is how you make an itemEditor which only accepts numeric values: <mx:DataGrid x="46" y="270" editable="true" dataProvider="{employeeDB}"> <mx:columns> <mx:DataGridColumn headerText="Name" dataField="name"/> <mx:DataGridColumn headerText="Position" dataField="position"/> <mx:DataGridColumn headerText="Age" dataField="age"> <mx:itemEditor> <mx:Component> <mx:TextInput restrict="0-9" maxChars="3" /> </mx:Component> </mx:itemEditor> </mx:DataGridColumn> </mx:columns> </mx:DataGrid> The The??????????? CheckBox is another common control to use for an itemEditor,because it is??????????? useful for editing Boolean values. Figure 2 shows an example of using the??????????? CheckBox to edit the values for an "In Stock" column of an inventory??????????? program.
??????? Figure 2. Using a CheckBox as an itemEditor for a Boolean value
Here is the code to make it work:
????????
<mx:DataGrid x="531" y="273" editable="true" dataProvider="{inventoryDB}"> <mx:columns> <mx:DataGridColumn headerText="Product" dataField="product"/> <mx:DataGridColumn headerText="Part #" dataField="part"/> <mx:DataGridColumn headerText="In Stock?" dataField="inStock" labelFunction="inStockLabeler" itemEditor="mx.controls.CheckBox" editorDataField="selected" /> <mx:DataGridColumn headerText="Quantity" dataField="quantity"/> </mx:columns> </mx:DataGrid> In this example the content of the cells in this column are rendered using a labelFunction ( At this point,you may wonder why the example with the TextInput did not supply the You can use this same technique with a number of Flex controls. Here is one for an order quantity column using NumericStepper,as shown in Figure 3:
????????
<mx:DataGrid x="531" y="82" editable="true" dataProvider="{inventoryDB}"> <mx:columns> <mx:DataGridColumn headerText="Product" dataField="product"/> <mx:DataGridColumn headerText="Part #" dataField="part"/> <mx:DataGridColumn headerText="In Stock?" dataField="inStock"/> <mx:DataGridColumn headerText="Quantity" dataField="quantity" itemEditor="mx.controls.NumericStepper" editorDataField="value"/> </mx:columns> </mx:DataGrid>
??????? Figure 3. Using a NumericStepper to edit quantities
Notice??????????? the A more complex editorNow??????????? 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 the user to enter a??????????? credit card number using four separate four-digit fields (see Figure 4):
??????? Figure 4. Editing a credit card number in four separate fields
Here is the code to make it work:
????????
<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 Note the 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 used to display the data to be edited. The Where to go from hereIn this article you've seen how to create an inline itemEditor by simply naming a class and by creating a complex class right within the MXML tags. Naming the property of the editor class that contains the final editor value allows the DataGrid to retrieve the value from the editor instance and replace the current value in the data. Part 2 of this series covers more complex itemEditors and editing events. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- C#设计模式之三抽象工厂模式(AbstractFactory)【创建型】
- Ajax上传图片以及上传之前先预览
- Unable to load sqlite_jni: java.lang.UnsatisfiedLinkErr
- Ajax简单总结
- Swift 基础学习(2)
- ios – 自动布局约束:调整大小时,将空间非均等地分配给子视
- Vue 爬坑之路(六)—— 使用 Vuex + axios 发送请求
- postgresql-9.1 – Postgresql:ERROR:类型“citext”不存
- ruby-on-rails-3 – surround.vim:删除嵌入的ruby标签
- c# – .NET Core EndRequest中间件