0% found this document useful (0 votes)
79 views

What Is ALV Programming in ABAP? When Is This Grid Used in ABAP?

This document provides an example of using ALV (Application List Viewer) grid programming in ABAP. It describes the common ALV functions used to display and format output data in a grid. These include functions to define the field catalog, sorting, layout, and events. The example code shows how to call the REUSE_ALV_GRID_DISPLAY function module to display transactional data in an ALV grid, including setting properties for columns, sorting, totals, and layout.

Uploaded by

anjali Anju
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views

What Is ALV Programming in ABAP? When Is This Grid Used in ABAP?

This document provides an example of using ALV (Application List Viewer) grid programming in ABAP. It describes the common ALV functions used to display and format output data in a grid. These include functions to define the field catalog, sorting, layout, and events. The example code shows how to call the REUSE_ALV_GRID_DISPLAY function module to display transactional data in an ALV grid, including setting properties for columns, sorting, totals, and layout.

Uploaded by

anjali Anju
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 12

What is ALV Programming?

What is ALV programming in ABAP? When is this grid used in ABAP? 

ALV is Application List viewer.

Sap provides a set of ALV (ABAP LIST VIEWER) function modules which can be put
into use to embellish the output of a report. This set of ALV functions is used to enhance
the readability and functionality of any report output. Cases arise in sap when the output
of a report contains columns extending more than 255 characters in length. 

In such cases, this set of ALV functions can help choose selected columns and arrange
the different columns from a report output and also save different variants for report
display. This is a very efficient tool for dynamically sorting and arranging the columns
from a report output. 

The report output can contain up to 90 columns in the display with the wide array of
display options.

The commonly used ALV functions used for this purpose are;

1. REUSE_ALV_VARIANT_DEFAULT_GET
2. REUSE_ALV_VARIANT_F4
3. REUSE_ALV_VARIANT_EXISTENCE
4. REUSE_ALV_EVENTS_GET
5. REUSE_ALV_COMMENTARY_WRITE
6. REUSE_ALV_FIELDCATALOG_MERGE
7. REUSE_ALV_LIST_DISPLAY
8. REUSE_ALV_GRID_DISPLAY
9. REUSE_ALV_POPUP_TO_SELECT

Purpose of the above Functions are differ not all the functions are required in all the ALV
Report. 

But either no.7 or No.8 is there in the Program. 

How you call this function in your report?

After completion of all the data fetching from the database and append this data into an
Internal Table. say I_ITAB.

Then use follwing function module.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'


       EXPORTING
            I_CALLBACK_PROGRAM       = 'Prog.name'
            I_STRUCTURE_NAME         = 'I_ITAB'
            I_DEFAULT                = 'X'
            I_SAVE                   = 'A'
       TABLES
            T_OUTTAB                 = I_ITAB.
  IF SY-SUBRC <> 0.
    WRITE: 'SY-SUBRC: ', SY-SUBRC .
  ENDIF.
ENDFORM.                    " GET_FINAL_DATA

 
ABAP Example Program ALV Grid Control
You need to create a screen 100 for calling it, and in the Element list of the sceen supply
OK_CODE of type OK & in the layout, place a Custom - control with name
DILEEP_TEST1.

Then activate modules STATUS_0100 and USER_COMMAND_0100 in the flow logic .

REPORT ZTEST_DIL4 .
TABLES ZMSTKSUM.
DATA : OK_CODE LIKE SY-UCOMM,
       TAB_DISPLAY TYPE TABLE OF ZMSTKSUM,
       C_CONTAINER TYPE SCRFNAME VALUE 'DILEEP_TEST1',
       ALV_GRID TYPE REF TO CL_GUI_ALV_GRID,
       C_CUSTOM_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER.

SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.

SELECT-OPTIONS : S_WERKS FOR ZMSTKSUM-WERKS.

SELECTION-SCREEN END OF BLOCK B1.

SELECT * FROM ZMSTKSUM INTO TABLE TAB_DISPLAY WHERE WERKS IN S_WERKS.

CALL SCREEN 100.


*&---------------------------------------------------------------------

*&      Module  STATUS_0100  OUTPUT


*&---------------------------------------------------------------------

*       text
*----------------------------------------------------------------------

MODULE STATUS_0100 OUTPUT.


  SET PF-STATUS 'MAIN'.
*  SET TITLEBAR 'xxx'.
  IF C_CUSTOM_CONTAINER IS INITIAL.
