PDA

View Full Version : array.push not working


salim_designer
06-28-2007, 12:33 PM
I can not understand why a simple array.push method is not working

Here is my code;


<?xml version="1.0"?>
<!-- Simple example to demonstrate the Repeater class. -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

<mx:Script>
<![CDATA[

import mx.controls.Alert;


private var dp:Array = ["1", "2"];
[B] dp.push("3");

]]>
</mx:Script>

<mx:Panel title="Repeater Example" width="75%" height="75%"
paddingTop="10" paddingLeft="10" paddingRight="10" paddingBottom="10">

<mx:Text width="100%" color="blue"
text="Use the Repeater class to create 9 Button controls in a 3 by 3 Tile container."/>

<mx:Tile direction="horizontal" borderStyle="inset"
horizontalGap="10" verticalGap="15"
paddingLeft="10" paddingTop="10" paddingBottom="10" paddingRight="10">

<mx:Repeater id="rp" dataProvider="{dp}">
<mx:Button height="49" width="50"
label="{String(rp.currentItem)}"
click="Alert.show(String(event.currentTarget.getRepeaterI tem()) + ' pressed')"/>
</mx:Repeater>
</mx:Tile>

</mx:Panel>
</mx:Application>



I am getting the error:

1120: Access of undefined property dp.

dr_zeus
06-28-2007, 05:05 PM
You need to place the push call within a function.

salim_designer
06-29-2007, 04:34 AM
I tried to place the push call in a function, but it is not working. I might be doing some mistake.

Here is my code:

<?xml version="1.0"?>
<!-- Simple example to demonstrate the Repeater class. -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="fillInput()">

<mx:Script>
<![CDATA[
import mx.collections.*;
import mx.controls.Alert;

[Bindable]
private var dp:Array = ["1", "2"];

private function fillInput():void{
dp.push("3");
}


]]>
</mx:Script>

<mx:Panel title="Repeater Example" width="75%" height="75%"
paddingTop="10" paddingLeft="10" paddingRight="10" paddingBottom="10">

<mx:Text width="100%" color="blue"
text="Use the Repeater class to create 9 Button controls in a 3 by 3 Tile container."/>

<mx:Tile direction="horizontal" borderStyle="inset"
horizontalGap="10" verticalGap="15"
paddingLeft="10" paddingTop="10" paddingBottom="10" paddingRight="10">

<mx:Repeater id="rp" dataProvider="{dp}">
<mx:Button height="49" width="50"
label="{String(rp.currentItem)}"
click="Alert.show(String(event.currentTarget.getRepeaterI tem()) + ' pressed')"/>
</mx:Repeater>
</mx:Tile>

</mx:Panel>
</mx:Application>

drkstr
06-29-2007, 09:18 AM
You also have to call the function. You should treat everything in the script tags like it is a class. Functions in a class don't execute automatically.

Best regards,
...aaron

dr_zeus
06-29-2007, 05:13 PM
Oh, I see why you don't think it is working:

<mx:Repeater id="rp" dataProvider="{dp}">

The Repeater component will not detect changes to an Array data provider when you push new data into it. You'll probably have better luck with an ArrayCollection.

salim_designer
06-30-2007, 04:31 AM
My code is working now.

Now I am not using dataprovider with the repeater tag but under function I am writing repeater id. dataprovider = my array and it is working fine.

Thank you