Monday 4 June 2012

Tutorial on Interactive Reports in ALV List/Grid

Here is the Interactive Report for ALV's.


Below code contains few Function Modules related to ALV's.
Just go through the below code where i have mentioned proper comments. Which helps you to make your work easy.


REPORT  ZALV_INTERACTIVE_REPORT2.

*//TABLES DECLARATION
TABLES: MARA.

*// FUNCTION GROUP TO HOLD ALV EVENTS
TYPE-POOLS: SLIS.

*//TYPES DECLARATION FOR MARA TABLE
TYPES: BEGIN OF TY_MARA,
       MATNR TYPE MARA-MATNR,"MATERIAL NUMBER
       ERSDA TYPE MARA-ERSDA,"CREATED ON
       ERNAM TYPE MARA-ERNAM,"CREATED BY
       END   OF TY_MARA.

DATA: IT_MARA TYPE STANDARD TABLE OF TY_MARA,
      WA_MARA TYPE TY_MARA.

*//TYPES DECLARATION FOR MARC TABLE
TYPES: BEGIN OF TY_MARC,
       MATNR TYPE MARC-MATNR,"MATERIAL NUMBER
       WERKS TYPE MARC-WERKS,"PLANT
       PSTAT TYPE MARC-PSTAT,"STATUS
       END   OF TY_MARC.

DATA: IT_MARC TYPE STANDARD TABLE OF TY_MARC,
      WA_MARC TYPE TY_MARC.

*//DECLARATIONS FOR FIELDCATALOGS
DATA: IT_FCAT1 TYPE SLIS_T_FIELDCAT_ALV,
      WA_FCAT1 TYPE SLIS_FIELDCAT_ALV.

DATA: IT_FCAT2 TYPE SLIS_T_FIELDCAT_ALV,
      WA_FCAT2 TYPE SLIS_FIELDCAT_ALV.

*//VARIABLE TO HOLD REPORT NAME
*//USED IN FUNCTION MODULE REUSE_ALV_GRID_DISPLAY
DATA: V_REPID TYPE SY-REPID VALUE 'ZALV_INTERACTIVE_REPORT2'.

*//TABLE AND WORKAREA FOR CATCHING EVENTS
*//USED IN FUNCTION MODULE REUSE_ALV_EVENTS_GET
DATA: IT_EVENTS TYPE SLIS_T_EVENT,
      WA_EVENTS TYPE SLIS_ALV_EVENT.

*//ALV LIST HEADER
*//USED IN FUNCTION MODULE REUSE_ALV_COMMENTRY_WRITE
DATA: IT_LISTHEADER1 TYPE SLIS_T_LISTHEADER,
      IT_LISTHEADER2 TYPE SLIS_T_LISTHEADER.

*//VARIABLES FOR READING DOUBLE CLICK EVENTS
DATA: R_UCOMM TYPE SY-UCOMM,
      RS_SELFIELD TYPE SLIS_SELFIELD.

*//VARIABLES FOR DISPLAYING ALV GRID TITLE
DATA:  I_TITLE_MARA TYPE LVC_TITLE VALUE 'FIRST LIST DISPLAYED'.
DATA:  I_TITLE_MARC TYPE LVC_TITLE VALUE 'SECONDARY LIST DISPLAYED'.
DATA:  v_layout TYPE slis_layout_alv.
DATA: MATNR_VAL TYPE MARA-MATNR.

SELECT-OPTIONS: S_MATNR FOR MARA-MATNR.

START-OF-SELECTION.
V_REPID = SY-REPID.
PERFORM BUILD_VALUES.     "RETRIEVING DATA
PERFORM BUILD_FCAT.       "FILLING FIELD CATALOG
PERFORM TOP_OF_PAGE.      "TOP OF PAGE
PERFORM GET_EVENTS.       "RETRIEVING EVENTS IN OUR REPORT
PERFORM POPULATE_EVENTS.  "POPULATING THE REQUIRED EVENTS
PERFORM DISPLAY_FCAT.     "DISPLAYING FIELDCATALOG
PERFORM USER_COMMAND USING R_UCOMM RS_SELFIELD.  "FOR LINE SELECTION