CREATE OBJECT C_CUSTOM_CONTAINER EXPORTING CONTAINER_NAME = C_CONTAINER
.
    CREATE OBJECT ALV_GRID EXPORTING I_PARENT = C_CUSTOM_CONTAINER.

    CALL METHOD ALV_GRID->SET_TABLE_FOR_FIRST_DISPLAY


      EXPORTING
*    I_BUFFER_ACTIVE               =
*    I_BYPASSING_BUFFER            =
        I_STRUCTURE_NAME              = 'ZMSTKSUM'
*    IS_VARIANT                    =
*    I_SAVE                        =
*    I_DEFAULT                     = 'X'
*    IS_LAYOUT                     =
*    IS_PRINT                      =
*    IT_SPECIAL_GROUPS             =
*    IT_TOOLBAR_EXCLUDING          =
      CHANGING
        IT_OUTTAB                     = TAB_DISPLAY
*    IT_FIELDCATALOG               =
*    IT_SORT                       =
*    IT_FILTER                     =
*  EXCEPTIONS
*    INVALID_PARAMETER_COMBINATION = 1
*    PROGRAM_ERROR                 = 2
*    others                        = 3
        .
    IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.

  ENDIF.
ENDMODULE.                 " STATUS_0100  OUTPUT

*&---------------------------------------------------------------------

*&      Module  USER_COMMAND_0100  INPUT


*&---------------------------------------------------------------------

*       text
*----------------------------------------------------------------------

MODULE USER_COMMAND_0100 INPUT.


  CASE OK_CODE.
    WHEN 'EXIT'.
      LEAVE PROGRAM.

  ENDCASE.
ENDMODULE.                 " USER_COMMAND_0100  INPUT
A Simple ABAP ALV LIST VIEWER Example

This ALV program have all the basic report requirements such as page heading, page no,
sub-total and a grand total.

* This is a basic ALV with the followings:-


* - Page Heading
* - Page No
* - Sub-Total
* - Grand Total

REPORT ZALV.

TYPE-POOLS: SLIS.

DATA: G_REPID LIKE SY-REPID,


GS_PRINT            TYPE SLIS_PRINT_ALV,
GT_LIST_TOP_OF_PAGE TYPE SLIS_T_LISTHEADER,
GT_EVENTS           TYPE SLIS_T_EVENT,
GT_SORT             TYPE SLIS_T_SORTINFO_ALV,
GS_LAYOUT           TYPE SLIS_LAYOUT_ALV,
GT_FIELDCAT         TYPE SLIS_T_FIELDCAT_ALV,
FIELDCAT_LN LIKE LINE OF GT_FIELDCAT,
COL_POS TYPE I.

DATA: BEGIN OF ITAB,


  FIELD1(5) TYPE C,
  FIELD2(5) TYPE C,
  FIELD3(5) TYPE P DECIMALS 2,
END OF ITAB.

DATA: BEGIN OF ITAB1 OCCURS 0.


  INCLUDE STRUCTURE ITAB.
DATA: END OF ITAB1.

DATA: BEGIN OF ITAB_FIELDCAT OCCURS 0.


  INCLUDE STRUCTURE ITAB.
DATA: END OF ITAB_FIELDCAT.
* Print Parameters
PARAMETERS:
            P_PRINT  AS CHECKBOX DEFAULT ' ', "PRINT IMMEDIATE
            P_NOSINF AS CHECKBOX DEFAULT 'X', "NO SELECTION INFO
            P_NOCOVE AS CHECKBOX DEFAULT ' ', "NO COVER PAGE
            P_NONEWP AS CHECKBOX DEFAULT ' ', "NO NEW PAGE
            P_NOLINF AS CHECKBOX DEFAULT 'X', "NO PRINT LIST INFO
            P_RESERV TYPE I.                  "NO OF FOOTER LINE

INITIALIZATION.
G_REPID = SY-REPID.
PERFORM PRINT_BUILD    USING GS_PRINT.      "Print PARAMETERS

START-OF-SELECTION.
* TEST DATA
MOVE 'TEST1' TO ITAB1-FIELD1.
MOVE 'TEST1' TO ITAB1-FIELD2.
MOVE '10.00' TO ITAB1-FIELD3.
APPEND ITAB1.

MOVE 'TEST2' TO ITAB1-FIELD1.


