加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用Flex itemEditors – Part 1: Inline itemEditors

发布时间:2020-12-15 03:40:00 所属栏目:百科 来源:网络整理
导读:转自: http://www.adobe.com/cn/devnet/flex/articles/itemeditors_pt1.html 必备知识 Prior experience working with Flex Builder to create applications is needed. 用户级别 中级 必需产品 Flex Builder 3 (Download trial) 范例文件 itemEditors_pt1.

转自: http://www.adobe.com/cn/devnet/flex/articles/itemeditors_pt1.html

必备知识

Prior experience working with Flex Builder to create applications is needed.

用户级别

中级

必需产品

  • Flex Builder 3 (Download trial)

范例文件

  • itemEditors_pt1.mxml ? (5 KB)
?

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 editor

Editing??? 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'seditable property to true. For a DataGrid you can exclude a column from being edited by??? setting the DataGridColumn'seditable property to false.

??????? 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,thedataProvider[row][dataField] value is given to the text property of the itemEditor (TextInput) control. When editing is finished,thetext property value from the itemEditor (TextInput) control is copied to thedataProvider[row][dataField]. The dataProvider,being a collection,dispatches an event in response to the update.

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 itemEditors

Here 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 restrict and maxChars properties ensure??????????? that age values are constrained to three-digit numbers.

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 (inStockLabeler),which can display descriptive strings such as "Yes","No","In Stock",or "Out of Stock". TheitemEditor property is set to the mx.controls.CheckBox class. And there is another,equally important,property set on the DataGridColumn:editorDataField. This field indicates the property of the itemEditor class to use to fetch the value when editing is finished. In this case it is the CheckBox'sselected property. When editing is finished,the DataGrid will use the CheckBox'sselected property to replace the inStock property in the data record.

At this point,you may wonder why the example with the TextInput did not supply theeditorDataField property. That is because the default value for editorDataField is "text" which just happens to be the name of the property on the TextInput control holding the value.

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 editorDataField is "value" - the property of the??????????? NumericStepper that holds the current value of the control. Make sure you use??????????? the fully-qualified class name for the itemEditor property or else the compiler??????????? will not be able to find the class and flag the line with an error.

A more 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 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 theeditorDataField 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 namedvalue is created to return the concatenation of the four input fields. Now when the user is finished editing the cell,the DataGrid can call upon the value property of the itemEditor and it will receive the combined fields.

Note the super.data = value in the data setter function. The data property - really the data getter function - is used extensively behind the scenes in the List controls and elsewhere in the framework (not to mention your own code). If you don't set the internal value of data using super.data,then the data getter function will return a null value and,most likely,cause your application to crash.

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. TheeditorDataField is the property used to retrieve the new value.

Where to go from here

In 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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读