Search code examples
actionscript-3apache-flexarraycollectiondataprovider

Strange Behaviour on DataGrid with ArrayCollection as DataProvider


I have a Datagrid with an ArrayCollection as DataProvider, the arrayCollection is partially generated by a remoteObject call, the dataprovider seems to works at least until I try to edit the field...

By the RemoteObject I only receive an ArrayCollection with the field ip, but the datagrid looks for the fields ip, check and save... If I add/edit this new field it works, but only under particular condition

The DataGrid:

<s:DataGrid id="datagrid" left="10" right="10" top="136" 
            dataProvider="{listaIPCheck}" bottom="10" requestedRowCount="4">
    <s:columns>
        <s:ArrayList>
            <s:GridColumn dataField="ip" headerText="Asset"/>
            <s:GridColumn dataField="check" headerText="Inventory"/>
            <s:GridColumn dataField="save" headerText="Salvataggio"/>
        </s:ArrayList>
    </s:columns>
</s:DataGrid>

The Script:

[Bindable]private var listaIPCheck:ArrayCollection; 

private function ro_resultHandler(event:Event=null):void
{
  listaIPCheck = new ArrayCollection();
  listaIPCheck = ro.getListUpdate.lastResult;
  heap = 0;         
  // Read Below {POINT #1}
  init3();  
}

private function init3():void
{
 // Read Below {POINT #2}
 if (heap<listaIPCheck.length)
 {
    // omitted the initialization of the process p
    p.addEventListener(NativeProcessExitEvent.EXIT, onExit);
    try{                        
      p.start(startupInfo);
    }catch(e:Error){}
 }
}


private function onExit(e:NativeProcessExitEvent):void {    
    // Read below {POINT #3}
}

Here is my code, now as you can see there are 3 line where I wrote to read below... Let's assume to put this simple for instead of the commented line (once at a time)

for (var k:Number=0;k<listaIPCheck.length;k++)
{
  listaIPCheck.getItemAt(k).check = "checkVal";
  listaIPCheck.getItemAt(k).save = "saveVal";
}

This code always work in the 3 points, so at the end of the call the ArrayCollection is always filled with the new values, but the datagrid refresh the items only in point #1 and #2 Why not in Point #3???


Solution

  • The reason the DataGrid doesn't refresh when you change properties on an item in an ArrayCollection is that because changing the properties does not trigger the collectionChange event. The DataGrid has no way to know that properties within the object changed. It has to do with pointers and memory spaces and what exactly the DataGrid is looking at for binding purposes.

    In most cases, the invalidateList() method to force a refresh of the display. You can call the refresh() method or the itemUpdated() method on the collection or replace the dataProvider completely to force a refresh.