DISPLAY SUBTOTALS AND TOTAL IN ALV REPORTS:
Display report output with subtotals and total based on sales document number. We used function module REUSE_ALV_GRID_DISPLAY with type SLIS_T_SORTINFO_ALV.
*&---------------------------------------------------------------------*
*& Report ZR_ALV_SUBTOTALS_TOTALS
*&
*&---------------------------------------------------------------------*
REPORT ZR_ALV_SUBTOTALS_TOTALS.
TYPE-POOLS SLIS .
tables : vbap.
TYPES : BEGIN OF TY_VBAP,
VBELN TYPE VBAP-VBELN,
POSNR TYPE VBAP-POSNR,
MATNR TYPE VBAP-MATNR,
NETWR TYPE VBAP-NETWR,
END OF TY_VBAP.
DATA : I_VBAP TYPE TABLE OF TY_VBAP .
DATA : WA_VBAP TYPE TY_VBAP .
DATA : I_FCAT TYPE SLIS_T_FIELDCAT_ALV .
DATA : WA_FCAT LIKE LINE OF I_FCAT .
DATA : I_SORT TYPE SLIS_T_SORTINFO_ALV .
DATA : WA_SORT LIKE LINE OF I_SORT .
select-options : s_vbeln for vbap-vbeln.
START-OF-SELECTION .
PERFORM GET_DATA .
PERFORM CREATE_FCAT.
PERFORM CALC_SUBTOT.
END-OF-SELECTION .
PERFORM DISP_ALV .
*---------------------------------------------------------------------*
* FORM Get_data
*---------------------------------------------------------------------*
FORM GET_DATA .
SELECT VBELN POSNR MATNR NETWR
FROM VBAP
INTO TABLE I_VBAP
where vbeln in s_vbeln
.
ENDFORM.
FORM DISP_ALV .
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = SY-REPID
IT_FIELDCAT = I_FCAT
IT_SORT = I_SORT
TABLES
T_OUTTAB = I_VBAP
* EXCEPTIONS
* PROGRAM_ERROR = 1
* OTHERS = 2
.
IF SY-SUBRC NE 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDFORM. " DISP_ALV
FORM CREATE_FCAT .
WA_FCAT-COL_POS = '1' .
WA_FCAT-FIELDNAME = 'VBELN' .
WA_FCAT-TABNAME = 'I_VBAP' .
WA_FCAT-SELTEXT_M = 'SDNO' .
WA_FCAT-KEY = 'X' .
APPEND WA_FCAT TO I_FCAT .
CLEAR WA_FCAT .
WA_FCAT-COL_POS = '2' .
WA_FCAT-FIELDNAME = 'POSNR' .
WA_FCAT-TABNAME = 'I_VBAP' .
WA_FCAT-SELTEXT_M = 'ITEMNO' .
* WA_FCAT-NO_OUT = 'X' .
WA_FCAT-HOTSPOT = 'X' .
APPEND WA_FCAT TO I_FCAT .
CLEAR WA_FCAT .
WA_FCAT-COL_POS = '3' .
WA_FCAT-FIELDNAME = 'MATNR' .
WA_FCAT-TABNAME = 'I_VBAP' .
WA_FCAT-SELTEXT_M = 'MATERIALNO' .
* WA_FCAT-EDIT = 'X' .
APPEND WA_FCAT TO I_FCAT .
CLEAR WA_FCAT .
WA_FCAT-COL_POS = '4' .
WA_FCAT-FIELDNAME = 'NETWR' .
WA_FCAT-TABNAME = 'I_VBAP' .
WA_FCAT-SELTEXT_M = 'NETPRICE' .
WA_FCAT-EMPHASIZE = 'C610'.
WA_FCAT-DO_SUM = 'X' .
APPEND WA_FCAT TO I_FCAT .
CLEAR WA_FCAT.
ENDFORM.
FORM CALC_SUBTOT .
WA_SORT-FIELDNAME = 'VBELN '.
WA_SORT-UP = 'X'.
WA_SORT-SUBTOT = 'X '.
APPEND WA_SORT TO I_SORT.
ENDFORM.
OUTPUT:
Display report output with subtotals and total based on sales document number. We used function module REUSE_ALV_GRID_DISPLAY with type SLIS_T_SORTINFO_ALV.
*&---------------------------------------------------------------------*
*& Report ZR_ALV_SUBTOTALS_TOTALS
*&
*&---------------------------------------------------------------------*
REPORT ZR_ALV_SUBTOTALS_TOTALS.
TYPE-POOLS SLIS .
tables : vbap.
TYPES : BEGIN OF TY_VBAP,
VBELN TYPE VBAP-VBELN,
POSNR TYPE VBAP-POSNR,
MATNR TYPE VBAP-MATNR,
NETWR TYPE VBAP-NETWR,
END OF TY_VBAP.
DATA : I_VBAP TYPE TABLE OF TY_VBAP .
DATA : WA_VBAP TYPE TY_VBAP .
DATA : I_FCAT TYPE SLIS_T_FIELDCAT_ALV .
DATA : WA_FCAT LIKE LINE OF I_FCAT .
DATA : I_SORT TYPE SLIS_T_SORTINFO_ALV .
DATA : WA_SORT LIKE LINE OF I_SORT .
select-options : s_vbeln for vbap-vbeln.
START-OF-SELECTION .
PERFORM GET_DATA .
PERFORM CREATE_FCAT.
PERFORM CALC_SUBTOT.
END-OF-SELECTION .
PERFORM DISP_ALV .
*---------------------------------------------------------------------*
* FORM Get_data
*---------------------------------------------------------------------*
FORM GET_DATA .
SELECT VBELN POSNR MATNR NETWR
FROM VBAP
INTO TABLE I_VBAP
where vbeln in s_vbeln
.
ENDFORM.
FORM DISP_ALV .
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = SY-REPID
IT_FIELDCAT = I_FCAT
IT_SORT = I_SORT
TABLES
T_OUTTAB = I_VBAP
* EXCEPTIONS
* PROGRAM_ERROR = 1
* OTHERS = 2
.
IF SY-SUBRC NE 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDFORM. " DISP_ALV
FORM CREATE_FCAT .
WA_FCAT-COL_POS = '1' .
WA_FCAT-FIELDNAME = 'VBELN' .
WA_FCAT-TABNAME = 'I_VBAP' .
WA_FCAT-SELTEXT_M = 'SDNO' .
WA_FCAT-KEY = 'X' .
APPEND WA_FCAT TO I_FCAT .
CLEAR WA_FCAT .
WA_FCAT-COL_POS = '2' .
WA_FCAT-FIELDNAME = 'POSNR' .
WA_FCAT-TABNAME = 'I_VBAP' .
WA_FCAT-SELTEXT_M = 'ITEMNO' .
* WA_FCAT-NO_OUT = 'X' .
WA_FCAT-HOTSPOT = 'X' .
APPEND WA_FCAT TO I_FCAT .
CLEAR WA_FCAT .
WA_FCAT-COL_POS = '3' .
WA_FCAT-FIELDNAME = 'MATNR' .
WA_FCAT-TABNAME = 'I_VBAP' .
WA_FCAT-SELTEXT_M = 'MATERIALNO' .
* WA_FCAT-EDIT = 'X' .
APPEND WA_FCAT TO I_FCAT .
CLEAR WA_FCAT .
WA_FCAT-COL_POS = '4' .
WA_FCAT-FIELDNAME = 'NETWR' .
WA_FCAT-TABNAME = 'I_VBAP' .
WA_FCAT-SELTEXT_M = 'NETPRICE' .
WA_FCAT-EMPHASIZE = 'C610'.
WA_FCAT-DO_SUM = 'X' .
APPEND WA_FCAT TO I_FCAT .
CLEAR WA_FCAT.
ENDFORM.
FORM CALC_SUBTOT .
WA_SORT-FIELDNAME = 'VBELN '.
WA_SORT-UP = 'X'.
WA_SORT-SUBTOT = 'X '.
APPEND WA_SORT TO I_SORT.
ENDFORM.
OUTPUT: