# ALV 小结之二：自定义小计列的值

ALV中有自带的求平均值的方法：do_sum = 'X'，但也有一种特殊情况，目前有A、B、C三列，C = A / B，在小计中要计算A、B列的和，小计C = A和 / B和。要实现这种方式很简单，捕获ALV列表的SUBTOTAL_TEXT事件，然后在事件处理器中进行相应的处理即可。见以下代码：

REPORT  zquy_alv_test_02.
TYPE-POOLS slis.
DATA :
it_fieldcat TYPE slis_t_fieldcat_alv,
wa_fieldcat TYPE slis_fieldcat_alv,
lt_sort  TYPE slis_t_sortinfo_alv,
lt_event TYPE slis_t_event,
c1 TYPE p DECIMALS 2,
BEGIN OF wa_data,
check TYPE c,
t(10) TYPE c,
t1(10) TYPE c,
a TYPE i,
b TYPE i,
c(10) TYPE c,
END OF wa_data,
wt_data LIKE wa_data OCCURS 0 WITH HEADER LINE.

START-OF-SELECTION.
PERFORM. get_data.
PERFORM. event_build.
PERFORM. sort_build.
PERFORM. set_alv_layout.
PERFORM. dis_alv_grid.
*&---------------------------------------------------------------------*
*&      Form  get_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM. get_data .
DATA i1 TYPE i.
DO 3 TIMES.
i1 = sy-index.
DO 4 TIMES.
wa_data-t = i1.
wa_data-t = i1.
wa_data-a = sy-index * i1.
wa_data-b = sy-index * i1 + 3.
c1 = wa_data-a * 100 / ( wa_data-a + wa_data-b ) .
wa_data-c = c1.
CONCATENATE wa_data-c '%' INTO wa_data-c.
APPEND wa_data TO wt_data.
ENDDO.
ENDDO.
ENDFORM.                    " get_data
*&---------------------------------------------------------------------*
*&      Form  event_build
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM. event_build .
DATA la_event TYPE slis_alv_event.
la_event-name = 'SUBTOTAL_TEXT'.
la_event-form. = 'CALLBACK_SUBTOTAL_TEXT'.
APPEND la_event TO lt_event.
CLEAR la_event.
ENDFORM.                    " event_build
*&---------------------------------------------------------------------*
*&      Form  sort_build
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM. sort_build .
DATA lwa_sort TYPE slis_sortinfo_alv.

lwa_sort-fieldname = 'T'.

lwa_sort-up = 'X'.

lwa_sort-subtot = 'X'.

APPEND lwa_sort TO lt_sort.

ENDFORM.                    " sort_build
*&---------------------------------------------------------------------*
*&      Form  set_alv_layout
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM. set_alv_layout .
PERFORM. sub_fill_alv_field_catalog USING:
'01' '01' 'T' 'L' '分组' 'X' ' ',
'01' '01' 'T1' 'L' '分组' ' ' ' ',
'01' '02' 'A' 'L' 'A' ' ' 'X',
'01' '03' 'B' 'L' 'B' ' ' 'X',
'01' '03' 'C' 'L' '百分比' ' ' ' '.
ENDFORM.                    " set_alv_layout
*&---------------------------------------------------------------------*
*&      Form  dis_alv_grid
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM. dis_alv_grid .
DATA:
lth_layout TYPE slis_layout_alv.

lth_layout-box_fieldname = 'CHECK'.
lth_layout-colwidth_optimize = 'X'.
lth_layout-zebra = 'X'.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program                = sy-repid
is_layout                         = lth_layout
it_fieldcat                       = it_fieldcat
it_sort                           = lt_sort
it_events                         = lt_event
TABLES
t_outtab                          = wt_data
*   EXCEPTIONS
*     PROGRAM_ERROR                     = 1
*     OTHERS                            = 2
.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

ENDFORM.                    " dis_alv_grid
*&---------------------------------------------------------------------*
*&      Form  sub_fill_alv_field_catalog
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_ROWPOS   text
*      -->P_COLPOS   text
*      -->P_FLDNAM   text
*      -->P_JUSTIF   text
*      -->P_SELTEXT  text
*      -->P_OUT      text
*      -->P_DOSUM    text
*----------------------------------------------------------------------*
FORM. sub_fill_alv_field_catalog  USING  p_rowpos    TYPE sycurow
p_colpos    TYPE sycucol
p_fldnam    TYPE fieldname
p_justif    TYPE char1
p_seltext   TYPE dd03p-scrtext_l
p_out       TYPE char1
p_dosum     TYPE c.
DATA: wa_lfl_fcat    TYPE  slis_fieldcat_alv.
wa_lfl_fcat-row_pos        =  p_rowpos.     "Row
wa_lfl_fcat-col_pos        =  p_colpos.     "Column
wa_lfl_fcat-fieldname      =  p_fldnam.     "Field Name
wa_lfl_fcat-just           =  p_justif.     "Screen Justified
wa_lfl_fcat-seltext_l      =  p_seltext.    "Field Text
wa_lfl_fcat-no_out         =  p_out.        "No output
wa_lfl_fcat-do_sum  =  p_dosum.
APPEND wa_lfl_fcat TO it_fieldcat.
CLEAR wa_lfl_fcat.
ENDFORM.                    "sub_fill_alv_field_catalog
*&---------------------------------------------------------------------*
*&      Form  callback_subtotal_text
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_TOTAL        text
*      -->P_SUBTOT_TEXT  text
*----------------------------------------------------------------------*
FORM. callback_subtotal_text CHANGING p_total LIKE wa_data p_subtot_text TYPE slis_subtot_text.

c1 = p_total-a * 100 / ( p_total-a + p_total-b ).
p_total-c = c1.
CONCATENATE p_total-c '%' INTO p_total-c.

CONCATENATE '小计' p_total-t INTO p_subtot_text-display_text_for_subtotal.

ENDFORM.                    "F_EVT_SUBTOTAL

11.jpg

• 博文量
13
• 访问量
40482