How to change the value of an item in a Flex ArrayCollection - actionscript-3

I have an ArrayCollection with values predefined. I want to assign a new value to items in the arrayCollection. This new value is define after datagrid ItemEditEnd.
private var tabRV :Array=[
{thevalue:"25",height:"115",col:"foo"},
{thevalue:"45",height:"115", col:"foo"},
{thevalue:"15",height:"115",col:"bb"},
{thevalue:"95",height:"115", col:"aa"},
];
[Bindable]
public var acRDV:ArrayCollection=new ArrayCollection(tabRV);
My ItemEditEnd, look like this:
private function getCellInfo(event:DataGridEvent):void {
// Get the new value from the editor.
var newVal:String = event.currentTarget.itemEditorInstance.htmlText;
// Get the old value.
var oldVal:String =
event.currentTarget.editedItemRenderer.data[event.dataField];
DP_LISTEREDVCLI[event.rowIndex].thevalue=newVal;
DP_LISTEREDVCLI[event.rowIndex].col=1;
trace("xml new val "+DP_LISTEREDVCLI[event.rowIndex].[event.columnIndex]+"/"+DP_LISTEREDVCLI[event.rowIndex].[DP_LISTEREDVCLI.length]);
}
I hope to create a generic function, DP_LISTEREDVCLI[event.rowIndex][event.columnIndex] but it doesn't work. Indeed in this case a new attribute is creating [event.columnIndex value]
Thanks for helping

Related

How to get all row value of datgrid in flex when i click on any row and compare with other datagrid

Actually i am new in flex my requirement is i have one datagrid which have 1 column name suppose it is (Name) this column has some value which is dynamic type what we want is we want all the row value when we click on any row then compare this datagrid with other datagrid when row value is match the check box get selected ..i wl be grateful if anybody can share any idea and links to me..
Thanks in advance
If what you need is to capture all the selected object, you can do it in any event of the grid, or a link to a column renderer follows.
Anywhere in the code
var entireObj:Object = dgDemo.selectedItem
Since an event of the grid
private function eventGrid(e:Event):void
{
var entireObj:Object = e.currentTarget.selectedItem
}
If you need all column data
[Bindable] private var MyArray2:ArrayCollection = new ArrayCollection([ {Label:"Item1", Value:100},
{Label:"Item2", Value:100},
{Label:"Item3", Value:100},
{Label:"Item4", Value:100},
{Label:"Item5", Value:100} ]);
private function eventGrid(e:ListEvent):void
{
var entireObj:Object = e.currentTarget.selectedItem //ENTIRE ROW
var columLabelSelected:String = e.currentTarget.columns[e.columnIndex].dataField //COLUMN LABEL SELECTED
var MyArray:Array = new Array(); //DATA ARRAY
for each(var objAux:Object in dgDemo.dataProvider) //FOR IN YOUR TYPE OBJECT
{
MyArray.push(objAux[columLabelSelected]) //ADD IN ARRAY DATA
}
}
]]>
</mx:Script>
<mx:DataGrid id="dgDemo" dataProvider="{this.MyArray2}" itemClick="eventGrid(event)"/>

Actionscript - how to pass object by value?

I want to pass an object to a function by value so that I can make modifications to that object. I don't want the original object to be updated. However, all the function parameters are passed by reference.
I've tried to copy an object ( var new_object:Object = original_object; ) This just creates a pointer to original_object.
Is there a way I can pass parameter by value?
update One workaround I see is to make deep copy of an object by using ByteArray as described here. Not sure how efficient it is. Maybe there is a better solution out there.
You will have to make a copy of the object before passing it to the function :
public function copy(value:Object):Object
{
var buffer:ByteArray = new ByteArray();
buffer.writeObject(value);
buffer.position = 0;
var result:Object = buffer.readObject();
return result;
}
public function testFunction(obj:Object):void
{
//do something with obj
}
public function test():void
{
var obj:Object = {};
testFunction(copy(obj));
}

How to point or reference a dynamically created item

I'm working on an invoice system. I need to add the invoice item dynamically by addchild method as below mentioned
1.select product(combo box)-- Quandity(text box)--Price(text box)--Total(text box)
2.select product(combo box)-- Quandity(text box)--Price(text box)--Total(text box)
My problem is i cant bring the sum amount of all the Total text boxes of each child element..
How to point or refernece a dynamically created item????
You can store all created item in a Array (or Vector), and then access each of them thru a cycle.
For example:
var allPrices:Array = [15.50, 20.24, 36.12];
var allElements:Array = new Array();
for (price in prices) {
// PriceText class is, for example, a movieclip with a textbox inside
var obj:PriceText = new PriceText(price);
addChild(obj);
allElements.push(obj);
}
Now you have all added elements in the allElements array.
Or You can create reference object , like :
class Bind {
public var target:Object;
public var key:String;
public function Bind(t:Object , k:String){
target = t;
key = k;
}
public function get value():* {
return target[key];
}
}
var myItem:SomeClass;
myItem.param = 100;
var bind:Bind = new Bind(myItem,"param");
trace("get myItem value:", bind.value);