*&---------------------------------------------------------------------*
*&      Form  BUILD_VALUES
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form BUILD_VALUES .
SELECT MATNR ERSDA ERNAM
       FROM MARA
       INTO TABLE IT_MARA
       WHERE MATNR IN S_MATNR.
endform.                    " BUILD_VALUES
*&---------------------------------------------------------------------*
*&      Form  BUILD_FCAT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form BUILD_FCAT .
  WA_FCAT1-TABNAME = 'IT_MARA'.
  WA_FCAT1-FIELDNAME = 'MATNR'.
  WA_FCAT1-SELTEXT_M = 'MATERIAL NO'.
  APPEND WA_FCAT1 TO IT_FCAT1.
  CLEAR WA_FCAT1.
  WA_FCAT1-TABNAME = 'IT_MARA'.
  WA_FCAT1-FIELDNAME = 'ERSDA'.
  WA_FCAT1-SELTEXT_M = 'CREATED ON'.
  APPEND WA_FCAT1 TO IT_FCAT1.
  CLEAR WA_FCAT1.
  WA_FCAT1-TABNAME = 'IT_MARA'.
  WA_FCAT1-FIELDNAME = 'ERNAM'.
  WA_FCAT1-SELTEXT_M = 'CREATED BY'.
  APPEND WA_FCAT1 TO IT_FCAT1.
  CLEAR WA_FCAT1.
endform.                    " BUILD_FCAT

*&---------------------------------------------------------------------*
*&      Form  TOP_OF_PAGE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form TOP_OF_PAGE .
DATA: WA_LISTHEADER TYPE SLIS_LISTHEADER.

*//HEADER TEXT
WA_LISTHEADER-INFO =  'ALV REPORT FOR MATERIALS'.
WA_LISTHEADER-TYP  =  'H'.
APPEND WA_LISTHEADER TO IT_LISTHEADER1.
CLEAR WA_LISTHEADER.

*//SUB-ITEMS
WA_LISTHEADER-TYP  =  'S'.
WA_LISTHEADER-KEY  =  'DATE: '.
WA_LISTHEADER-INFO =  SY-DATUM.
APPEND WA_LISTHEADER TO IT_LISTHEADER1.
CLEAR WA_LISTHEADER.

WA_LISTHEADER-TYP  =  'S'.
WA_LISTHEADER-KEY  =  'TIME: '.
WA_LISTHEADER-INFO =  SY-UZEIT.
APPEND WA_LISTHEADER TO IT_LISTHEADER1.
CLEAR WA_LISTHEADER.

WA_LISTHEADER-TYP  =  'S'.
WA_LISTHEADER-KEY  =  'EXECUTED BY: '.
WA_LISTHEADER-INFO =  SY-UNAME.
APPEND WA_LISTHEADER TO IT_LISTHEADER1.
CLEAR WA_LISTHEADER.

  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      it_list_commentary       = IT_LISTHEADER1
     I_LOGO                    = 'VENKAT_LOGO'
*     I_END_OF_LIST_GRID       =
*     I_ALV_FORM               =
            .
  REFRESH IT_LISTHEADER1.
endform.                    " TOP_OF_PAGE

*&---------------------------------------------------------------------*
*&      Form  DISPLAY_FCAT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form DISPLAY_FCAT .
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
   EXPORTING
     i_callback_program                = v_repid
     i_callback_user_command           = 'USER_COMMAND'
     i_callback_top_of_page            = 'TOP_OF_PAGE'
    I_GRID_TITLE                       = I_TITLE_MARA
     is_layout                         = v_layout
     it_fieldcat                       = it_fcat1
*     IS_VARIANT                        = VARIANT
*     I_SAVE                            = 'U'
    IT_EVENTS                          = IT_EVENTS
    TABLES
      t_outtab                          = IT_MARA
 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.                    " DISPLAY_FCAT
