Monday, March 18, 2013

Read data from xml file received through HttpService


[Bindable]
private var imgXML:XML;


[Bindable]
private var carArray:Array;



private function displayImages(event:ResultEvent):void{

imgXML = XML(event.result);

var decoder:SimpleXMLDecoder = new SimpleXMLDecoder();

var data:Object = decoder.decodeXML( new XMLDocument(imgXML) );

carArray = ArrayUtil.toArray(data.images.image);

}

Wednesday, March 13, 2013

Flex pass data or objects from one mxml to another

The best way to pass the objects or array across the mxml's is to use Shared Objects.

Declaring a Shared Object :

public var studentSO:SharedObject = SharedObject.getLocal("student","/");

Set values to Shared Objects :

studentSO.data.id = 101;
studentSO.arr = new Array([1,2,3]);

studentSO.flush();


Get values from Shared Objects :

private var i:int = studentSO.data.id;
private var arr:Array = studentSO.arr;

Flex Http result into Array Collection

HttpService has two event hanlders (mostly used)

a) RESULT_EVENT
b) FAULT_EVENT

example:

private var http:HttpService;
private var ac:ArrayCollection;

private function init():void
{
        http = new HttpService();
        http.method = "POST";
        http.URL = "....";
        http.addEventListener(ResultEvent.RESULT, success);
        http.send();
}

private function success(event:ResultEvent):void
{
      Alert.show("Success");
      ac = event.result.root.base;
}


where my incoming xml looks like 

<root>
      <base>.....</base>
      <base>.....</base>
      <base>.....</base>
....
</root>

Flex Remove an Item from one Datagrid and push it into another Datagrid

To make this easier, we need to use Bindable ArrayCollections.

example:

[Bindable]
private var ac1:ArrayCollection;
[Bindable]

private var ac2:ArrayCollection;

private function init():void
{
        ac1 = new ArrayCollection(...);
        
        for( var s1:String in ac1)
            if( ac1[s1].id == 101)
            {
                ac2.addItem(ac1[s1]);
                ac1.removeItemAt( int(s1) );
            }

        dg1.dataProvider = ac1;
        dg2.dataProvider = ac2;
}

Flex Remove an Item from ArrayCollection

Use "removeItemAt()" function to remove an item from the array collection.

example:

private var ac:ArrayCollection = [ {id: 101, name:'John', status : 'Married'},
                                              {d: 102, name:'Steven', status : 'Single'} ];

private function init():void
{
     for( var s:String in ac )
          if( ac[s].status == 'Married' )
               ac.removeItemAt(int(s));
}

Flex : Updating a dataGrid when arrayCollection is modified.

Datagrid and ArrayCollection are Bindable with each other.
Hence if we add or remove elements to the ArrayCollections, Datagrid will update Automatically.

But, We need to bind them using [Bindable] tag.

example:

[Bindable]
private var ac:ArrayCollection;

private function init():void
{
      ac = new ArrayCollection(<xml file link or items>);
      dg.dataProvider = ac;
}

<Datagrid creationComplete = "init();" />

Flex Call a function after loading data into an Array.

Hi,

I have a logic of implementing this scenario. To call a function after loading data into the datagrid or wait until an array is filled...

Try to load the datagrid after the required operation is done.

ex:

[Bindable]
private var ac:ArrayCollection = {some xml file};

private function init():void
{
     dg.dataProvider = ac;
     ---
     ---
     ---
     Loading data into an Array which you need to compare datagrid values.
}

Modified version:



[Bindable]
private var ac:ArrayCollection = {some xml file};

private function init():void
{
     Loading data into an Array which you need to compare datagrid values.
     dg.dataProvider = ac;
     Using updateComplete event, we can call a function and do the required processing.
}

Flex Datagrid ItemRenderer, Access outside data and functions.

use "outerDocument" keyword

private var id:int;
private var arr:Array;

private function init():void
{
    id = 0;
    arr = new Array();
}


---
---
---


<s:GridColumn headerText="REGISTER" rendererIsEditable="true">
  <s:itemRenderer>
    <fx:Component>
      <s:GridItemRenderer>
 <fx:Script>
<![CDATA[
                          
                           private function initCB():void
                           {
                                   outerDocument.init();
                                   outerDocument.id = 101;
                                   outerDocument.arr = ["a","b","c"];
                            }

                 ]]>
     </fx:Script>
      
   <s:CheckBox id="cb" creationComplete="initCB()"/>
</s:GridItemRenderer>

Remove duplicate elements from Array.

use "SPLICE" function for integer values
use "forEach" function for String values

<Integer values>

private function uniqueArray(arr:Array):Array
{
for(var i:int=0; i<arr.length; i++)
for(var j:int=i+1; j<arr.length; j++)
if(arr[i]==arr[j])
arr.splice(j, 1);
return arr;
}

<String values>

private function uniqueArray(arr:Array):Array
{
var currentValue:String = "";

var tempArray:Array = new Array();
arr.sort(Array.CASESENSITIVE);
arr.forEach(
function(item:*, index:uint, array:Array):void {
if (currentValue != item) {
tempArray.push(item);
currentValue = item;
}
}
);
return tempArray.sort(Array.CASESENSITIVE);}