lundi 16 janvier 2017

Keep current value when edit in datagrid - Adobe Flex

I have two datagrids, and it's possible to drag any items from datagrid A to drop in datagrid B. In datagrid B, I can edit the values, and those values have limits minimum and maximum. But, I have a problem: when I edit a value in an item dropped in datagrib B, the value automatically change to minimum value.

The correct is keep the current value when edit.

Any idea?

The code:

<s:Application xmlns:fx="http://ift.tt/rYUnH9" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx"
               creationComplete="initApp();">

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

            private function initApp():void {
                costAgrid.dataProvider = new ArrayCollection([
                    {Expense:'Electricity', Value:300, minNo: 100, maxNo: 500},
                    {Expense:'Phone', Value:200, minNo: 50, maxNo: 300},
                    {Expense:'Contract A', Value:5000, minNo: 4000, maxNo: 6000},
                    {Expense:'Contract B', Value:6000, minNo: 4500, maxNo: 8500},
                    {Expense:'Contract C', Value:3000, minNo: 2500, maxNo: 3500}
                ]);

                costBgrid.dataProvider = new ArrayCollection([]);
            }

            private function disableEditing(event:DataGridEvent):void {
                if(event.columnIndex==0)
                {
                    event.preventDefault(); 
                }
            }

            protected function LabelFormatter(item:Object, column:DataGridColumn):String
            {
                return MoedaFormatter.format(item.Value);
            }

        ]]>
    </fx:Script>

    <fx:Declarations>
        <mx:CurrencyFormatter id="MoedaFormatter" precision="2" currencySymbol="R$" alignSymbol="left" decimalSeparatorTo="," decimalSeparatorFrom=","  thousandsSeparatorFrom="." thousandsSeparatorTo="."/>
    </fx:Declarations>

    <s:HGroup>

        <s:VGroup>
            <s:Label text="Cost 1"/>
            <mx:DataGrid id="costAgrid"
                         allowMultipleSelection="true" 
                         dragEnabled="true" 
                         dropEnabled="true" 
                         dragMoveEnabled="true">
                <mx:columns>
                    <mx:DataGridColumn dataField="Expense"/>
                    <mx:DataGridColumn dataField="Value" labelFunction="LabelFormatter"/>
                </mx:columns>    
            </mx:DataGrid>
        </s:VGroup>

        <s:VGroup>
            <s:Label text="Cost 2"/>
            <mx:DataGrid id="costBgrid"
                         allowMultipleSelection="true" 
                         dragEnabled="true" 
                         dropEnabled="true" 
                         dragMoveEnabled="true"
                         editable="true"
                         itemEditBeginning="disableEditing(event);">
                <mx:columns>
                    <mx:DataGridColumn dataField="Expense"/>
                    <mx:DataGridColumn dataField="Value" editorDataField="value" labelFunction="LabelFormatter">
                        <mx:itemEditor>
                            <fx:Component>
                                <mx:NumericStepper stepSize="1" width="35" height="20">
                                    <fx:Script>
                                        <![CDATA[
                                            override public function set data(value:Object):void
                                            {
                                                super.data = value;

                                                if (value && value.hasOwnProperty("minNo")) {
                                                    minimum = value.minNo;
                                                }

                                                if (value && value.hasOwnProperty("maxNo")) {
                                                    maximum = value.maxNo;
                                                }
                                            }
                                        ]]>
                                    </fx:Script>
                                </mx:NumericStepper>
                            </fx:Component>
                        </mx:itemEditor>
                    </mx:DataGridColumn>
                </mx:columns>    
            </mx:DataGrid>
        </s:VGroup>

    </s:HGroup>

</s:Application>

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire