Wednesday, May 5, 2010

Flex Actionscript GoodNess: Updating Datagrid Display when Model(ArrayCollection) changes

Normally, making the dataProvider bindable makes sure, that if the underlying VO(Value Object) changes, the changes are reflected in the dataGrid.
But In case if the VO used cannot be made Bindable it becomes a problem, for e.g if the VO is a Dictionary Object.
In that case we need to notify the listeners of ArrayCollection that one of the VOs has changed, following code demostrates that.

[Bindable]
var myDataArray:ArrayCollection = new ArrayCollection()

Lets Create the VOs to add to this collection
var empArr:Array = new Array()
var emp1:Dictionary = new Dictionary()
emp1.name ="john"
emp1.lastName="rick"
empArr.push(emp1)

var emp2:Dictionary = new Dictionary()
emp2.name ="richard"
emp2.lastName="martin"
empArr.push(emp2)

myDataArray = new ArrayCollection(empArr)

//Now lets set the dataProvider to the DataGrid


Now lets say if I go and update the emp1 Object, the value needs to get updated in
the DataGrid, but it will not unless I call itemUpdated() on the ArrayCollection.

Here is how I updated the DataGrid
emp1.name="UpdatedJohn"
myDataArray.itemUpdated(emp1) //And that will do the trick


Done!! Hurray!!

No comments:

Post a Comment