ABAP--如何实现合计字段分单位合计

1、增加一个参量 在你所要合计的FIELD 中添加一个 CFIELDNAME或者qfieldname = 你的单位的FIELD
2、Here is a simple example.
1) Add a unit field "UNIT" for PCS and TON
2) Refer to the unit measure field to the number field

REPORT ztest NO STANDARD PAGE HEADING  LINE-SIZE 170.
* ALV
TYPE-POOLS: slis.

TYPES: BEGIN OF typ_data,
       key(10),
       num  TYPE i,
       unit TYPE lvc_quan,
     END OF typ_data.
*--------------------------------------------------------------------
* g l o b a l   d a t a
*--------------------------------------------------------------------
DATA gt_outtab   TYPE TABLE OF typ_data WITH HEADER LINE.
DATA gt_fieldcat TYPE slis_t_fieldcat_alv.
DATA gt_sort     TYPE slis_t_sortinfo_alv.
DATA gs_variant  TYPE disvariant.

*--------------------------------------------------------------------
* Program constructor.
*--------------------------------------------------------------------
LOAD-OF-PROGRAM.
** For Display Variant
 gs_variant-report = sy-repid.
 PERFORM layout_sort_build CHANGING gt_sort.
 PERFORM prepare_field_catalog changing gt_fieldcat.

*--------------------------------------------------------------------
* s t a r t - o f - s e l e c t i o n.
*--------------------------------------------------------------------
START-OF-SELECTION.
 PERFORM selection.

 CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
       EXPORTING
          i_callback_program      = gs_variant-report
          it_fieldcat             = gt_fieldcat[]
          it_sort                 = gt_sort[]
          is_variant              = gs_variant
       TABLES
          t_outtab                = gt_outtab[]
       EXCEPTIONS
          program_error  = 1
          OTHERS         = 2.

*&---------------------------------------------------------------------*
*&      Form  prepare_field_catalog
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM prepare_field_catalog changing lt_fieldcat TYPE slis_t_fieldcat_alv..
 DATA ls_fieldcat TYPE slis_fieldcat_alv.

* Key
 CLEAR ls_fieldcat.
 ls_fieldcat-fieldname    = 'KEY'.            "#EC *
 ls_fieldcat-tabname      = 'gt_outtab'.
 ls_fieldcat-reptext_ddic = 'Key'(020).       "#EC *
 ls_fieldcat-outputlen    = 10.
 ls_fieldcat-no_sum       = 'X'.
 ls_fieldcat-key          = 'X'.
 APPEND ls_fieldcat TO lt_fieldcat.

* Num
 CLEAR ls_fieldcat.
 ls_fieldcat-fieldname    = 'NUM'.            "#EC *
 ls_fieldcat-tabname      = 'gt_outtab'.
 ls_fieldcat-reptext_ddic = 'Field Num'(021). "#EC *
* QFIELDNAME - unit measure field
 ls_fieldcat-qfieldname   = 'UNIT'.           "关键所在
 ls_fieldcat-outputlen    = 10.
 ls_fieldcat-do_sum       = 'X'.
 APPEND ls_fieldcat TO lt_fieldcat.

* UNIT
 CLEAR ls_fieldcat.
 ls_fieldcat-fieldname    = 'UNIT'.            "#EC *
 ls_fieldcat-tabname      = 'gt_outtab'.
 ls_fieldcat-reptext_ddic = 'Field Unit'(022). "#EC *
 ls_fieldcat-outputlen    = 10.
 ls_fieldcat-do_sum       = 'X'.
 APPEND ls_fieldcat TO lt_fieldcat.
ENDFORM.                    "prepare_field_catalog
*---------------------------------------------------------------------*
*       FORM LAYOUT_SORT_BUILD                                        *
*---------------------------------------------------------------------*
*       Sorting                                                       *
*---------------------------------------------------------------------*
*  -->  LT_SORT                                                       *
*---------------------------------------------------------------------*
FORM layout_sort_build CHANGING lt_sort TYPE slis_t_sortinfo_alv.
 DATA ls_sort TYPE slis_sortinfo_alv.
* Key
 CLEAR ls_sort.
 ls_sort-fieldname = 'KEY'.  "#EC *
 ls_sort-spos      = 1.
 ls_sort-up        = 'X'.
 ls_sort-subtot    = 'X'.
 APPEND ls_sort TO lt_sort.
ENDFORM.  " LAYOUT_sort_build
*&---------------------------------------------------------------------*
*&      Form  selection
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM selection .
* load the testing data
 gt_outtab-key = 'Group 1'. gt_outtab-num = 10. gt_outtab-unit = 'PCS'.
 APPEND gt_outtab.
 gt_outtab-key = 'Group 1'. gt_outtab-num = 10. gt_outtab-unit = 'TON'.
 APPEND gt_outtab.
 gt_outtab-key = 'Group 1'. gt_outtab-num = 20. gt_outtab-unit = 'PCS'.
 APPEND gt_outtab.

 gt_outtab-key = 'Group 2'. gt_outtab-num = 20. gt_outtab-unit = 'TON'.
 APPEND gt_outtab.
 gt_outtab-key = 'Group 2'. gt_outtab-num = 40. gt_outtab-unit = 'PCS'.
 APPEND gt_outtab.
 gt_outtab-key = 'Group 2'. gt_outtab-num = 10. gt_outtab-unit = 'TON'.
 APPEND gt_outtab.
ENDFORM.                    " selection 

url:http://www.erphome.net/wdb/wdbread.php?forumid=14&filename=f_3206

posted on 2007-07-31 11:53  毛小娃  阅读(248)  评论(0编辑  收藏  举报

导航