Customize subtotal text and grandtotal text in ALV

REPORT  zalv_test.

TABLES: ekko.

TYPE-POOLS:slis.

TYPES: BEGIN OF x_data,
  ebeln TYPE char30,
  ebelp TYPE ebelp,
  matnr TYPE matnr,
  matnr1 TYPE matnr,
  ntgew TYPE entge,[img][/img]
END OF x_data.

TYPES: BEGIN OF x_data_tot,
  const(15),
  ebeln TYPE char30,
  ebelp TYPE ebelp,
  matnr TYPE matnr,
  matnr1 TYPE matnr,
  ntgew TYPE entge,
END OF x_data_tot.

DATA: i_ekpo TYPE STANDARD TABLE OF x_data INITIAL SIZE 0,
      i_ekpo_tot TYPE STANDARD TABLE OF x_data_tot INITIAL SIZE 0,
      i_fieldcat TYPE slis_t_fieldcat_alv,
      i_alv_top_of_page TYPE slis_t_listheader,
      i_events TYPE slis_t_event,
      i_sort TYPE slis_t_sortinfo_alv,
      i_event TYPE slis_t_event.

DATA:wa_ekko TYPE x_data,
      wa_ekko_tot TYPE x_data_tot,
      wa_layout TYPE slis_layout_alv,
      wa_events TYPE slis_alv_event,
      wa_sort TYPE slis_sortinfo_alv.

CONSTANTS:c_header TYPE char1 VALUE 'H',
          c_item TYPE char1 VALUE 'S'.

START-OF-SELECTION.
  SELECT ebeln ebelp matnr matnr ntgew FROM ekpo INTO TABLE i_ekpo UP TO 10 ROWS.
  LOOP AT i_ekpo INTO wa_ekko.
    MOVE-CORRESPONDING wa_ekko TO wa_ekko_tot.
    wa_ekko_tot-ntgew = sy-tabix.
    wa_ekko_tot-const = 'Grand Total'.
    APPEND wa_ekko_tot TO i_ekpo_tot.
  ENDLOOP.
  IF sy-subrc EQ 0.
    SORT i_ekpo_tot BY ebeln ebelp matnr.
  ENDIF.

  PERFORM sub_field_catalog.
  PERFORM sub_populate_layout.
  PERFORM sub_populate_sort.
  PERFORM sub_get_event.

END-OF-SELECTION.
  PERFORM sub_alv_report_display.

*&---------------------------------------------------------------------*
*&      Form  sub_field_catalog
*&---------------------------------------------------------------------*
FORM sub_field_catalog.
  PERFORM sub_fill_alv_field_catalog USING:
        '01' '01' 'CONST' 'I_EKPO_TOT' 'L'
        ' '(005) 'X' 'X' ' ' ' ',
        '01' '01' 'EBELN' 'I_EKPO_TOT' 'L'
        'Doc No'(003) ' ' ' ' ' ' ' ',
        '01' '02' 'EBELP' 'I_EKPO_TOT' 'L'
        'Item No'(004) ' ' ' ' ' ' ' ',
        '01' '03' 'MATNR' 'I_EKPO_TOT' 'L'
        'Material No'(005) 'X' 'X' ' ' ' ',
        '01' '03' 'MATNR1' 'I_EKPO_TOT' 'L'
        'Material No'(005) ' ' ' ' ' ' ' ',
        '01' '04' 'NTGEW' 'I_EKPO_TOT' 'R'
        'Net Weight'(007) ' ' ' ' ' ' ' '.
ENDFORM.                    "sub_field_catalog

