![]() |
![]() |
||||||
|
|||||||
| Tags: arraycollection, datagrid, editing, issue |
![]() |
|
|||
|
Hey everyone,
Here is the problem: I have a master ArrayCollection and a secondary ArrayCollection that is a copy of the master. I user the secondary AC as my dataProvider for the Datagrid. I allow the DG to be editable. Once a user changes the data within the DG, it updates the secondary AC and then updates the master AC. The thing is I don't want to update the master AC. I thought if you copy a collection there is no more reference to the master copy. How do I do this? I have tried different ways, secondary = master, secondary = new ArrayCollection(master.source), I even try looping through the master AC and adding each object to the secondary and it still updated the master. I have ran out of options. Can anyone solve this issue? <?xml version="1.0"?> <!-- itemRenderers\events\EndEditEventFormatter.mxml --> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" > <mx:Script> <![CDATA[ import flash.utils.setInterval; import mx.controls.TextInput; import mx.events.DataGridEvent; import mx.events.DataGridEventReason; import mx.formatters.NumberFormatter; import mx.collections.ArrayCollection; [Bindable] private var master:ArrayCollection = new ArrayCollection([ {Artist:'Pavement', Album:'Slanted and Enchanted', Price:11.99}, {Artist:'Pavement', Album:'Brighten the Corners', Price:11.99 } ]); [Bindable] private var secondary:ArrayCollection = master; // Define the number formatter. private var myFormatter:NumberFormatter=new NumberFormatter(); private function checkCol():void { trace(master.getItemAt(0).Artist); } // Define the eventlistner for the itemEditEnd event. public function formatData(event ataGridEvent):void {// Check the reason for the event. var intInter:int = setInterval(checkCol, 100); if (event.reason == DataGridEventReason.CANCELLED) { // Do not update cell. return; } // Get the new data value from the editor. var newData:String= TextInput(event.currentTarget.itemEditorInstance). text; // Determine if the new value is an empty String. if(newData == "") { // Prevent the user from removing focus, // and leave the cell editor open. event.preventDefault(); // Write a message to the errorString property. // This message appears when the user // mouses over the editor. TextInput(myGrid.itemEditorInstance).errorString= "Enter a valid string."; return; } // For the Price column, return a value // with a precision of 2. if(event.dataField == "Price") { myFormatter.precision=2; TextInput(myGrid.itemEditorInstance).text= myFormatter.format(newData); } } ]]> </mx:Script> <mx ataGrid id="myGrid"dataProvider="{secondary}" editable="true" itemEditEnd="formatData(event);" > <mx:columns> <mx ataGridColumn dataField="Artist"/><mx ataGridColumn dataField="Album"/><mx ataGridColumn dataField="Price"/></mx:columns> </mx ataGrid></mx:Application> |
| Sponsored Links |
|
|||
|
An assignment of anything but a primitive value, like String or int uses a
reference, and does not make a copy. Since the values in the array are objects, you cannot use slice() on the underlying array to get an independant copy. You will need to loop over the array, and for each item, create a new item object, assign its properties based on the master item. Access the AC's underlying array through the "source" property. Tracy |
![]() |
| Thread Tools | |
| Display Modes | |
|
|
- Contact Us
-|-
Adobe Dreamweaver Forums -|-
Archive -|-
Top -|-Rules/Disclaimer-|-Help/Support-|-Advertise