apache-flex 显示数组集合中行值的总和- Adobe Flex

i5desfxk  于 2022-11-01  发布在  Apache
关注(0)|答案(1)|浏览(170)

我想显示数组集合中行值的总和。例如:

Definition     Value
Product 1      20.00
Product 2      50.00
Product 3      30.00

Total          100.00

我有这样的代码:

<mx:DataGrid id="srcgrid">
        <mx:columns>
            <mx:DataGridColumn dataField="Definition"/>
            <mx:DataGridColumn dataField="Value"/>
        </mx:columns>    
    </mx:DataGrid>

    <s:Form>
        <s:FormItem label="Total">
            <s:Label text="{total()}"/>
        </s:FormItem>
    </s:Form>

还有剧本:

public function total():String {
    var i:Number = 0;
    for each(var obj:Object in ArrayCollection(DataGrid(srcgrid).dataProvider)){
        i = i + obj.Value;
    }

    return i.toString();
}

你知道吗?
先谢谢你

iaqfqrcu

iaqfqrcu1#

total()函数在dataProvider内有任何内容之前被调用。
srcgrid.dataProvider也可以作为Object循环

<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" applicationComplete="addInitData(event)">

    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
        <mx:DataGrid id="srcgrid">
        <mx:columns>
            <mx:DataGridColumn dataField="Definition"/>
            <mx:DataGridColumn dataField="Value"/>
        </mx:columns>    
    </mx:DataGrid>

    <s:Form x="250">
        <s:FormItem label="Total">
            <s:Label id="total"/>
        </s:FormItem>
    </s:Form>
    <fx:Script>
        <![CDATA[
            import flash.events.Event;
            import mx.collections.ArrayList;

            private function addInitData(e:Event = null):void{
                var dataProvider:ArrayList = new ArrayList();
                for (var i:int = 0; i < 12; i++){
                    dataProvider.addItem({Definition : 'item_' + i, Value : i});                    
                }
                srcgrid.dataProvider = dataProvider;
                updTotal();
            }

            private function updTotal():void{
                var sum:Number = 0;
                for (var k:String in srcgrid.dataProvider){
                    sum += srcgrid.dataProvider[k]['Value'];
                }
                total.text = sum.toString();
            }
        ]]>
    </fx:Script>
</s:Application>

相关问题