5 Replies Latest reply on Feb 6, 2011 2:47 PM by WebTrauma

    Accessing TextInput.text within a TileGroup itemEditor of a DataGrid

    WebTrauma Level 1

      Hi,

       

      I have a datagrid that has an itemEditor which is a TileGroup containing varying rows of TextInputs.

       

      In my MXDataGridItemRenderer, for each row in the TileGroup I am trying to retrieve the TextInput.text so I can put the new value back into the datagrid's dataProvider.

       

      I thought that creating a TextInput and doing a getElementAt() on the TileGroup would work, but it doesn't. Frustratingly, TileGroup.mxmlContent is write-only, so it doesn't look like I can access it that way.

       

      Please help !

       

       

      <?xml version="1.0" encoding="utf-8"?>
      <s:MXDataGridItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
                                xmlns:s="library://ns.adobe.com/flex/spark" 
                                xmlns:mx="library://ns.adobe.com/flex/mx">
          <fx:Script>
              <![CDATA[
                  // For a datagrid, provides a dynamic tileGroup to show multiple TextInput lines
                  import mx.collections.ArrayCollection;
                  
                  import spark.components.CheckBox;
                  import spark.components.TextInput;
                  
                  private var _linesTotal:Number;
                  private var _charPerLine:Number;
                  [Bindable]
                  private var _message:String;
                  
                  override public function set data( value:Object ) : void 
                  { 
                      if (value != null)
                      {
                          super.data = value; 
                          
                          _linesTotal = super.data.linesTotal;
                          _charPerLine = super.data.charPerLine;
                          
                          var selMessage:CheckBox;
                          var tiMessageLine:TextInput;
                          var currentLine:String;
                          
                          if (tgMessage.numElements != _linesTotal)    // number of lines in message has changed (or init)
                          {
                              tgMessage.removeAllElements();
                              
                              for (var i:int=0; i < _linesTotal; i++)
                              {
                                  if (tgMessage.numElements != _linesTotal)    
                                  {
                                      currentLine = "messageLine" + (i+1).toString();
                                      tiMessageLine = new TextInput();
                                      tiMessageLine.maxChars = super.data.charPerLine;
                                      
                                      if (super.data[currentLine] != null)
                                      {
                                          tiMessageLine.text = super.data[currentLine];    // put data from server into textInput.text
                                          tgMessage.width=150;
                                          tgMessage.addElement(tiMessageLine);
                                      }
                                  }
                                  else    // put message into parent datagrid's dataProvider
                                  {
                                      var tiMessageLineTeemp:TextInput = new TextInput();
                                      for (var j:int=0; j < _linesTotal; j++)
                                      {
                                          currentLine = "messageLine" + (j+1).toString();
                                          tiMessageLineTeemp == tgMessage.getElementAt(j);
                                          
                                          super.data[currentLine] = tiMessageLineTeemp.text;
                                      }
                                  }
                              }
                          }
                          tgMessage.invalidateDisplayList();
                      }
                  }
              ]]>
          </fx:Script>
          
          <s:TileGroup id="tgMessage" 
                      width="200"
                      x="5"
                      y="2"
                      verticalGap="1"
                      />
      </s:MXDataGridItemRenderer>
      
      
        • 1. Re: Accessing TextInput.text within a TileGroup itemEditor of a DataGrid
          Flex harUI Adobe Employee

          I would keep an array of all the TextInputs created then use the array to

          access them later.

          1 person found this helpful
          • 2. Re: Accessing TextInput.text within a TileGroup itemEditor of a DataGrid
            WebTrauma Level 1

            Thank you!

             

            Each row in the DataGrid has a TileGroup itemEditor, which in turn has multiple (array) of TextInputs. Aren't ItemEditors recycleable ? If so, then wouldn't my Array potentially get clobbered at the ItemEditor level?

             

            I think also it would have to be a 2 dimensional array since there are multiple rows in the DataGrid, each row having unique TextInput values ?

             

            The DataGrid/TileGroup/TextInput is within an MXML component. Should I store the array at that level, or extend DataGrid to have that array as a property?

            • 3. Re: Accessing TextInput.text within a TileGroup itemEditor of a DataGrid
              Flex harUI Adobe Employee

              You would store the array in each renderer, so it would be one-dimensional.

              It will and should get clobbered as they get recycled.  The key is that the

              array needs to be determined from the data item.

              1 person found this helpful
              • 4. Re: Accessing TextInput.text within a TileGroup itemEditor of a DataGrid
                WebTrauma Level 1

                TileGroup is already storing an array of my TextInputs, but TileGroup.mxmlContent is write-only. It would be more elegant to extend TileGroup to make public the accessor method to the mxmlContent array?

                 

                I found the get/set mxmlContent functions in Group, from which TileGroup inherits. What is the rationale behind making these private? If I manipulate this directly in my ItemEditor, am I going to violate/break some assumption in the framework ?

                 

                Thanks again !!!

                 

                 

                /**
                     *  @private
                     */
                    mx_internal function getMXMLContent():Array
                    {
                        if (_mxmlContent)
                            return _mxmlContent.concat();
                        else
                            return null;
                    }
                    
                   /**
                     *  @private
                     *  Adds the elements in <code>mxmlContent</code> to the Group.
                     *  Flex calls this method automatically; you do not call it directly.
                     *  
                     *  @langversion 3.0
                     *  @playerversion Flash 10
                     *  @playerversion AIR 1.5
                     *  @productversion Flex 4
                     */ 
                    private function setMXMLContent(value:Array):void
                    {
                        var i:int;
                        
                        // if there's old content and it's different than what 
                        // we're trying to set it to, then let's remove all the old 
                        // elements first.
                        if (_mxmlContent != null && _mxmlContent != value)
                        {
                            for (i = _mxmlContent.length - 1; i >= 0; i--)
                            {
                                elementRemoved(_mxmlContent[i], i);
                            }
                        }
                        
                        _mxmlContent = (value) ? value.concat() : null;  // defensive copy
                        
                        if (_mxmlContent != null)
                        {
                            var n:int = _mxmlContent.length;
                            for (i = 0; i < n; i++)
                            {   
                                var elt:IVisualElement = _mxmlContent[i];
                
                                // A common mistake is to bind the viewport property of an Scroller
                                // to a group that was defined in the MXML file with a different parent    
                                if (elt.parent && (elt.parent != this))
                                    throw new Error(resourceManager.getString("components", "mxmlElementNoMultipleParents", [elt]));
                
                                elementAdded(elt, i);
                            }
                        }
                    }
                
                • 5. Re: Accessing TextInput.text within a TileGroup itemEditor of a DataGrid
                  WebTrauma Level 1

                  This actually turned out to be really simple.   

                   

                  All I have to do is cast TileGroup.getElementAt(i) as TextInput.

                   

                   

                  override public function set data( value:Object ) : void 
                              { 
                                  if (value != null)
                                  {
                                      super.data = value; 
                                      
                                      _linesTotal = super.data.linesTotal;
                                      _charPerLine = super.data.charPerLine;
                                      
                                      var selMessage:CheckBox;
                                      var tiMessageLine:TextInput;
                                      var currentLine:String;
                                      
                                      if (tgMessage.numElements != _linesTotal)    // number of lines in message has changed (or init)
                                      {
                                          tgMessage.removeAllElements();
                                      }
                                          
                                      for (var i:int=0; i < _linesTotal; i++)
                                      {
                                          if (tgMessage.numElements != _linesTotal)    
                                          {
                                              currentLine = "messageLine" + (i+1).toString();
                                              tiMessageLine = new TextInput();
                                              tiMessageLine.tabEnabled=true;
                                              tiMessageLine.id = "ti" + currentLine;
                                              tiMessageLine.maxChars = super.data.charPerLine;
                                                  
                                              if (super.data[currentLine] != null)    // put data from server into textInput.text
                                              {
                                                  tiMessageLine.text = super.data[currentLine];
                                                  tgMessage.width=150;
                                                  tgMessage.addElement(tiMessageLine);
                                              }
                                          }
                                          else    // put message into parent datagrid's dataProvider
                                          {
                                              for (var j:int=0; j < _linesTotal; j++)
                                              {
                                                  currentLine = "messageLine" + (j+1).toString();
                                                  var tiMessageLineTemp:TextInput = new TextInput();
                                                  tiMessageLineTemp = tgMessage.getElementAt(j) as TextInput;
                                                  
                                                  super.data[currentLine] = tiMessageLineTemp.text;
                                              }
                                          }
                                      }
                                  }
                                      tgMessage.invalidateDisplayList();
                              }