Variants in ALV Reporting

Variants are selection screen values that are stored in ALVs.  The variants are used to select separate layout.

We can set default varients using initialization event and in select-options, parameters statements. 

Look at this sample code:

parameters: example like saptab-field default 'ABCDEF'.
In this case, you can see an input field with name example have ABCDEF filled in.

select-options: example like saptab-field default 'LOW' to 'HIGH'.
In this case, you can see an input field with name example having low value LOW and high value HIGH.

INITIALIZATION.
      airp_fr-sign = 'I'.
      airp_fr-option = 'EQ'.
      airp_fr-low = 'JFK'.
  APPEND airp_fr.
      airp_to-sign = 'I'.
      airp_to-option = 'EQ'.
      airp_to-low = 'FRA'.
  APPEND airp_to.
      carrid-sign   = 'I'.
      carrid-option = 'BT'.
      carrid-low    = 'AA'.
      carrid-high   = 'LH'.
  APPEND carrid.

Here you are assigning respective fields with default values as show above.

Also

Check in the below program:

data: alv_variant like disvarint

AT SELECTION-SCREEN ON VALUE-REQUEST FOR alv_def.

and check perform alv_f4.

*&---------------------------------------------------------------------*
*& Report  YANBUTEST_INFO                                              *
*&                                                                     *
*&---------------------------------------------------------------------*
*&                                                                     *
*&                                                                     *
*&---------------------------------------------------------------------*

REPORT  yanbutest_info                          .


************************************************************************
*TABLE DECLARATION
************************************************************************
TYPE-POOLS: slis.
TABLES: t001l,t024.

************************************************************************
* TYPES DECLARATION
************************************************************************
TYPES: BEGIN OF ty_marc,
       matnr TYPE mara-matnr,
       werks TYPE marc-werks,
       ekgrp TYPE marc-ekgrp,
       plifz TYPE marc-plifz,
       beskz TYPE marc-beskz,
       sobsl TYPE marc-sobsl,
       END OF ty_marc,
       BEGIN OF ty_eina,
       infnr TYPE eina-infnr,
       matnr TYPE eina-matnr,
       lifnr TYPE eina-lifnr,
       END OF ty_eina,
       BEGIN OF ty_eine,
       infnr TYPE eine-infnr,
       werks TYPE eine-werks,
       aplfz TYPE eine-aplfz,
       END OF ty_eine,
       BEGIN OF ty_final,
       slno TYPE sy-cucol,
       matnr TYPE mara-matnr,
       beskz TYPE marc-beskz,
       sobsl TYPE marc-sobsl,
       plifz TYPE marc-plifz,
       infnr TYPE eina-infnr,
       lifnr TYPE eina-lifnr,
       aplfz TYPE eine-aplfz,
       ekgrp TYPE marc-ekgrp,
       END OF ty_final.

************************************************************************
* INTERNAL TABLE DECLARATION
************************************************************************

DATA: wa_marc TYPE ty_marc,
      it_marc TYPE TABLE OF ty_marc,
      wa_eina TYPE ty_eina,
      it_eina TYPE TABLE OF ty_eina,
      it_eine TYPE TABLE OF ty_eine,
      wa_eine TYPE ty_eine,
      it_final TYPE TABLE OF ty_final,
      wa_final TYPE ty_final.

************************************************************************
* DATA DECLARATION
************************************************************************
DATA: alv_variant TYPE disvariant,
      gt_fieldcat TYPE slis_t_fieldcat_alv,
      i_header TYPE slis_t_listheader.

SELECTION-SCREEN : BEGIN OF BLOCK blk1 WITH FRAME.
PARAMETER     : p_werks TYPE t001l-werks OBLIGATORY DEFAULT 'BLR1'.
SELECT-OPTIONS: s_matnr FOR wa_marc-matnr,
                s_ekgrp FOR t024-ekgrp.
SELECTION-SCREEN : END OF BLOCK blk1.