*&---------------------------------------------------------------------*
*&      Form  GET_EVENTS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form GET_EVENTS .
CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
 EXPORTING
   I_LIST_TYPE           = 0
 IMPORTING
   ET_EVENTS             = IT_EVENTS
 EXCEPTIONS
   LIST_TYPE_WRONG       = 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.                    " GET_EVENTS
*&---------------------------------------------------------------------*
*&      Form  POPULATE_EVENTS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form POPULATE_EVENTS .
READ TABLE IT_EVENTS INTO WA_EVENTS WITH KEY NAME = 'TOP_OF_PAGE'.
IF SY-SUBRC EQ 0.
  WA_EVENTS-FORM = 'TOP_OF_PAGE'.
  MODIFY IT_EVENTS FROM WA_EVENTS
         TRANSPORTING FORM WHERE NAME = WA_EVENTS-NAME.
  CLEAR WA_EVENTS.
ENDIF.

READ TABLE IT_EVENTS INTO WA_EVENTS WITH KEY NAME = 'USER_COMMAND'.
IF SY-SUBRC EQ 0.
  WA_EVENTS-FORM = 'USER_COMMAND'.
  MODIFY IT_EVENTS FROM WA_EVENTS
         TRANSPORTING FORM WHERE NAME = WA_EVENTS-NAME.
  CLEAR WA_EVENTS.
ENDIF.
endform.                    " POPULATE_EVENTS
*&---------------------------------------------------------------------*
*&      Form  USER_COMMAND
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_R_UCOMM  text
*      -->P_TYPE  text
*      -->P_SY_UCOMM  text
*      -->P_RS_SELFIELD  text
*      -->P_TYPE  text
*      -->P_SLIS_SELFIELD  text
*----------------------------------------------------------------------*
form USER_COMMAND  using    R_UCOMM RS_SELFIELD TYPE SLIS_SELFIELD .
CASE R_UCOMM.
  WHEN '&IC1'.
      IF RS_selfield-FIELDNAME = 'MATNR'.
       READ TABLE IT_MARA INTO WA_MARA INDEX RS_selfield-tabindex.
       MOVE RS_SELFIELD-VALUE TO MATNR_VAL.
       ENDIF.
    PERFORM BUILD_VALUES_MARC.
    PERFORM BUILD_FCAT_MARC.
    PERFORM TOP_OF_PAGE_MARC.
    PERFORM GET_EVENTS_MARC.
    PERFORM POPULATE_EVENTS_MARC.
    PERFORM DISPLAY_FCAT_MARC.
ENDCASE.
REFRESH IT_FCAT2.
endform.                    " USER_COMMAND
*&---------------------------------------------------------------------*
*&      Form  BUILD_FCAT_MARC
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form BUILD_FCAT_MARC .
  WA_FCAT1-TABNAME = 'IT_MARC'.
  WA_FCAT1-FIELDNAME = 'MATNR'.
  WA_FCAT1-SELTEXT_M = 'MATERIAL NUMBER'.
  APPEND WA_FCAT1 TO IT_FCAT2.
  CLEAR WA_FCAT1.
  WA_FCAT1-TABNAME = 'IT_MARC'.
  WA_FCAT1-FIELDNAME = 'WERKS'.
  WA_FCAT1-SELTEXT_M = 'PLANT'.
  APPEND WA_FCAT1 TO IT_FCAT2.
  CLEAR WA_FCAT1.
  WA_FCAT1-TABNAME = 'IT_MARC'.
  WA_FCAT1-FIELDNAME = 'PSTAT'.
  WA_FCAT1-SELTEXT_M = 'STATUS'.
  APPEND WA_FCAT1 TO IT_FCAT2.
  CLEAR WA_FCAT1.

