Robert Bruguera
Robert Bruguera

Reputation: 1

Get all elements in a group

How can I access the TextInput value in this example?

I'm using Flex 4.5

<s:VGroup id="qItems" width="100%" styleName="qMain">
  <s:HGroup id="id_1"><s:TextInput id="total_1" width="100" text="1.00"/></s:HGroup>
  <s:HGroup id="id_2"><s:TextInput id="total_2" width="100" text="1.00"/></s:HGroup>
  <s:HGroup id="id_3"><s:TextInput id="total_3" width="100" text="1.00"/></s:HGroup>
</s:VGroup>

I can only access the HGroup ids but I don't know how to go deeper to the TextInput to add the value of the total text.

This is the function that I'm using to access the HGroups

protected function viewElements(event:MouseEvent):void{
  var messageText:String = "There are " + qItems.numElements + " elements in the MX Panel.";

  for (var i:int = 0; i < qItems.numElements; i++)
  {
    var stringName:String = qItems.getElementAt(i) + "";
    if (stringName.indexOf(".") > 1)
    {
      stringName = stringName.substr(stringName.lastIndexOf(".")+1, stringName.length);
    }
    messageText += "\n" + (i+1) + ". " + stringName;
  }
  Alert.show(messageText, "Show Elements", Alert.OK);
}

Thanks, Robert

+++++++++++++++++++++++++++++++++++++++++++++

Dom, thanks a lot. That worked very good. Now I'm trying to add these values but I'm getting an NaN result with this function

protected function viewElements(event:MouseEvent):void{
  var totalField:Number;
  var sum:Number;
  for (var i:int = 0; i < qItems.numElements; i++)
  {
    var parentGroup:Object = qItems.getElementAt(i);
    for (var j:int = 0; j < parentGroup.numElements; j++) 
    {
      var childGroup:Object = parentGroup.getElementAt(j);
      totalField = childGroup.getElementAt(4).text;
    }
    sum += totalField;
  }
  Alert.show(sum.toFixed(2).toString());
}

Can you help me out with this. Thanks, again.

Upvotes: 0

Views: 1881

Answers (1)

Dom
Dom

Reputation: 2569

Just add another for loop and you are good

for (var i:int = 0; i < qItems.numElements; i++)
{
    var currentGroup:Object = qItems.getElementAt(i);

    for(var j:int = 0; j < currentGroup.numElements; j++)
    {
        Alert.show(currentGroup.getElementAt(j).text);
    }
}

Upvotes: 1

Related Questions