SELECTION-SCREEN: BEGIN OF BLOCK blk5 WITH FRAME.
PARAMETERS: alv_def TYPE disvariant-variant.
SELECTION-SCREEN: END OF BLOCK blk5.

************************************************************************
* SELECTION-SCREEN
************************************************************************

AT SELECTION-SCREEN ON VALUE-REQUEST FOR alv_def.
  PERFORM alv_f4.

START-OF-SELECTION.

  PERFORM fetch_data.
  PERFORM format_data.
  PERFORM build_fieldcatlog.
  PERFORM alv_header USING i_header.
  PERFORM display_data.


*  loop at it_final into wa_final.
*  write:/ wa_final-matnr,
*       wa_final-BESKZ,
*       wa_final-SOBSL,
*       wa_final-PLIFZ,
*       wa_final-INFNR,
*       wa_final-LIFNR,
*       wa_final-APLFZ,
*       wa_final-ekgrp.
*  endloop.
*&---------------------------------------------------------------------*
*&      Form  alv_f4
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM alv_f4 .
  alv_variant-report = sy-repid.
  CALL FUNCTION 'REUSE_ALV_VARIANT_F4'
    EXPORTING
      is_variant = alv_variant
      i_save     = 'A'
    IMPORTING
      es_variant = alv_variant
    EXCEPTIONS
      not_found  = 2.
  IF sy-subrc = 2.
    MESSAGE ID sy-msgid TYPE 'I' NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ELSE.
    alv_def = alv_variant-variant.
  ENDIF.
ENDFORM.                                                    " alv_f4

*&---------------------------------------------------------------------*
*&      Form  fetch_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM fetch_data .

  SELECT matnr werks ekgrp plifz beskz sobsl
         FROM marc
         INTO TABLE it_marc
         WHERE matnr IN s_matnr
           AND werks = p_werks
           AND ekgrp IN s_ekgrp.

  IF NOT it_marc[] IS INITIAL.
    SELECT infnr matnr lifnr
           FROM eina
           INTO TABLE it_eina
           FOR ALL ENTRIES IN it_marc
           WHERE matnr = it_marc-matnr.

    IF NOT it_eina[] IS INITIAL.
      SELECT infnr werks aplfz
             FROM eine
             INTO TABLE it_eine
             FOR ALL ENTRIES IN it_eina
             WHERE infnr = it_eina-infnr
               AND werks = p_werks.
    ENDIF.
  ENDIF.
ENDFORM.                    " fetch_data

*&---------------------------------------------------------------------*
*&      Form  format_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM format_data .

  LOOP AT it_marc INTO wa_marc.

    LOOP AT it_eina INTO wa_eina WHERE matnr = wa_marc-matnr.
      READ TABLE it_eine INTO wa_eine WITH KEY infnr = wa_eina-infnr.

      wa_final-slno = wa_final-slno + 1.
      wa_final-matnr = wa_marc-matnr.
      wa_final-beskz = wa_marc-beskz.
      wa_final-sobsl = wa_marc-sobsl.
      wa_final-aplfz = wa_eine-aplfz.
      wa_final-infnr = wa_eina-infnr.
      wa_final-lifnr = wa_eina-lifnr.
      wa_final-plifz = wa_marc-plifz.
      wa_final-ekgrp = wa_marc-ekgrp.
      APPEND wa_final TO it_final.
      CLEAR: wa_eina,wa_eine.

    ENDLOOP.
    CLEAR wa_marc.
  ENDLOOP.
ENDFORM.                    " format_data

