Assign Xml Values to dynamically created components - apache-flex

xml values are stored in 'arr' array collection. depending on the length of the array stored, the below described components are created and assign those values to relevant components dynamically.
For Example:
AdvanceSearch.mxml is one component, and another components as advanceSearchAtom.mxml. within 'advanceSearchAtom' has some sub components as textbox, combo box etc,. we can add many advanceSearchAtom.mxml inside 'AdvanceSearch.mxml'.
var container : AdvanceSearch;
var adv : advanceSearchAtom;
for(var i:int=0;i<arr.length;i++) {
adv = new advanceSearchAtom();
adv.field.text = arr[i].field;
adv.value.selectedIndex = arr[i].value;
container.addChild(adv);
}
Please let me know if anybody come across this type of problem. if any relevant link is appreciable. Thanks in advance

You didn't mention it, but I guess the problem is that you're getting null reference error (1009) on the following lines:
adv.field.text = arr[i].field;
adv.value.selectedIndex = arr[i].value;
Am I right?
This is because field and value are not yet created. As per the default component instantiation, Flex creates the children only when it is required - i.e., when it is to be displayed.
You can either listen to the creationComplete event of the AdvanceSearchAtom component and update the values from there; or have Binadble public variables in the AdvanceSearchAtom class, bind them to field.text and value.selectedIndex and assign xml values to those variables in the loop.
Using creation complete:
public var container:AdvanceSearch;
public var searchItems:Array = [];
public var arr:Array;
//assuming that arr has been initialized with xml values.
var adv:AdvanceSearchAtom;
for(var i:int=0;i<arr.length;i++) {
adv = new AdvanceSearchAtom();
adv.addEventListener(FlexEvent.CREATION_COMPLETE, onAtomCreated);
container.addChild(adv);
searchItems.push(adv);
}
public function onAtomCreated(e:Event):void
{
var adv:AdvanceSearchAtom = e.currentTarget as AdvanceSearchAtom;
if(!adv)
return;
var index:Number = searchItems.indexOf(adv);
adv.field.text = arr[index].field;
adv.value.selectedIndex = arr[index].value;
}
Using data binding:
Inside AdvanceSearchAtom.mxml
<mx:TextInput id="field" text="{textValue}"/>
<mx:ComboBox id="value" selectedIndex="{comboIndex}"/>
In the script block of AdvanceSearchAtom.mxml
[Bindable]
public var textValue:String;
[Bindable]
public var comboIndex:Number;
var adv:AdvanceSearchAtom;
In the AdvanceSearch class:
for(var i:int=0;i<arr.length;i++) {
adv = new AdvanceSearchAtom();
container.addChild(adv);
adv.field.text = arr[i].field;
adv.value.selectedIndex = arr[i].value;
}

Related

DataGrid not appropriate dataProvider value

I have an ArrayCollection filled with 'o' objects. This AC should be the dataProvider of a DataGrid. After setting the dp: dgMT.dataProvider=acDataGrid the DataGrid contains only the last item of the arrayCollection.
Code:
[Bindable]
public var acDataGrid:ArrayCollection = new ArrayCollection();
protected function ddlLanguage_changeHandler(event:IndexChangeEvent):void{
gcTranslate.headerText=Globals.acLanguages.getItemAt(ddlLanguage.selectedIndex,0).toString();
Globals.acActValues=convertXmlToArrayCollection(File.applicationDirectory.resolvePath("xmls"+File.separator+Globals.acFileNames.getItemAt(ddlLanguage.selectedIndex,0)));
Globals.acDataGrid.removeAll();
var o:DataGridObject = new DataGridObject();
var i:int=0;
var angol:Object;
for each(angol in Globals.acValues){
o.en=angol.value;
o.name=angol.name;
if(i<Globals.acActValues.length && o.name==Globals.acActValues.getItemAt(i,0).name){
o.translation=Globals.acActValues.getItemAt(i,0).value;
}
else{
o.translation="";
Globals.acActValues.addItemAt("",i);
}
acDataGrid.addItemAt(o,i);
trace("NAME: "+acDataGrid.getItemAt(i,0).name+" VAL:"+acDataGrid.getItemAt(i,0).en+"TRANS: "+acDataGrid.getItemAt(i,0).translation);
// the values are different!
i++;
}
dgMT.dataProvider=acDataGrid;//setting the dataProvider
}
How could I reach that the DataGrid's rows are filled with the correct values?
Thank you!
You're instantiating o just once, outside of the for loop. Which means your just changing the values of that instance's properties and adding that same instance to the dataprovider over and over. You should instead create a new instance on every iteration.
To fix this, simply move the instantiation of o inside the for loop:
for each(angol in Globals.acValues){
var o:DataGridObject = new DataGridObject();
...
}

