Display text 'Total' using OO-ALV

By Joyjit Ghosh, IBM India

We had one requirement where we need to display Total text in ALV grid using oops concept. To achieve it we did following things:

  1. Added one extra field in the ALV output table and populated it with a constant value (This is nothing but the text that need to be displayed as Total text) for all the records.

  2. Hide this field in field catalog level

  3. Populated the sort table for ALV with this field and enable subtotal display for it.

  4. Hide total line display in ALV by passing appropriate value in the Layout structure.

As this extra field contains constant value for all the records so subtotal on it is equivalent to total for rest of the fields. 

Ex:  

Output table

Extra Field

Field1

Field2

Field3

Field4

Total:

A

B

10

10

Total:

A

A

10

10

Total:

B

C

10

10

Total:

C

D

10

10

Final Output in ALV after subtotal on Extra Field

Field1

Field2

Field3

Field4

A

B

10

10

A

A

10

10

B

C

10

10

C

D

10

10

Total:

40

40

 Code:

*&----------------------------------------------------------------*
*& Report  Z_ALV_DEMO_TOTAL_TEXT
*&
*&----------------------------------------------------------------*
*&
*&
*&----------------------------------------------------------------*
REPORT  z_alv_demo_total_text.
* Type declaration for final table to display the output
TYPES: BEGIN OF ty_mara,
        srno TYPE char40, " Storing the total text
        matnr TYPE matnr, " Material
        ersda TYPE ersda, " Creation date
        ernam TYPE ernam, " Created by
        laeda TYPE laeda, " Last change date
        aenam TYPE aenam, " Last change by
        vpsta TYPE vpsta, " Maintenance status
        brgew TYPE brgew, " Gross weight
        ntgew TYPE ntgew, " Net weight
        gewei TYPE gewei, " Weight Unit
       END OF ty_mara.
* Type declaration for table storing temp. data
TYPES: BEGIN OF ty_mara_tmp,
        matnr TYPE matnr, " Material
        ersda TYPE ersda, " Creation date
        ernam TYPE ernam, " Created by
        laeda TYPE laeda, " Last change date
        aenam TYPE aenam, " Last change by
        vpsta TYPE vpsta, " Maintenance status
        brgew TYPE brgew, " Gross weight
        ntgew TYPE ntgew, " Net weight
        gewei TYPE gewei, " Weight Unit
      END OF ty_mara_tmp.
*  Internal table for storing final data
DATA: i_mara TYPE STANDARD TABLE OF ty_mara INITIAL SIZE 0.
* Work area for final table
DATA: w_mara TYPE ty_mara.
*  Internal table for storing temp. data
DATA: i_mara_tmp TYPE STANDARD TABLE OF ty_mara_tmp INITIAL SIZE 0.
* Work area for temp. table
DATA: w_mara_tmp TYPE ty_mara_tmp.
* Object variable for ALV grid
DATA: oref1 TYPE REF TO cl_gui_alv_grid.
* Field catalog table for ALV grid
DATA: fieldcat TYPE  lvc_t_fcat.
* Workarea for field catalog table
DATA: w_field TYPE lvc_s_fcat.
*  Internal table for storing info. for ALV grid
data: i_sort2 TYPE STANDARD TABLE OF lvc_s_sort INITIAL SIZE 0.
* Workarea for sort table
DATA: wa_sort2      TYPE  lvc_s_sort.
* Workarea for ALV layout
data: wa_layout     TYPE  lvc_s_layo.
START-OF-SELECTION.
* Fetch data
SELECT  matnr   " Material
        ersda   " Creation date
        ernam   " Created by
        laeda   " Last change date
        aenam   " Last change by
        vpsta   " Maintenance status
        brgew   " Gross weight
        ntgew   " Net weight
        gewei   " Weight Unit
  FROM mara
  INTO TABLE i_mara_tmp
  UP TO 100 ROWS.
  CHECK sy-subrc = 0.
* Populate final table
  LOOP AT i_mara_tmp INTO w_mara_tmp.
