1

I have an advanced data grid with many rows and columns(editable). I want to save the data from grid only if there is any change made in the grid. How can this be done without using boolean variable? Is there any inbuilt function for eg: like 'datagridId.isModified'.

BenMorel
  • 34,448
  • 50
  • 182
  • 322

2 Answers2

0

Here is the way to know that the data was really changed:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
    <mx:AdvancedDataGrid id="myGrid"
                         dataProvider="{dataCollection}"
                         width="200" height="100%" editable="true">
        <mx:groupedColumns>
            <mx:AdvancedDataGridColumn dataField="name"/>
            <mx:AdvancedDataGridColumn dataField="score"/>
        </mx:groupedColumns>
    </mx:AdvancedDataGrid>

    <mx:Script>  
        <![CDATA[  
            import mx.collections.ArrayCollection;
            import mx.events.CollectionEvent;

            [Bindable]  
            private var dataCollection:ArrayCollection = new ArrayCollection([  
                {name:"Name1", score:0},   
                {name:"Name2", score:10},   
            ]);

            private function init():void 
            {
                myGrid.dataProvider.addEventListener(CollectionEvent.COLLECTION_CHANGE, saveData);
            }

            private function saveData(event:CollectionEvent):void 
            {
                //your saving code
                trace("saving");
            }
        ]]>  
    </mx:Script> 
</mx:Application>
Serge Him
  • 936
  • 6
  • 8
0

Just as an FYI - the code above will fire in all scenarios - data provider refreshses, sorts, etc. There is a collection event kind that will limit when it dispatches

dataGrid.dataProvider.addEventListener(CollectionEvent.COLLECTION_CHANGE, function(e:CollectionEvent) {
    if(e.kind == CollectionEventKind.UPDATE){
         //this means something changed. event.items will tell you what changed. 

    }
}

=====

Another approach is to use the itemEditEnd event. From the adobe docs:

<mx:AdvancedDataGrid id="myGrid" 
        itemEditEnd="onItemEditEnd(event);" >   
            private function onItemEditEnd(event:DataGridEvent):void {

                var myEditor:TextInput = 
                    TextInput(event.currentTarget.itemEditorInstance);
                var newVal:String = myEditor.text;
                var oldVal:String = event.currentTarget.editedItemRenderer.data[event.dataField];  
            }   

===

Finally, a plug : Our Ultimate DataGrid internally keeps track of all changed items, via its changes collection. http://flexicious.com/Home/Ultimate (Change Tracking API) examnple

flexicious.com
  • 1,601
  • 1
  • 13
  • 21