0

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

  • mx or spark datagrid?, restoring a single cell, or row, or the entire dataset in the grid? Is the dataProvider an ArrayCollection? – SushiHangover Oct 14 '15 at 06:30
  • mx AdvanceddataGrid, we need to restore entire dataset in the grid. The dataProvider is VectorCollection. – BikrantSingh Oct 14 '15 at 06:58
  • Without seeing your code and I haven't use apache flex, I think that.. First, clone the original dataProvider(VectorCollection). If user clicks cancel button, set that cloned VectorCollection to DataGrid. – Yasuyuki Uno Oct 14 '15 at 07:00
  • Is there any example or any link please provide it to me. – BikrantSingh Oct 14 '15 at 07:13
  • Suppose if there is mx datagrid and restore entire dataset and the dataprovider is arrayCollection then How can i do that. – BikrantSingh Oct 14 '15 at 07:14
  • If the dataProvider is an ArrayCollection, this code can clone ArrayCollection. `import mx.utils.ObjectUtil; private var _clonedCollection:ArrayCollection = new ArrayCollection(); for each ( var _myObj:Object in _originalCollection ) { _clonedCollection.addItem(ObjectUtil.copy(_myObj)); } yourgrid.dataProvider = _clonedCollection;` – Yasuyuki Uno Oct 14 '15 at 07:22

1 Answers1

0

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...

SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • How can i make a clone of VectorCollection. Could you please provide me line of code or any link which help me. – BikrantSingh Oct 14 '15 at 08:41
  • That is not working i am using the following code. [Bindable] public var taskCollection:VectorCollection; [Bindable] public var tempArray:VectorCollection = new VectorCollection; taskCollection.addAllAt(result.taskList , (index * pageLen)); dgTaskResults.dataProvider = new VectorCollection(tempArray.source.slice((start * pageLen),(start * pageLen) + pageLen) ); So please tell me how can i make a clone/copy of taskCollection. – BikrantSingh Oct 14 '15 at 09:18