MOVE 'TEST2' TO ITAB1-FIELD2.
MOVE '20.00' TO ITAB1-FIELD3.
APPEND ITAB1.

DO 50 TIMES.
  APPEND ITAB1.
ENDDO.

END-OF-SELECTION.

PERFORM BUILD.
PERFORM EVENTTAB_BUILD CHANGING GT_EVENTS.
PERFORM COMMENT_BUILD  CHANGING GT_LIST_TOP_OF_PAGE.
PERFORM CALL_ALV.

FORM BUILD.
* DATA FIELD CATALOG
* Explain Field Description to ALV
DATA: FIELDCAT_IN TYPE SLIS_FIELDCAT_ALV.

CLEAR FIELDCAT_IN.
FIELDCAT_LN-FIELDNAME = 'FIELD1'.
FIELDCAT_LN-TABNAME   = 'ITAB1'.
*FIELDCAT_LN-NO_OUT    = 'X'.  "FIELD NOT DISPLAY, CHOOSE FROM
LAYOUT
FIELDCAT_LN-KEY       = ' '.   "SUBTOTAL KEY
FIELDCAT_LN-NO_OUT    = ' '.
FIELDCAT_LN-SELTEXT_L = 'HEAD1'.
APPEND FIELDCAT_LN TO GT_FIELDCAT.

CLEAR FIELDCAT_IN.
FIELDCAT_LN-FIELDNAME = 'FIELD2'.
FIELDCAT_LN-TABNAME   = 'ITAB1'.
FIELDCAT_LN-NO_OUT    = 'X'.
FIELDCAT_LN-SELTEXT_L = 'HEAD2'.
APPEND FIELDCAT_LN TO GT_FIELDCAT.

CLEAR FIELDCAT_IN.
FIELDCAT_LN-FIELDNAME     = 'FIELD3'.
FIELDCAT_LN-TABNAME       = 'ITAB1'.
FIELDCAT_LN-REF_FIELDNAME = 'MENGE'. "<- REF FIELD IN THE
DICTIONNARY
FIELDCAT_LN-REF_TABNAME   = 'MSEG'.  "<- REF TABLE IN THE
DICTIONNARY
FIELDCAT_LN-NO_OUT        = ' '.
FIELDCAT_LN-DO_SUM        = 'X'.   "SUM UPON DISPLAY
APPEND FIELDCAT_LN TO GT_FIELDCAT.

* DATA SORTING AND SUBTOTAL


DATA: GS_SORT TYPE SLIS_SORTINFO_ALV.

CLEAR GS_SORT.
GS_SORT-FIELDNAME = 'FIELD1'.
GS_SORT-SPOS      = 1.
GS_SORT-UP        = 'X'.
GS_SORT-SUBTOT    = 'X'.
APPEND GS_SORT TO GT_SORT.

CLEAR GS_SORT.
GS_SORT-FIELDNAME = 'FIELD2'.
GS_SORT-SPOS      = 2.
GS_SORT-UP        = 'X'.
*GS_SORT-SUBTOT    = 'X'.
APPEND GS_SORT TO GT_SORT.

ENDFORM.

FORM CALL_ALV.
* ABAP List Viewer
CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING
* I_INTERFACE_CHECK = ' '
* I_BYPASSING_BUFFER =
* I_BUFFER_ACTIVE = ' '
I_CALLBACK_PROGRAM = G_REPID
* I_CALLBACK_PF_STATUS_SET = ' '
* I_CALLBACK_USER_COMMAND = ' '
I_STRUCTURE_NAME = 'ITAB1'
IS_LAYOUT =  GS_LAYOUT
IT_FIELDCAT = GT_FIELDCAT[]
* IT_EXCLUDING =
* IT_SPECIAL_GROUPS =
  IT_SORT = GT_SORT[]
* IT_FILTER =
* IS_SEL_HIDE =
* I_DEFAULT = 'X'
* I_SAVE = ' '
* IS_VARIANT =
  IT_EVENTS = GT_EVENTS[]
* IT_EVENT_EXIT =
  IS_PRINT = GS_PRINT
* IS_REPREP_ID =
* I_SCREEN_START_COLUMN = 0
* I_SCREEN_START_LINE = 0
* I_SCREEN_END_COLUMN = 0
* I_SCREEN_END_LINE = 0
* IMPORTING
* E_EXIT_CAUSED_BY_CALLER =
* ES_EXIT_CAUSED_BY_USER =
TABLES
T_OUTTAB = ITAB1
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2.
ENDFORM.

* HEADER FORM
FORM EVENTTAB_BUILD CHANGING LT_EVENTS TYPE SLIS_T_EVENT.
CONSTANTS:
GC_FORMNAME_TOP_OF_PAGE TYPE SLIS_FORMNAME VALUE
'TOP_OF_PAGE'.
*GC_FORMNAME_END_OF_PAGE TYPE SLIS_FORMNAME VALUE
'END_OF_PAGE'.

  DATA: LS_EVENT TYPE SLIS_ALV_EVENT.


  CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
       EXPORTING
            I_LIST_TYPE = 0
       IMPORTING
            ET_EVENTS   = LT_EVENTS.

  READ TABLE LT_EVENTS WITH KEY NAME =  SLIS_EV_TOP_OF_PAGE


                           INTO LS_EVENT.
  IF SY-SUBRC = 0.
    MOVE GC_FORMNAME_TOP_OF_PAGE TO LS_EVENT-FORM.
    APPEND LS_EVENT TO LT_EVENTS.
  ENDIF.

* define END_OF_PAGE event


* READ TABLE LT_EVENTS WITH KEY NAME =  SLIS_EV_END_OF_PAGE
*                          INTO LS_EVENT.
* IF SY-SUBRC = 0.
*   MOVE GC_FORMNAME_END_OF_PAGE TO LS_EVENT-FORM.
*   APPEND LS_EVENT TO LT_EVENTS.
* ENDIF.
ENDFORM.

FORM COMMENT_BUILD CHANGING GT_TOP_OF_PAGE TYPE


SLIS_T_LISTHEADER.
  DATA: GS_LINE TYPE SLIS_LISTHEADER.

  CLEAR GS_LINE.
  GS_LINE-TYP  = 'H'.
  GS_LINE-INFO = 'HEADER 1'.
  APPEND GS_LINE TO GT_TOP_OF_PAGE.

  CLEAR GS_LINE.
  GS_LINE-TYP  = 'S'.
  GS_LINE-KEY  = 'STATUS 1'.
  GS_LINE-INFO = 'INFO 1'.
  APPEND GS_LINE TO GT_TOP_OF_PAGE.
  GS_LINE-KEY  = 'STATUS 2'.
  GS_LINE-INFO = 'INFO 2'.
  APPEND GS_LINE TO GT_TOP_OF_PAGE.

* CLEAR GS_LINE.
* GS_LINE-TYP  = 'A'.
*
* GS_LINE-INFO = 'ACTION'.
* APPEND GS_LINE TO  GT_TOP_OF_PAGE.
ENDFORM.

FORM TOP_OF_PAGE.
  CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
       EXPORTING
            IT_LIST_COMMENTARY = GT_LIST_TOP_OF_PAGE.
  WRITE: SY-DATUM, 'Page No', SY-PAGNO LEFT-JUSTIFIED.
ENDFORM.

FORM END_OF_PAGE.
  WRITE at (sy-linsz) sy-pagno CENTERED.
ENDFORM.

* PRINT SETTINGS
FORM PRINT_BUILD USING LS_PRINT TYPE SLIS_PRINT_ALV.
  LS_PRINT-PRINT              = P_PRINT.  "PRINT IMMEDIATE
  LS_PRINT-NO_PRINT_SELINFOS  = P_NOSINF. "NO SELECTION INFO
  LS_PRINT-NO_COVERPAGE       = P_NOCOVE. "NO COVER PAGE
  LS_PRINT-NO_NEW_PAGE        = P_NONEWP.
  LS_PRINT-NO_PRINT_LISTINFOS = P_NOLINF. "NO PRINT LIST INFO
  LS_PRINT-RESERVE_LINES      = P_RESERV.
ENDFORM.

*END OF ZALV PROGRAM

REPORT Z_ALV_SIMPLE_EXAMPLE_WITH_ITAB .
************************************************************************
*Simple example to use ALV and to define the ALV data in an internal
*table
************************************************************************
* Martin Schlegel, BearingPoint, December 2004
*
* Thanks to Madhusudhan Sonee and Rama Krishna Kommineni for testing
* and feedback
*
************************************************************************
************************************************************************
*For a very long time, people gave me the feeling that ALV is a
*complicated tool that is difficult to understand and to use.
*Lately I had to use it and I discovered that ALV is easy to use and
*saves a lot of work:
*ALV will generate the column headings on its own, so one does not need
*to work on headlines and transalation.
*ALV allows the user to select the columns he wants to see, so the user
*does not need to contact a developer for every change he likes to have.
*ALV allows the user to create his own sums, so …
*ALV has a simple way to work with internal tables.
*If you really want to save time, use ALV instead of write!
************************************************************************
*
*Please take 30 minutes to explore the following example and see how
*easy it is to use ALV!
*
************************************************************************

