View Single Post
  #1 (permalink)  
Old 07-24-2008, 03:29 PM
NathanThibert
 
Posts: n/a
Diggs:
Default Editing ArrayCollection with Datagrid 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(eventataGridEvent):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>

<mxataGrid id="myGrid"
dataProvider="{secondary}"
editable="true"
itemEditEnd="formatData(event);" >
<mx:columns>
<mxataGridColumn dataField="Artist"/>
<mxataGridColumn dataField="Album"/>
<mxataGridColumn dataField="Price"/>
</mx:columns>
</mxataGrid>
</mx:Application>



Reply With Quote
Sponsored Links