Blocked list in ALV code example

Example: Blocked list in ALV

REPORT ZSAPN_ALV_BLOCKEDLIST.
TYPES: BEGIN OF TY_MARA, "user defined type for mara
        MATNR TYPE MARA-MATNR,
        MTART TYPE MARA-MTART,
        MBRSH TYPE MARA-MBRSH,
        MATKL TYPE MARA-MATKL,
        MEINS TYPE MARA-MEINS,
      END OF TY_MARA.
DATA : IT_MARA TYPE TABLE OF TY_MARA, "mara internal table
       WA_MARA TYPE TY_MARA. "mara work area
DATA : T_FCAT TYPE SLIS_T_FIELDCAT_ALV. "field catalog for MARA table
DATA : W_FCAT LIKE LINE OF T_FCAT.

TYPES: BEGIN OF TY_MAKT, "user defined type for MAKT
        MATNR TYPE MAKT-MATNR,
        SPRAS TYPE MAKT-SPRAS,
        MAKTX TYPE MAKT-MAKTX,
      END OF TY_MAKT.
DATA : IT_MAKT TYPE TABLE OF TY_MAKT, "makt internal table
       WA_MAKT TYPE TY_MAKT.
DATA : T_FCAT_MAKT TYPE SLIS_T_FIELDCAT_ALV. "makt field catalog
DATA : W_FCAT_MAKT LIKE LINE OF T_FCAT_MAKT.
PARAMETERS : P_MTART TYPE MARA-MTART. "material type input

START-OF-SELECTION.

  SELECT MATNR
         MTART
         MBRSH
         MATKL
         MEINS FROM MARA "get MARA data
    INTO TABLE IT_MARA UP TO 10 ROWS WHERE MTART = P_MTART.
  IF NOT IT_MARA IS INITIAL .
    SELECT MATNR
           SPRAS
           MAKTX FROM MAKT INTO TABLE IT_MAKT "get makt data
      FOR ALL ENTRIES IN IT_MARA WHERE MATNR = IT_MARA-MATNR.
  ENDIF.
***build fcat for MARA
  W_FCAT-COL_POS       = '1'. "coloum position
  W_FCAT-FIELDNAME     = 'MATNR'. "column name
  W_FCAT-TABNAME       = 'IT_MARA'. "table
  W_FCAT-REF_TABNAME   = 'MARA'. "table
  W_FCAT-REF_FIELDNAME = 'MATNR'. "referance field, it will show descriptions automatically
  APPEND W_FCAT TO T_FCAT.
  CLEAR W_FCAT.

  W_FCAT-COL_POS       = '2'.
  W_FCAT-FIELDNAME     = 'MTART'.
  W_FCAT-TABNAME       = 'IT_MARA'.
  W_FCAT-REF_TABNAME   = 'MARA'.
  W_FCAT-REF_FIELDNAME = 'MTART'.
  APPEND W_FCAT TO T_FCAT.
  CLEAR W_FCAT.

  W_FCAT-COL_POS       = '3'.
  W_FCAT-FIELDNAME     = 'MBRSH'.
  W_FCAT-TABNAME       = 'IT_MARA'.
  W_FCAT-REF_TABNAME   = 'MARA'.
  W_FCAT-REF_FIELDNAME = 'MBRSH'.
  APPEND W_FCAT TO T_FCAT.
  CLEAR W_FCAT.

  W_FCAT-COL_POS       = '4'.
  W_FCAT-FIELDNAME     = 'MATKL'.
  W_FCAT-TABNAME       = 'IT_MARA'.
  W_FCAT-REF_TABNAME   = 'MARA'.
  W_FCAT-REF_FIELDNAME = 'MATKL'.
  APPEND W_FCAT TO T_FCAT.
  CLEAR W_FCAT.

  W_FCAT-COL_POS       = '5'.
  W_FCAT-FIELDNAME     = 'MEINS'.
  W_FCAT-TABNAME       = 'IT_MARA'.
  W_FCAT-REF_TABNAME   = 'MARA'.
  W_FCAT-REF_FIELDNAME = 'MEINS'.
  APPEND W_FCAT TO T_FCAT.
  CLEAR W_FCAT.

***build fcat for MAKT
  W_FCAT_MAKT-COL_POS       = '1'.
  W_FCAT_MAKT-FIELDNAME     = 'MATNR'.
  W_FCAT_MAKT-TABNAME       = 'IT_MAKT'.
  W_FCAT_MAKT-REF_TABNAME   = 'MAKT'.
  W_FCAT_MAKT-REF_FIELDNAME = 'MATNR'.
  APPEND W_FCAT_MAKT TO T_FCAT_MAKT.
  CLEAR W_FCAT_MAKT.
  W_FCAT_MAKT-COL_POS       = '2'.
  W_FCAT_MAKT-FIELDNAME     = 'SPRAS'.
  W_FCAT_MAKT-TABNAME       = 'IT_MAKT'.
  W_FCAT_MAKT-REF_TABNAME   = 'MAKT'.
  W_FCAT_MAKT-REF_FIELDNAME = 'SPRAS'.
  APPEND W_FCAT_MAKT TO T_FCAT_MAKT.
  CLEAR W_FCAT_MAKT.
  W_FCAT_MAKT-COL_POS       = '3'.
  W_FCAT_MAKT-FIELDNAME     = 'MAKTX'.
  W_FCAT_MAKT-TABNAME       = 'IT_MAKT'.
  W_FCAT_MAKT-REF_TABNAME   = 'MAKT'.
  W_FCAT_MAKT-REF_FIELDNAME = 'MAKTX'.
  APPEND W_FCAT_MAKT TO T_FCAT_MAKT.
  CLEAR W_FCAT_MAKT.
*  * init
  CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_INIT' "initialize Block List ALV
    EXPORTING
      I_CALLBACK_PROGRAM = SY-REPID.

  DATA S_EVENTS TYPE SLIS_T_EVENT.
  DATA S_LAYOUT TYPE SLIS_LAYOUT_ALV.
  S_LAYOUT-ZEBRA = 'X'.
  CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_APPEND' "append ALV lists
    EXPORTING
      IS_LAYOUT   = S_LAYOUT "set layout
      IT_FIELDCAT = T_FCAT "set field catalog
      I_TABNAME   = 'IT_MARA' "table
      IT_EVENTS   = S_EVENTS "events
*     IT_SORT     =
*     I_TEXT      = ' '
    TABLES
      T_OUTTAB    = IT_MARA "out put table
*   EXCEPTIONS
*     PROGRAM_ERROR                    = 1
*     MAXIMUM_OF_APPENDS_REACHED       = 2
*     OTHERS      = 3
    .
  IF SY-SUBRC <> 0.
* Implement suitable error handling here
  ENDIF.

  CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_APPEND' "append ALV lists
    EXPORTING
      IS_LAYOUT   = S_LAYOUT "set layout
      IT_FIELDCAT = T_FCAT_MAKT "set field catalog
      I_TABNAME   = 'IT_MAKT' "table
      IT_EVENTS   = S_EVENTS "events
*     IT_SORT     =
*     I_TEXT      = ' '
    TABLES
      T_OUTTAB    = IT_MAKT "out put table
*   EXCEPTIONS
*     PROGRAM_ERROR                    = 1
*     MAXIMUM_OF_APPENDS_REACHED       = 2
*     OTHERS      = 3
    .
  IF SY-SUBRC <> 0.
* Implement suitable error handling here
  ENDIF.
  CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_DISPLAY' . "display blocked list

Tags:

Sql Example