as3 operator= /(object assign )

I know that AS3 does not have pointer or reference. Every object is pass by reference already. (I supposed?)
What should I do if I want to do with pointer?
e.g. all object point to one target, I only need to change target's value, then all other object will access different value.
You can effectively get the same behavior by using a helper object to simulate a pointer -- in other words using it to carry the target reference. For instance:
public class PseudoPointer
{
private var obj:Object;
private var prop:String;
public function PseudoPointer(obj:Object, prop:String)
{
// Point to property with name 'prop' on object 'obj'.
this.obj = obj;
this.prop = prop;
}
public function get value():* {
return obj[prop];
}
public function set value(value:*):void {
obj[prop] = value;
}
}
Then you could do this -- assume there's a magicNumber property on an object named target:
var numberPtr = new PseudoPointer(target, "magicNumber");
myDynamicObjectA.numberPtr = numberPtr;
myDynamicObjectB.numberPtr = numberPtr;
myDynamicObjectC.numberPtr = numberPtr;
Now any object that has a reference to the pseudo-pointer can read and write the target property:
numberPtr.value = 42;
You could create a function and in which you give it the value and then subsequently assign it to all of those other variables.
Something like below:
var objectA:Number;
var objectB:Number;
...
function myFunction(newValue:Number):void
{
objectA = newValue;
objectB = newValue;
...
}
You could try setting a variable reference to a function. Then if you update that reference, it would return a different function.
var myFunc:Function;
function funcOne():int {
return 1;
}
function funcTwo():int {
return 2;
}
function getFunc():Function {
return myFunc;
}
myFunc = funcOne;
var myObjOne:Object = new Object();
myObjOne.objFunc = getFunc;
var myObjTwo:Object = new Object();
myObjTwo.objFunc = getFunc;
trace(myObjOne.objFunc.call().call()); // 1
trace(myObjTwo.objFunc.call().call()); // 1
myFunc = funcTwo;
trace(myObjOne.objFunc.call().call()); // 2
trace(myObjTwo.objFunc.call().call()); // 2
This allows any object to point at a single function and have what that returns be updateable for all of them simultaneously. It's not the prettiest code and it's not as type-safe as if ActionScript had delegates to enforce the signatures of what's set for myFunc, but it is still a pretty flexible model if you get creative with it.
Have those pointers point to something that contains the object or has the object as a property on it.
So
var myArr:Array = [new YourObject()];
var client1:ArrayClient = new ArrayClient();
client1.array = myArr;
var client2:ArrayClient = new ArrayClient();
client2.array = myArr;
myArr[0] = new YourObject();
//client1.array[0]==client2.array[0]==the new object
However, this seems to be a great way to get yourself into a lot of trouble really quickly. What's the use case?

Changes to AS3 array of string variables doesn't update the variables themselves

I have to assume I am missing something simple, or I am not understanding something, but I can't seem to figure this.
I have a list of strings that I add to an array, then attempt to set those values in a for-loop using data that I read in. The array gets updated, but the values the array contains do not. I also have an array of buttons that I update this same way that works great, but strings don't seem to work the same way. I have tried moving the string array to give it full scope, and still nothing... What am I missing?
public class test extends Sprite
{
// Declare a list of strings
protected var title0:String = undefined;
protected var title1:String = undefined;
protected var title2:String = undefined;
protected function onDataLoad(evt:Event):void{
var titleArray:Array = new Array(title0,title1,title2); // Array of strings
for(i=0; i<=evt.target.data.count; i++){
titleArray[i] = evt.target.data["title"+i]; // attempt to set the title values
}
}
protected function function0(e:Event):void {
trace("My title is: " + title0); // title0 is null
}
}
It has to do with how AS3 stores Array elements. You would need to explicitly set both the instance property (title1, title2, title3) and the associated array element (array[0], array[1], array[2]) to achieve what you want. Or, you could drop the instance properties (title0, title1, title2) completely and just use the array to store your values. Something like:
public class test extends Sprite
{
protected var _titleArray:Array = [];
protected function onDataLoad(evt:Event):void{
for(i=0; i<=evt.target.data.count; i++){
// set the title values
_titleArray[i] = evt.target.data["title"+i];
}
}
protected function function0(e:Event):void {
trace("My title is: " + _titleArray[0]);
}
}