*   Storing the Total text need to be displayed in
*   ALV
    w_mara-srno = 'Total weight (Gross & Net)'.
    w_mara-matnr = w_mara_tmp-matnr.
    w_mara-ersda = w_mara_tmp-ersda.
    w_mara-ernam  = w_mara_tmp-ernam .
    w_mara-laeda = w_mara_tmp-laeda.
    w_mara-aenam = w_mara_tmp-aenam.
    w_mara-vpsta = w_mara_tmp-vpsta.
    w_mara-brgew = w_mara_tmp-brgew.
    w_mara-ntgew = w_mara_tmp-ntgew.
    w_mara-gewei = w_mara_tmp-gewei.
    APPEND w_mara TO i_mara.
  ENDLOOP.
* Calling the screen to display ALV
  CALL SCREEN 100.
*&----------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&----------------------------------------------------------------*
*       Display ALV report
*-----------------------------------------------------------------*
MODULE status_0100 OUTPUT.
  IF oref1 IS INITIAL.
*   Create ALV grid object
*   In this case we have not created any custom container in the screen,
*   Instead of that dummy container name is passed
*   ADVANTAGE: we can run this report in background without any problem
    CREATE OBJECT oref1
      EXPORTING
        i_parent          = cl_gui_custom_container=>screen0
      EXCEPTIONS
        error_cntl_create = 1
        error_cntl_init   = 2
        error_cntl_link   = 3
        error_dp_create   = 4
        OTHERS            = 5
        .
    CHECK sy-subrc = 0.
*   Preparing the field catalog
*   ZDEMO: Defined in DDIC, it's structure is same as TYPE ty_mara
*   defined in the program
    CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
      EXPORTING
        i_structure_name       = 'ZDEMO'
      CHANGING
        ct_fieldcat            = fieldcat
      EXCEPTIONS
        inconsistent_interface = 1
        program_error          = 2
        OTHERS                 = 3.
    IF sy-subrc = 0.
      LOOP AT fieldcat INTO w_field.
        IF w_field-fieldname = 'BRGEW' OR
          w_field-fieldname = 'NTGEW'.
*         Summation for Gross & Net weight
          w_field-do_sum = 'X'.
          MODIFY fieldcat FROM w_field TRANSPORTING do_sum.
        ENDIF.
        IF w_field-fieldname = 'SRNO'.
*         Hide this field so that it can display it's content i.e.
*         Total text in Subtotal level
          w_field-tech = 'X'.
          w_field-no_out = 'X'.
          MODIFY fieldcat FROM w_field TRANSPORTING tech no_out.
        ENDIF.
        CLEAR w_field.
      ENDLOOP.
    ENDIF.
*   Populate Sort table with SRNO field so that we can display the total
*   text in it's subtotal level
    wa_sort2-spos = 1.
    wa_sort2-fieldname = 'SRNO'.
    wa_sort2-up = 'X'.
    wa_sort2-subtot = 'X'.
    APPEND wa_sort2 TO i_sort2.
*   Hide the total line
    wa_layout-no_totline = 'X'.
*   Display the ALV grid
    CALL METHOD oref1->set_table_for_first_display
      EXPORTING
        is_layout                     = wa_layout
      CHANGING
        it_outtab                     = i_mara[]
        it_fieldcatalog               = fieldcat
        it_sort                       = i_sort2
      EXCEPTIONS
        invalid_parameter_combination = 1
        program_error                 = 2
        too_many_lines                = 3
        OTHERS                        = 4.
    IF sy-subrc <> 0.
    ENDIF.
*   Set the focus on the grid
    CALL METHOD cl_gui_alv_grid=>set_focus
      EXPORTING
        control           = oref1
      EXCEPTIONS
        cntl_error        = 1
        cntl_system_error = 2
        OTHERS            = 3.
    IF sy-subrc <> 0.
    ENDIF.
  ENDIF.
ENDMODULE.                 " STATUS_0100  OUTPUT

Output:

 

Please send us your feedback/suggestions at webmaster@SAPTechnical.COM 

HomeContribute About Us Privacy Terms Of Use • Disclaimer • SafeCompanies: Advertise on SAPTechnical.COM | Post JobContact Us  

Graphic Design by Round the Bend Wizards

footer image footer image