Test ALV Display With Header & Footer

*Program to Test ALV Display With Header & Footer.
*&---------------------------------------------------------------------*
*& Report  ZRJR02                                                      *
*&---------------------------------------------------------------------*
REPORT  ZRJR02                                  .

*Table declaration.
TABLES:ZEMP_MST,ZDEPT_MST,ZDESG_MST,ZSL_TXN.

*Varriable declaration.
TYPE-POOLS SLIS.
DATA : POS TYPE I.
DATA  REPID  LIKE SY-REPID.

DATA : F1 TYPE SLIS_T_FIELDCAT_ALV,
       F2 TYPE SLIS_FIELDCAT_ALV,
       L_LAYOUT TYPE SLIS_LAYOUT_ALV.
DATA  L_POS TYPE I VALUE 1.               "position of the column
DATA  GT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV.
*DATA  GT_SORT TYPE SLIS_T_SORTINFO_ALV.

data:  GT_EVENTS    TYPE SLIS_T_EVENT,
       FS_EVENTCAT LIKE LINE OF GT_EVENTs.

*Internal table declaration.
*DATA  BEGIN OF IT_SORT OCCURS 5.
*      INCLUDE TYPE SLIS_SORTINFO_ALV.
*DATA  END OF IT_SORT.

DATA:BEGIN OF ITAB OCCURS 0,
      ZEMPNO    LIKE ZEMP_MST-ZEMPNO,
      ZEMPNAME  LIKE ZEMP_MST-ZEMPNAME,
      ZDEPTCD   LIKE ZEMP_MST-ZDEPTCD,
      ZDEPTNAME LIKE ZDEPT_MST-ZDEPTNAME,
      ZDESGCD   LIKE ZEMP_MST-ZDESGCD,
      ZDESGNAME LIKE ZDESG_MST-ZDESGNAME,
     END OF ITAB.
REFRESH ITAB.CLEAR ITAB.

START-OF-SELECTION.

SELECT A~ZEMPNO A~ZEMPNAME A~ZDEPTCD B~ZDEPTNAME A~ZDESGCD C~ZDESGNAME
       FROM ZEMP_MST AS A
         INNER JOIN ZDEPT_MST AS B
           ON A~ZDEPTCD EQ B~ZDEPTCD
         INNER JOIN ZDESG_MST AS C
           ON A~ZDESGCD EQ C~ZDESGCD
       INTO CORRESPONDING FIELDS OF TABLE ITAB.
IF SY-SUBRC <> 0.
   MESSAGE E899(M3) WITH 'No records'.
ENDIF.

perform f_build_eventcat.
PERFORM LAYOUT.

END-OF-SELECTION.

*&---------------------------------------------------------------------*
*&      Form  LAYOUT
*&---------------------------------------------------------------------*
FORM LAYOUT .
  PERFORM FCAT USING 'ZEMPNO'    'ITAB' '' 'Emp.No.'   'ZEMPNO'    'ZEMP_MST'  ''.
  PERFORM FCAT USING 'ZEMPNAME'  'ITAB' '' 'Emp. Name' 'ZEMPNAME'  'ZEMP_MST'  ''.
  PERFORM FCAT USING 'ZDEPTCD'   'ITAB' '' 'Dept.Code' 'ZDEPTCD'   'ZEMP_MST'  ''.
  PERFORM FCAT USING 'ZDEPTNAME' 'ITAB' '' 'Dept.Name' 'ZDEPTNAME' 'ZDEPT_MST' ''.
  PERFORM FCAT USING 'ZDESGCD'   'ITAB' '' 'Desg.Code' 'ZDESGCD'   'ZEMP_MST'  ''.
  PERFORM FCAT USING 'ZDESGNAME' 'ITAB' '' 'Desg.Name' 'ZDESGNAME' 'ZDESG_MST' ''.

*  PERFORM LSORT USING  'ZEMPNO' 'IDATA' ''.
*  PERFORM LSORT USING  'ZEMPNAME' 'IDATA' ''.

*  MOVE IT_SORT[] TO GT_SORT[].

  REPID = SY-REPID.
  CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
    EXPORTING
         I_CALLBACK_PROGRAM       = REPID
         IT_FIELDCAT              = F1
*         IT_SORT                  = GT_SORT
         I_SAVE                   = 'X'
         IT_EVENTS                = GT_EVENTS[]

     TABLES
          T_OUTTAB                 = ITAB.
  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.                    " LAYOUT


*&---------------------------------------------------------------------*
*&      Form  FCAT
*&---------------------------------------------------------------------*
FORM FCAT USING P_FIELD P_TABLE P_SUM P_TEXT P_RFIELD P_RTABLE P_DISP.
  ADD 1 TO POS.
  F2-COL_POS       = POS.
  F2-FIELDNAME     = P_FIELD.
  F2-TABNAME       = P_TABLE.
  F2-SELTEXT_L     = P_TEXT.
  F2-REF_FIELDNAME = P_RFIELD.
  F2-REF_TABNAME   = P_RTABLE.
  F2-DO_SUM        = P_SUM.
  F2-NO_OUT        = P_DISP.
  APPEND F2 TO F1.
  CLEAR F2.
ENDFORM.                " FCAT

*&---------------------------------------------------------------------*
*&      Form  LSORT
*&---------------------------------------------------------------------*
*FORM LSORT USING P_FIELD P_TABLE P_UP.
*  ADD 1 TO L_POS.
*  IT_SORT-SPOS      = L_POS.
*  IT_SORT-FIELDNAME = P_FIELD.
*  IT_SORT-TABNAME   = P_TABLE.
*  IT_SORT-UP        = P_UP.
*  APPEND IT_SORT.
*ENDFORM.                    " LSORT
*-------------
FORM F_BUILD_EVENTCAT .
 CLEAR: GT_EVENTS.  REFRESH: GT_EVENTS.

  CLEAR: FS_EVENTCAT.
  FS_EVENTCAT-NAME = 'TOP_OF_PAGE'.
  FS_EVENTCAT-FORM = 'F_REPORT_HEADER_ALV'.
  APPEND FS_EVENTCAT TO GT_EVENTS.


  CLEAR: FS_EVENTCAT.
  FS_EVENTCAT-NAME = 'END_OF_LIST'.
  FS_EVENTCAT-FORM = 'F_WRITE_SUMMARY'.
  APPEND FS_EVENTCAT TO GT_EVENTS.

ENDFORM.                    " F_BUILD_EVENTCAT

FORM F_REPORT_HEADER_ALV.
CALL FUNCTION 'Z_YHEAD_PRINT'
 EXPORTING
   TITLE1        = 'XYZ Limited'
   TITLE2        = 'Employee Master'
   TITLE3        = 'Created on '
   COLOR         = 'X'
          .
ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  F_WRITE_SUMMARY
*&---------------------------------------------------------------------*
*       Write summary before exit
*----------------------------------------------------------------------*
FORM F_WRITE_SUMMARY .

write:/ 'Welcome to XYZ Limited'.
write:/ 'This is a test program to display Report in ALV Format'.

ENDFORM.

ABAP Tips

Related ABAP Topics:
A Sample ALV Program 
ALV Reporting - Z_LIST_MATERIALS

Get help for your ABAP problems
Do you have a ABAP Question?

SAP Books
SAP Certification, Interview Questions, Functional, Basis Administration and ABAP Programming Reference Books

More ABAP Tips

Main Index
SAP Basis, ABAP Programming and Other IMG Stuff
http://www.erpgreat.com

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.  The site www.erpgreat.com is in no way affiliated with SAP AG.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.