*&---------------------------------------------------------------------*
*&      Form  build_fieldcatlog
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM build_fieldcatlog .
  DATA: fieldcat_ln TYPE slis_fieldcat_alv,
          w_pos TYPE sy-cucol.

  CLEAR fieldcat_ln.
  w_pos = w_pos + 1.
  fieldcat_ln-col_pos       = w_pos.
  fieldcat_ln-fieldname     = 'SLNO'.
  fieldcat_ln-seltext_l     = 'Serial Number'.
  fieldcat_ln-seltext_m     = 'SlNo'.
  fieldcat_ln-tabname       = 'it_final'.
  fieldcat_ln-outputlen     = 5.
  fieldcat_ln-no_zero       = 'X'.
  APPEND fieldcat_ln TO gt_fieldcat.

  CLEAR fieldcat_ln.
  w_pos = w_pos + 1.
  fieldcat_ln-col_pos       = w_pos.
  fieldcat_ln-fieldname     = 'MATNR'.
  fieldcat_ln-seltext_l     = 'Material'.
  fieldcat_ln-seltext_m     = 'Material'.
  fieldcat_ln-tabname       = 'it_final'.
  fieldcat_ln-outputlen     = 18.
  fieldcat_ln-color = 'c111'.
  APPEND fieldcat_ln TO gt_fieldcat.

  CLEAR fieldcat_ln.
  w_pos = w_pos + 1.
  fieldcat_ln-col_pos       = w_pos.
  fieldcat_ln-fieldname     = 'BESKZ'.
  fieldcat_ln-seltext_l     = 'P-TY'.
  fieldcat_ln-seltext_m     = 'P-TY'.
  fieldcat_ln-tabname       = 'it_final'.
  fieldcat_ln-outputlen     = 4.
  APPEND fieldcat_ln TO gt_fieldcat.

  CLEAR fieldcat_ln.
  w_pos = w_pos + 1.
  fieldcat_ln-col_pos       = w_pos.
  fieldcat_ln-fieldname     = 'SOBSL'.
  fieldcat_ln-seltext_l     = 'S-TY'.
  fieldcat_ln-seltext_m     = 'S-TY'.
  fieldcat_ln-tabname       = 'it_final'.
  fieldcat_ln-outputlen     = 4.
  APPEND fieldcat_ln TO gt_fieldcat.

  CLEAR fieldcat_ln.
  w_pos = w_pos + 1.
  fieldcat_ln-col_pos       = w_pos.
  fieldcat_ln-fieldname     = 'PLIFZ'.
*  fieldcat_ln-seltext_l     = 'Planned delivery time'.
  fieldcat_ln-seltext_m     = 'Matl PDT'.
  fieldcat_ln-tabname       = 'it_final'.
  fieldcat_ln-outputlen     = 8.
  APPEND fieldcat_ln TO gt_fieldcat.

  CLEAR fieldcat_ln.
  w_pos = w_pos + 1.
  fieldcat_ln-col_pos       = w_pos.
  fieldcat_ln-fieldname     = 'INFNR'.
  fieldcat_ln-seltext_l     = 'Info Record'.
  fieldcat_ln-seltext_m     = 'Info Record'.
  fieldcat_ln-tabname       = 'it_final'.
  fieldcat_ln-outputlen     = 12.
  APPEND fieldcat_ln TO gt_fieldcat.

  CLEAR fieldcat_ln.
  w_pos = w_pos + 1.
  fieldcat_ln-col_pos       = w_pos.
  fieldcat_ln-fieldname     = 'LIFNR'.
  fieldcat_ln-seltext_l     = 'Vendor'.
  fieldcat_ln-seltext_m     = 'Vendor'.
  fieldcat_ln-tabname       = 'it_final'.
  fieldcat_ln-outputlen     = 10.
  APPEND fieldcat_ln TO gt_fieldcat.

  CLEAR fieldcat_ln.
  w_pos = w_pos + 1.
  fieldcat_ln-col_pos       = w_pos.
  fieldcat_ln-fieldname     = 'APLFZ'.
  fieldcat_ln-seltext_l     = 'Info rcd PDT'.
  fieldcat_ln-seltext_m     = 'Info rcd PDT'.
  fieldcat_ln-tabname       = 'it_final'.
  fieldcat_ln-outputlen     = 12.
  APPEND fieldcat_ln TO gt_fieldcat.

  CLEAR fieldcat_ln.
  w_pos = w_pos + 1.
  fieldcat_ln-col_pos       = w_pos.
  fieldcat_ln-fieldname     = 'EKGRP'.
  fieldcat_ln-seltext_l     = 'Pur Grp'.
  fieldcat_ln-seltext_m     = 'Pur Grp'.
  fieldcat_ln-tabname       = 'it_final'.
  fieldcat_ln-outputlen     = 7.
  APPEND fieldcat_ln TO gt_fieldcat.

