Remove local changes from DataGrid in Flex

118 Views Asked by At

I am working Apache Flex 4.11 web application. I have a DataGrid which has item renderer for change the record. Now I change the record in the Datagrid using item renderer. Now there is a cancel button out side the DataGrid when I click on that it should remove the local changes(which I have done it for change the record) and show the original data which is showing before the change.

How can I do that.

Thanks Bikrant Singh

1

There are 1 best solutions below

2
SushiHangover On

1st) Make a copy/backup of your dataProvider's data, not a reference copy, but a true deep copy, do this before you allow the user to start editing.

If you were using an ArrayCollection, you could use the Flex based ObjectUtil:

dataGridDataBackup = ObjectUtil.copy(myDataProvidersData) as ArrayCollection;

VectorCollection does not have a clone/copy (need to verify this...), but you can easily sub-class it and add a AMF-based (via byteArray) deep-copy, 5/6s line of code for that...

2nd) On your Cancel button click eventHandler:

myDataProvidersData.disableAutoUpdate();
myDataProvidersData.removeAll();
myDataProvidersData.addAll(ObjectUtil.copy(dataGridDataBackup) as ArrayCollection);
myDataProvidersData.enableAutoUpdate();

Note: This is from memory as I do not have AS3/Flex on this machine but should be pretty close...

Update:

Shallow copy a vector:

var myNewVector:Vector.<yourvectortype> = orginalVector.concat();

Deep copy (just change it to a vector instead of a dynamic object for performance):

function deepCopy(source:Object):* { 
    var _ba:ByteArray = new ByteArray(); 
    _ba.writeObject(source); 
    _ba.position = 0; 
    return(_ba.readObject()); 
}

Note: If you are creating a vector of some complex class/object, you might need to register these class(es)... Again from memory, so adjust as needed...