*&---------------------------------------------------------------------*
*&      Form  sub_fill_alv_field_catalog
*&---------------------------------------------------------------------*
FORM sub_fill_alv_field_catalog USING p_rowpos TYPE sycurow
                                      p_colpos TYPE sycucol
                                      p_fldnam TYPE fieldname
                                      p_tabnam TYPE tabname
                                      p_justif TYPE char1
                                      p_seltext TYPE dd03p-scrtext_l
                                      p_out    TYPE char1
                                      p_tech  TYPE char1
                                      p_qfield TYPE slis_fieldname
                                      p_qtab TYPE slis_tabname.
  DATA: wa_lfl_fcat TYPE slis_fieldcat_alv.

  wa_lfl_fcat-row_pos = p_rowpos.
  wa_lfl_fcat-col_pos = p_colpos.
  wa_lfl_fcat-fieldname = p_fldnam.
  wa_lfl_fcat-tabname = p_tabnam.
  wa_lfl_fcat-just  = p_justif.
  wa_lfl_fcat-seltext_l = p_seltext.
  wa_lfl_fcat-no_out = p_out.
  wa_lfl_fcat-tech = p_tech.
  wa_lfl_fcat-qfieldname  = p_qfield.
  wa_lfl_fcat-qtabname  = p_qtab.

  IF p_fldnam EQ 'NTGEW'.
    wa_lfl_fcat-do_sum = 'X'.
  ENDIF.
  APPEND wa_lfl_fcat TO i_fieldcat.
  CLEAR wa_lfl_fcat.
ENDFORM.                    "sub_fill_alv_field_catalog

*&---------------------------------------------------------------------*
*&      Form  sub_populate_layout
*&---------------------------------------------------------------------*
FORM sub_populate_layout.
  CLEAR wa_layout.
  wa_layout-colwidth_optimize = 'X'.
  wa_layout-no_totalline = 'X'.
ENDFORM.                    "sub_populate_layout

*&---------------------------------------------------------------------*
*&      Form  sub_populate_sort
*&---------------------------------------------------------------------*
FORM sub_populate_sort.
  wa_sort-spos = '01'.
  wa_sort-fieldname = 'CONST'.
  wa_sort-tabname = 'I_EKPO_TOT'.
  wa_sort-up  = 'X'.
  wa_sort-subtot = 'X'.
  APPEND wa_sort TO i_sort.
  CLEAR wa_sort.

  wa_sort-spos = '02'.
  wa_sort-fieldname = 'MATNR'.
  wa_sort-tabname = 'I_EKPO_TOT'.
  wa_sort-up  = 'X'.
  wa_sort-subtot = 'X'.
  APPEND wa_sort TO i_sort.
  CLEAR wa_sort.
ENDFORM.                    "sub_populate_sort

*&---------------------------------------------------------------------*
*&      Form  sub_get_event
*&---------------------------------------------------------------------*
FORM sub_get_event.
  CONSTANTS: c_formname_subtotal_text TYPE slis_formname VALUE 'SUBTOTAL_TEXT'.

  DATA: l_s_event TYPE slis_alv_event.

  CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
    EXPORTING
      i_list_type     = 4
    IMPORTING
      et_events       = i_event
    EXCEPTIONS
      list_type_wrong = 0
      OTHERS          = 0.

  READ TABLE i_event INTO l_s_event WITH KEY name = slis_ev_subtotal_text.
  IF sy-subrc EQ 0.
    MOVE c_formname_subtotal_text TO l_s_event-form.
    MODIFY i_event FROM l_s_event INDEX sy-tabix.
  ENDIF.
ENDFORM.                    "sub_get_event

*&---------------------------------------------------------------------*
*&      Form  sub_alv_report_display
*&---------------------------------------------------------------------*
FORM sub_alv_report_display.
  DATA:l_repid TYPE syrepid.

  l_repid = sy-repid.

  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program = l_repid
      is_layout          = wa_layout
      it_fieldcat        = i_fieldcat
      it_sort            = i_sort
      it_events          = i_event
      i_default          = 'X'
      i_save             = 'A'
    TABLES
      t_outtab           = i_ekpo_tot
    EXCEPTIONS
      program_error      = 1
      OTHERS             = 2.
ENDFORM.                    "sub_alv_report_display

*&---------------------------------------------------------------------*
*&      Form  subtotal_text
*&---------------------------------------------------------------------*
*      -->P_TOTAL        text
*      -->P_SUBTOT_TEXT  text
*----------------------------------------------------------------------*
FORM subtotal_text CHANGING
          p_total TYPE any
          p_subtot_text TYPE slis_subtot_text.
  IF p_subtot_text-criteria = 'MATNR'.
    p_subtot_text-display_text_for_subtotal =
      'Material level total'.
  ENDIF.
ENDFORM.                    "subtotal_text

你可能感兴趣的:(C++,c,.net,C#,UP)