ENDFORM.                    " build_fieldcatlog

*&--------------------------------------------------------------------*
*&      Form  ALV_HEADER
*&--------------------------------------------------------------------*
*       text
*---------------------------------------------------------------------*
*      -->I_HEADER   text
*---------------------------------------------------------------------*

FORM alv_header USING i_header TYPE slis_t_listheader.
  DATA: w_space(10) VALUE '.        .',w_date(16),w_date1(10).
  DATA: wa_line TYPE slis_listheader.
  CLEAR wa_line.
  wa_line-typ = 'H'.
  wa_line-info = 'Planned Delivery Time'.
  APPEND wa_line TO i_header.

*To print plant
*---------------
  CLEAR wa_line.
  wa_line-typ = 'S'.
  wa_line-key = 'Plant    :'.
  wa_line-info = p_werks.
 WRITE sy-datum USING EDIT MASK '__.__.____' TO w_date1.
  CONCATENATE wa_line-info '...' 'Date :' w_date1 INTO wa_line-info SEPARATED BY space.

  APPEND wa_line TO i_header.

*To print Material
*--------------------
  CLEAR wa_line.
  wa_line-typ = 'S'.
  wa_line-key = 'Material    :'.
  IF s_matnr-high IS INITIAL.
    wa_line-info = s_matnr-low.
  ELSE.
    CONCATENATE s_matnr-low 'to' s_matnr-high INTO wa_line-info SEPARATED BY space.
  ENDIF.
  WRITE sy-uzeit USING EDIT MASK '__:__:__' TO w_date1.
  CONCATENATE wa_line-info '...' 'Time :' w_date1 INTO wa_line-info SEPARATED BY space.
  APPEND wa_line TO i_header.

*To print Material
*--------------------
  CLEAR wa_line.
  wa_line-typ = 'S'.
  wa_line-key = 'Purchasing Group  :'.
  IF s_ekgrp-high IS INITIAL.
    wa_line-info = s_ekgrp-low.
  ELSE.
    CONCATENATE s_ekgrp-low 'to' s_ekgrp-high INTO wa_line-info SEPARATED BY space.
  ENDIF.
  APPEND wa_line TO i_header.


ENDFORM.                    "ALV_HEADER

*&---------------------------------------------------------------------*
*&      Form  top_of_page
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM top_of_page .
  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
    EXPORTING
      it_list_commentary = i_header.
ENDFORM.


*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM display_data .
  DATA: is_layout TYPE slis_layout_alv.
  is_layout-zebra = 'X'.


  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      i_callback_program       = sy-repid
      i_callback_top_of_page   = 'TOP_OF_PAGE'
      is_layout                = is_layout
      it_fieldcat              = gt_fieldcat
      is_variant               = alv_variant
      i_save                   = 'A'
*      it_events                = it_events[]
    TABLES
      t_outtab                 = it_final.
  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_data

ABAP

See Also:
Edit a Particular Column in an ALV Report

Tables
Database Table

ABAP Books List
ABAP/4 Certification, Programming, Smartforms, Sapscripts and Object Oriented Programming Books

Smart Forms
SAP Smartforms

ABAP Menu:
ABAP Example Hints and Tips

Return to Index:-
SAP ABAP/4 Programming, Basis Administration, Configuration Hints and Tips

(c) www.gotothings.com All material on this site is Copyright.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
All product names are trademarks of their respective companies.  The site www.gotothings.com is in no way affiliated with SAP AG.
Any unauthorised copying or mirroring is prohibited.