2 Replies Latest reply on Apr 28, 2010 10:15 AM by SalmonScreen

    itemEditor in DataGrid dispatches two events instead of one

    SalmonScreen

      I am new to flex development.

       

      I am building a data-editing application.  I am using a DataGrid to display the fields, and custom itemEditors to allow the user to edit them.  One of the fields to be edited is an IP Address.  The itemEditor splits up the octets into four TextInputs, each with a NumberValidator.  The itemEditor also concatenates the text properties of these TextInputs (with dots) to make an ip address, which it assigns to a public property called "returnIPAddress".

       

      If the ip address is valid, the itemEditor dispatches one DataGridEvent (where DataGridEventReason is either "newColumn" or "other", depending of where the user clicked or tabbed to).  However, when the ip address is invalid, the itemEditor dispatches TWO DataGridEvents, the second of which is always an "other".  This causes me to display two Alert messages ("the ip address is invalid"), when only one should be displayed.

       

      Why are two itemEditEnd events being dispatched?

       

       

      --END OF QUESTION, CODE FOLLOWS--

       

       

      The DataGrid:

       

          <mx:DataGrid id="editListDG"

               horizontalScrollPolicy="on"
               dataProvider="{__CDMModel.editingItems}"

               editable="true"
               itemEditEnd="processData(event)"
               width="100%" height="100%">
               <mx:columns>
                   <mx:DataGridColumn

                       dataField="ip_address"

                       headerText="IP Address"

                       width="200"
                       itemEditor="IPAddressEditor"

                       editorDataField="returnIPAddress" />

                </mx:columns>

          </mx:DataGrid>

       

      The itemEditor:

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()" >


          <mx:Script>
              <![CDATA[
                 
                  [Bindable] public var returnIPAddress:String;

         


                     private function init():void {
                      var ipAddress:String = new String(this.data.ip_address);
                      var pattern:RegExp = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/;
                      var results:Array = ipAddress.match(pattern);
                      ip1.text = results[1];
                      ip2.text = results[2];
                      ip3.text = results[3];
                      ip4.text = results[4];
                      returnIPAddress = ip1.text + "." + ip2.text + "." + ip3.text + "." + ip4.text;
                     }
                    
                     private function updateReturnIPAddress():void {
                         returnIPAddress = ip1.text + "." + ip2.text + "." + ip3.text + "." + ip4.text
                         trace('return IP Address is now ' + returnIPAddress);
                     }
                    
              ]]>
          </mx:Script>
         
          <mx:NumberValidator    source="{ip1}" trigger="{ip1}" property="text" triggerEvent="change"
              required="true" minValue="1" maxValue="254"
              lowerThanMinError="More than 0, please" exceedsMaxError="Less than 255, please" />
          <mx:NumberValidator    source="{ip2}" trigger="{ip2}" property="text" triggerEvent="change"
              required="true" minValue="1" maxValue="254"
              lowerThanMinError="More than 0, please" exceedsMaxError="Less than 255, please" />
          <mx:NumberValidator    source="{ip3}" trigger="{ip3}" property="text" triggerEvent="change"
              required="true" minValue="1" maxValue="254"
              lowerThanMinError="More than 0, please" exceedsMaxError="Less than 255, please" />
          <mx:NumberValidator    source="{ip4}" trigger="{ip4}" property="text" triggerEvent="change"
              required="true" minValue="1" maxValue="254"
              lowerThanMinError="More than 0, please" exceedsMaxError="Less than 255, please" />
             
                    
          <mx:TextInput id="ip1" width="40" maxChars="3" restrict="0-9"
              dataChange="updateReturnIPAddress()"
              change="updateReturnIPAddress()" updateComplete="updateReturnIPAddress()" />
          <mx:TextInput id="ip2" width="40" maxChars="3" restrict="0-9"
              dataChange="updateReturnIPAddress()"
              change="updateReturnIPAddress()" updateComplete="updateReturnIPAddress()" />
          <mx:TextInput id="ip3" width="40" maxChars="3" restrict="0-9"
              dataChange="updateReturnIPAddress()"
              change="updateReturnIPAddress()" updateComplete="updateReturnIPAddress()" />
          <mx:TextInput id="ip4" width="40" maxChars="3" restrict="0-9"
              dataChange="updateReturnIPAddress()"
              change="updateReturnIPAddress()" updateComplete="updateReturnIPAddress()" />

       

      </mx:HBox>