*data definition

tables:
marav. "Table MARA and table MAKT

*---------------------------------------------------------------------*
* Data to be displayed in ALV
* Using the following syntax, REUSE_ALV_FIELDCATALOG_MERGE can auto-
* matically determine the fieldstructure from this source program
Data:
begin of imat occurs 100,
matnr like marav-matnr, "Material number
maktx like marav-maktx, "Material short text
matkl like marav-matkl, "Material group (so you can test to make
" intermediate sums)
ntgew like marav-ntgew, "Net weight, numeric field (so you can test to
"make sums)
gewei like marav-gewei, "weight unit (just to be complete)
end of imat.

*---------------------------------------------------------------------*
* Other data needed
* field to store report name
data i_repid like sy-repid.
* field to check table length
data i_lines like sy-tabix.

*---------------------------------------------------------------------*
* Data for ALV display
TYPE-POOLS: SLIS.
data int_fcat type SLIS_T_FIELDCAT_ALV.

*---------------------------------------------------------------------*
select-options:
s_matnr for marav-matnr matchcode object MAT1.

*---------------------------------------------------------------------*
start-of-selection.

* read data into table imat


select * from marav
into corresponding fields of table imat
where
matnr in s_matnr.

* Check if material was found


clear i_lines.
describe table imat lines i_lines.
if i_lines lt 1.
* Using hardcoded write here for easy upload
write: /
'No materials found.'.
exit.
endif.

end-of-selection.
*---------------------------------------------------------------------*
*
* Now, we start with ALV
*
*---------------------------------------------------------------------*
*
*
* To use ALV, we need a DDIC-structure or a thing called Fieldcatalogue.
* The fieldcatalouge can be generated by FUNCTION
* 'REUSE_ALV_FIELDCATALOG_MERGE' from an internal table from any
* report source, including this report.
* The only problem one might have is that the report and table names
* need to be in capital letters. (I had it :-( )
*
*
*---------------------------------------------------------------------*

* Store report name


i_repid = sy-repid.

* Create Fieldcatalogue from internal table


CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
I_PROGRAM_NAME = i_repid
I_INTERNAL_TABNAME = 'IMAT' "capital letters!
I_INCLNAME = i_repid
CHANGING
CT_FIELDCAT = int_fcat
EXCEPTIONS
INCONSISTENT_INTERFACE = 1
PROGRAM_ERROR = 2
OTHERS = 3.
*explanations:
* I_PROGRAM_NAME is the program which calls this function
*
* I_INTERNAL_TABNAME is the name of the internal table which you want
* to display in ALV
*
* I_INCLNAME is the ABAP-source where the internal table is defined
* (DATA....)
* CT_FIELDCAT contains the Fieldcatalouge that we need later for
* ALV display

IF SY-SUBRC <> 0.
write: /
'Returncode',
sy-subrc,
'from FUNCTION REUSE_ALV_FIELDCATALOG_MERGE'.
ENDIF.
*This was the fieldcatlogue
*---------------------------------------------------------------------*
*
* And now, we are ready to display our list

* Call for ALV list display


CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING
* I_CALLBACK_PROGRAM = 'Z_ALV_SIMPLE_EXAMPLE_WITH_ITAB'
I_CALLBACK_PROGRAM = i_repid
IT_FIELDCAT = int_fcat
I_SAVE = 'A'
TABLES
T_OUTTAB = imat
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2.
*
*explanations:
* I_CALLBACK_PROGRAM is the program which calls this function
*
* IT_FIELDCAT (just made by REUSE_ALV_FIELDCATALOG_MERGE) contains
* now the data definition needed for display
*
* I_SAVE allows the user to save his own layouts
*
* T_OUTTAB contains the data to be displayed in ALV

IF SY-SUBRC <> 0.
write: /
'Returncode',
sy-subrc,
'from FUNCTION REUSE_ALV_LIST_DISPLAY'.
ENDIF.
*

You might also like