Flex AdvancedDataGrid添加汇总行处理

表格行汇总是一个很普遍的业务需求,比如编辑单元格数据时,用户往往想知道,该列的总和是多少?这就需要给表格添加汇总行。Flex本身自带的Datagrid或者AdvancedDataGrid都没有此功能,但是我们只要稍作处理便可实现简单的表格行汇总,思路如下(假设表格的dataprovoider都是ArrayCollection):

1datagrid填充完数据时,给ArrayCollection插入一条记录,该记录用于存放每列的汇总信息;

2datagrid单元格编辑的时候,处理ItemEditEnd事件,在该事件中队每列进行重新汇总;

       该思路有个缺点,那就是表格新增记录时要确保汇总行是ArrayCollection的最后一条记录,执行新增或者删除行记录时都需要手工进行汇总处理。下述为具体的实现方法:

       1ItemEditEnd事件处理及汇总处理代码,sum_fields为要汇总的列

       public static function getGridColSumByEvent(event:Event,ds_list:ArrayCollection,sum_fields:Array):void{

           //if edited column is not sum column then return

           if( sum_fields.indexOf(event["dataField"]) < 0 ){

              return;

           }

           if( ds_list.length <= 1 )

              return;

           var inputData:Object= event.currentTarget.itemEditorInstance.text;

          

           var itemadd:Object = ds_list[ds_list.length-1];

          

           for( var k:int=0;k<sum_fields.length;k++ ){

              var field:String = sum_fields[k].toString();

              var sum_num:Number = 0;

              for( var i:int=0;i<ds_list.length-1;i++ ){

                  var item:Object = ds_list[i];

                  var num:Number = Number(item[field]);

                  if( i == event["rowIndex"] && field == event["dataField"] ){

                     num = Number(inputData);

                  }

                  sum_num = Number(sum_num + num);

              }

              itemadd[field] = sum_num;

           }

           ds_list.setItemAt(itemadd,ds_list.length-1);

           return; 

       }

       //

       public static function getGridColSum(ds_list:ArrayCollection,sum_fields:Array):void{

           if( ds_list.length <= 1 )

              return;

          

           var itemadd:Object = ds_list[ds_list.length-1];

           for( var k:int=0;k<sum_fields.length;k++ ){

              var field:String = sum_fields[k].toString();

              var sum_num:Number = 0;

              for( var i:int=0;i<ds_list.length-1;i++ ){

                  var item:Object = ds_list[i];

                  var num:Number = Number(item[field]);

                  sum_num = Number(sum_num + num);

              }

              itemadd[field] = sum_num;

           }

           ds_list.setItemAt(itemadd,ds_list.length-1);

           return; 

       }

       2)假设表格填充数据为ArrayCollectiondp_data则只要做如下处理,即可在表格填充数据时即进行汇总。

       假设表格idadg1, adg1.dataProviderdp_datadp_data中的对象结构为{f1:1, s1:汇总列1, s2:汇总列2}

adg1.dataProvider = responder.lastResult;

    //加入汇总行

    //先加一个空行

    var newrow:PlanMonthPutInVOEX = new PlanMonthPutInVOEX();

    newrow["f1"] = "合计";

    adg1.dataProvider.addItem(newrow);

    var field:Array = new Array("s1","s2");   //汇总列字段

    getGridColSum (adg1.dataProvider as ArrayCollection,field);

    3adg1表格添加 itemEditEnd事件处理

    protected function itemEditEndHandle(event:AdvancedDataGridEvent):void{

var field:Array = new Array("s1","s2");   //汇总列字段

    getGridColSumByEvent(event,adg1.dataProvider as ArrayCollection,

       field);

    }

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页