endform.                    " BUILD_FCAT_MARC
*&---------------------------------------------------------------------*
*&      Form  BUILD_VALUES_MARC
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form BUILD_VALUES_MARC .
REFRESH IT_MARC.
  SELECT SINGLE  MATNR WERKS PSTAT
         FROM MARC
         INTO WA_MARC
         WHERE MATNR = MATNR_VAL.
APPEND WA_MARC TO IT_MARC.
CLEAR WA_MARC.
endform.                    " BUILD_VALUES_MARC
*&---------------------------------------------------------------------*
*&      Form  TOP_OF_PAGE_MARC
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form TOP_OF_PAGE_MARC .
DATA: WA_LISTHEADER1 TYPE SLIS_LISTHEADER.
REFRESH IT_LISTHEADER1.
WA_LISTHEADER1-INFO =  'ALV REPORT FOR PLANTS'.
WA_LISTHEADER1-TYP  =  'H'.
APPEND WA_LISTHEADER1 TO IT_LISTHEADER2.
CLEAR  WA_LISTHEADER1.

WA_LISTHEADER1-TYP  =  'S'.
WA_LISTHEADER1-KEY  =  'DATE: '.
WA_LISTHEADER1-INFO =  SY-DATUM.
APPEND WA_LISTHEADER1 TO IT_LISTHEADER2.
CLEAR WA_LISTHEADER1.

WA_LISTHEADER1-TYP  =  'S'.
WA_LISTHEADER1-KEY  =  'TIME: '.
WA_LISTHEADER1-INFO =  SY-UZEIT.
APPEND WA_LISTHEADER1 TO IT_LISTHEADER2.
CLEAR WA_LISTHEADER1.

WA_LISTHEADER1-TYP  =  'S'.
WA_LISTHEADER1-KEY  =  'EXECUTED BY: '.
WA_LISTHEADER1-INFO =  SY-UNAME.
APPEND WA_LISTHEADER1 TO IT_LISTHEADER2.
CLEAR WA_LISTHEADER1.

  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      it_list_commentary       = IT_LISTHEADER2
      I_LOGO                   = 'VENKAT_LOGO'
*     I_END_OF_LIST_GRID       =
*     I_ALV_FORM               =
            .
  REFRESH IT_LISTHEADER2.
endform.                    " TOP_OF_PAGE_MARC
*&---------------------------------------------------------------------*
*&      Form  GET_EVENTS_MARC
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form GET_EVENTS_MARC .
CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
 EXPORTING
   I_LIST_TYPE           = 0
 IMPORTING
   ET_EVENTS             = IT_EVENTS
 EXCEPTIONS
   LIST_TYPE_WRONG       = 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.                    " GET_EVENTS_MARC
*&---------------------------------------------------------------------*
*&      Form  POPULATE_EVENTS_MARC
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form POPULATE_EVENTS_MARC .
READ TABLE IT_EVENTS INTO WA_EVENTS WITH KEY NAME = 'TOP_OF_PAGE'.
IF SY-SUBRC EQ 0.
  WA_EVENTS-FORM = 'TOP_OF_PAGE_MARC'.
  MODIFY IT_EVENTS FROM WA_EVENTS
         TRANSPORTING FORM WHERE NAME = WA_EVENTS-NAME.
  CLEAR WA_EVENTS.
ENDIF.

endform.                    " POPULATE_EVENTS_MARC
*&---------------------------------------------------------------------*
*&      Form  DISPLAY_FCAT_MARC
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form DISPLAY_FCAT_MARC .
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
   EXPORTING
     i_callback_program                = v_repid
     i_callback_top_of_page            = 'TOP_OF_PAGE_MARC'
    I_GRID_TITLE                      = I_TITLE_MARC
     is_layout                         = v_layout
     it_fieldcat                       = it_fcat2
    TABLES
      t_outtab                          = IT_MARC
 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.                    " DISPLAY_FCAT_MARC


------------------------------------------------------------------------------------------------------------      
           NOTE: If you have any queries, leave comments so that i acn get back to you.
           Thanks.
------------------------------------------------------------------------------------------------------------


No comments:

Post a Comment