拖放值时更新总值数据网格 - Adob​​e Flex [英] Update total value datagrid when drag drop values - Adobe Flex

查看:24
本文介绍了拖放值时更新总值数据网格 - Adob​​e Flex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据网格,每个都有总标签.该标签汇总了每个数据网格的所有值.当我将一个值从一个数据网格拖放到另一个数据网格时,必须根据每个数据网格的值更新两个标签总计.

I have two datagrid, and each has total label. This label sums all the values of each datagrid. When I drag and drop a value from one datagrid to another, the two label total must be updated according to the values of each datagrid.

但是,一笔款项被延迟了.

However, a sum is delayed.

代码:

<?xml version="1.0"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx" 
               width="650"
               creationComplete="initApp();">

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

            private function initApp():void {
                dgA.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}
                ]);

                dgB.dataProvider = new ArrayCollection([]);

                sumA();
            }

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

            public function sumA():void {
                var sum:Number = 0;
                for (var k:String in dgA.dataProvider){
                    sum += dgA.dataProvider[k]['Value'];
                }
                totalA.text = sum.toString();
            }

            public function sumB():void {
                var sum:Number = 0;
                for (var k:String in dgB.dataProvider){
                    sum += dgB.dataProvider[k]['Value'];
                }
                totalB.text = sum.toString();
            }

            public function dragDropHandler(event:DragEvent):void {
                sumA();
                sumB();
            }

        ]]>
    </fx:Script>

    <s:HGroup>

        <s:VGroup>
            <s:Label text="Cost 1"/>
            <mx:DataGrid id="dgA"
                         allowMultipleSelection="true" 
                         dragEnabled="true" 
                         dropEnabled="true" 
                         dragMoveEnabled="true"
                         dragDrop="dragDropHandler(event)">
                <mx:columns>
                    <mx:DataGridColumn dataField="Expense"/>
                    <mx:DataGridColumn dataField="Value"/>
                </mx:columns>    
            </mx:DataGrid>

            <s:Form>
                <s:FormItem label="Total">
                    <s:Label id="totalA"/>
                </s:FormItem>
            </s:Form>
        </s:VGroup>

        <s:VGroup>
            <s:Label text="Cost 2"/>
            <mx:DataGrid id="dgB"
                         allowMultipleSelection="true" 
                         dragEnabled="true" 
                         dropEnabled="true" 
                         dragMoveEnabled="true"
                         editable="true"
                         itemEditBeginning="disableEditing(event);"
                         dragDrop="dragDropHandler(event)">
                <mx:columns>
                    <mx:DataGridColumn dataField="Expense"/>
                    <mx:DataGridColumn dataField="Value" editorDataField="value">
                        <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:Form>
                <s:FormItem label="Total">
                    <s:Label id="totalB"/>
                </s:FormItem>
            </s:Form>
        </s:VGroup>

    </s:HGroup>

</s:Application>

推荐答案

使用callLater()方法从dragDropHandler调用sum方法如下

Use callLater() method to call the sum method from dragDropHandler as below

public function dragDropHandler(event:DragEvent):void {
       callLater(doTotal);
    }

    public function doTotal():void{
        sumA();
        sumB();
    }

我对其进行了测试,它对我来说运行良好.

I tested it and it is working fine for me.

这篇关于拖放值时更新总值数据网格 - Adob​​e Flex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