Flex Advanced Data Grid w/ hierarchical data: How to access currentTarget fields on dragdrop event?

So this is driving me crazy. I've got an advanced data grid with a dataprovider that's an array collection w/ hierarchical data. Each object (including the children) has an id field. I'm trying to drag and drop data from within the ADG. When this happens, I need to grab the id off the drop target and change the dragged object's parentid field. Here's what I've got:
public function topAccountsGrid_dragDropHandler(event:DragEvent):void{
//In this function, you need to make the move, update the field in salesforce, and refresh the salesforce data...
if(checkActivateAccountManageMode.selected == true) {
var dragObj:Array = event.dragSource.dataForFormat("treeDataGridItems") as Array;
var newParentId:String = event.currentTarget['Id'];
dragObj[0].ParentId = newParentId;
} else {
return;
}
app.wrapper.save(dragObj[0],
new mx.rpc.Responder(
function():void {
refreshData();
},
function():void{_status = "apex error!";}
)
);
}
I can access the data I'm draggin (hence changing parentId) but not the currentTarget. I think the hierarchical data is part of the problem, but I can't find much in the documentation? Any thoughts?
event.currentTarget is not a node, it's the ADG itself. However, it's quite easy to get the information you want, since the ADG stores that data internally (as mx_internal).
I'm using the following code snippets (tested with Flex SDK 4.1) in a dragOver handler, but I guess it will work in a dragDrop handler too.
protected function myGrid_dragDropHandler(event:DragEvent):void
{
// Get the dragged items. This could either be an Array, a Vector or NULL.
var draggedItems:Object = getDraggedItems(event.dragSource);
if (!draggedItems)
return;
// That's our ADG where the event handler is registered.
var dropTarget:AdvancedDataGrid = AdvancedDataGrid(event.currentTarget);
// Get the internal information about the dropTarget from the ADG.
var dropData:Object = mx_internal::dropTarget._dropData;
// In case the dataProvider is hierarchical, get the internal hierarchicalData aka rootModel.
var hierarchicalData:IHierarchicalData = dropTarget.mx_internal::_rootModel;
var targetParent:Object = null;
// If it's a hierarchical data structure and the dropData could be retrieved
// then get the parent node to which the draggedItems are going to be added.
if (hierarchicalData && dropData)
targetParent = dropData.parent;
for each (var draggedItem:Object in draggedItems)
{
// do something with the draggedItem
}
}
protected function getDraggedItems(dragSource:DragSource):Object
{
if (dragSource.hasFormat("treeDataGridItems"))
return dragSource.dataForFormat("treeDataGridItems") as Array;
if (dragSource.hasFormat("items"))
return dragSource.dataForFormat("items") as Array;
if (dragSource.hasFormat("itemsByIndex"))
return dragSource.dataForFormat("itemsByIndex") as Vector.<Object>;
return null;
}
var dropData:Object = mx_internal::dropTarget._dropData;
should be
var dropData:Object = dropTarget.mx_internal::_dropData;
Try this.

Combine/merge Dynamic Objects in AS3

I have 2 dynamic objects and I want to build one to contain all the properties:
var o1:Object = {prop1:val1,prop2:val2,prop3:val3};
var o2:Object = {prop3:val3a,prop4:val4};
and I need to obtain a third object that looks like that:
{prop1:val1, prop2:val2, prop3:val3a, prop4:val4};
Basically I need a way to iterate through the object properties and to add new properties to the third object. I have to mention I'm quite new to AS3/Flash/Flex.
First question, do you really mean to have prop3 in both objects? you will need to decide what to do in case of a collision like that, which object has precedence.
Secondly, check out the introspection apis: http://livedocs.adobe.com/flex/3/html/help.html?content=usingas_8.html
something like this should work:
public function mergeDynamicObjects ( objectA:Object, objectB:Object ) : Object
{
var objectC:Object = new Object();
var p:String;
for (p in objectA) {
objectC[p] = objectA[p];
}
for (p in objectB) {
objectC[p] = objectB[p];
}
return objectC;
}
If the property exists in A and B, B's will overwrite A's. Also note that if the values of a property is an object, it will pass a reference, not a copy of the value. You might need to clone the object in those cases, depending on your needs.
Note: I haven't actually tested the above, but it should be close. Let me know if it doesn't work.
Updated to fix the errors. Glad it works for you though.
You can dynamically access/set properties on objects with the index operator. The for loop will itterate over the property names, so if you put it all together, the following test passes:
[Test]
public function merge_objects():void {
var o1:Object = {prop1:"one", prop2:"two", prop3:"three"};
var o2:Object = {prop3:"threeA", prop4:"four"};
var o3:Object = new Object();
for (var prop in o1) o3[prop] = o1[prop];
for (var prop in o2) o3[prop] = o2[prop];
assertThat(o3.prop1, equalTo("one"));
assertThat(o3.prop2, equalTo("two"));
assertThat(o3.prop3, equalTo("threeA"));
assertThat(o3.prop4, equalTo("four"));
}
you can iterate over the object properties like:
var obj1:Object = new Object();
for(var str:String in obj2){
obj1[str] = "any value"; // insert the property from obj2 to obj1
}

Unwanted binding

The situation is simple. I have a datagrid that gets its data from a webservice.
When data from the webservice is retrived it calls the following function:
private function onListReg():void
{
arrRegOld = WSAutoreg.list.lastResult as ArrayCollection;
arrReg = WSAutoreg.list.lastResult as ArrayCollection;
dgReg.dataProvider = autoreglist;
}
dgReg is the Datagrid. the arr variables are ArrayCollections defined like so:
private var arrRegOld:ArrayCollection = new ArrayCollection;
[Bindable]
private var arrReg:ArrayCollection = new ArrayCollection;
The intent is when I hit a update button, it compares arrRegOld with arrReg and see if any values have changes. The problem is whenever I change values on the Datagrid it changes on both the dataProvider and on both ArrayCollections.
Does anyone know why is this happening? What should I do so that the binding applies only to one ArrayCollection?
Appreciate any tip.
- Mike
Your lists are sharing the same objects, if you modify the first element from arrReg you will see the modification also in arrRegOld - it is not related to binding. You need to clone the objects. You have several choices:
a) Implement a clone method for your objects (recommended)
b) Use a generic method like this one:
private function clone(source:Object):*
{
var array:ByteArray=new ByteArray();
array.writeObject(source);
array.position=0;
return(array.readObject());
}
and call arrRegOld = clone (arrReg); after arrReg = WSAutoreg.list.lastResult as ArrayCollection;

Flex: Passing data to a php file under some condition

I am using an accordian in which has three childs. Each child has some textInput elements. Now, i want to send data written in currently selected accordian's child's textInputs.
I have created a function "configure" which is called when someone clicks a button. That function checks as to which child of accordian is selected. Whichever is selected, the textInputs' text of that child are stored in locally defined variables.
Now, i have no idea how to pass these variable to the HTTPService i am sending at the end of function configure.
Can anyone tell me what should i do now or if there is any other efficient solution?
Thankyou
Codes:
private function configure():void
{
var selectedAlgos:Array = algosList.selectedItems;
var selectedMode:Array;
if (modeAccordian.selectedIndex == 0)
{
var N_interface:String = N_interface.text;
var N_duration:String = N_duration.text;
selectedMode.push(N_interface);
selectedMode.push(N_duration);
}
else if (modeAccordian.selectedIndex == 1)
{
var F_filePath:String = F_filePath.text;
var F_filePrefix:String = F_filePrefix.text;
}
else if (modeAccordian.selectedIndex == 2)
{
var T_filePath:String = T_filePath.text;
var T_filePrefix:String = T_filePrefix.text;
var T_metaFile:String = T_metaFile.text;
var T_toMergeFile:String = T_toMergeFile.text;
var T_NAT:String = T_NAT.text;
var T_NATIP:String = T_NATIP.text;
}
configureService.send();
}
HTTPService:
<mx:HTTPService id="configureService" url="configure.php" resultFormat="object" method="POST">
<mx:request xmlns="">
<selectedAlgos>{selectedAlgos}</selectedAlgos>
<selectedMode>{selectedMode}</selectedMode>
</mx:request>
</mx:HTTPService>
According to the HTTPService docs:
public function send(parameters:Object = null):mx.rpc:AsyncToken
parameters:Object (default = null)
An Object containing name-value pairs or an XML object,
depending on the content type for service requests.
So I believe you can drop the mx:request section of your mxml, and just add this to the send request:
configureService.send(
{
selectedAlgos:selectedAlgos.join(","),
selectedMode:selectedMode.join(",")
}
);
Otherwise, if you want to use binding, you should make the selectedAlgos/selectedMode bindable members of the same class that configure is defined in.

Resources