SAP - Simple Re-Usable ALV Grid Display Report Program - Reusable Asset

Here's a simple way of displaying data in ALV Grid Display.

First create a function module as shown below:







Now you can call this function module as shown below

SAMPLE REPORT PROGRAM:

REPORT ztest_simple_alv_report.
DATA: gt_mara TYPE TABLE OF mara,
            gwa_mara TYPE mara.

SELECT * FROM mara
UP TO 10 ROWS
 INTO TABLE gt_mara.

CALL FUNCTION 'ZDISPLAY_ALV'
  EXPORTING
* I_START_COLUMN = 25
* I_START_LINE = 6
* I_END_COLUMN = 120
* I_END_LINE = 20
* I_TITLE = 'ALV'
* I_POPUP = ' '
* i_pf_status = 'STANDARD'
 i_program = sy-repid
   TABLES
 it_alv = gt_mara.


Output Screen:

If you want to pass your own custom PF-STATUS, Modify the program as shown below:

Create your own custom PF-STATUS for the program and pass i to the function module as shown below. (In this case we created a PF-STATUS named 'STANDARD' for this sample program)

REPORT ztest_simple_alv_report.
DATA: gt_mara TYPE TABLE OF mara,
            gwa_mara TYPE mara.

SELECT * FROM mara
UP TO 10 ROWS
 INTO TABLE gt_mara.

CALL FUNCTION 'ZDISPLAY_ALV'
  EXPORTING
* I_START_COLUMN = 25
* I_START_LINE = 6
* I_END_COLUMN = 120
* I_END_LINE = 20
* I_TITLE = 'ALV'
* I_POPUP = ' '
 i_pf_status = 'STANDARD'
 i_program = sy-repid
   TABLES
 it_alv = gt_mara.

Output Screen: 

If there is a requirement to show ALV as POP Up Screen, Modify the code as shown below:

REPORT ztest_simple_alv_report.
DATA: gt_mara TYPE TABLE OF mara,
            gwa_mara TYPE mara.

SELECT * FROM mara
UP TO 10 ROWS
 INTO TABLE gt_mara.

CALL FUNCTION 'ZDISPLAY_ALV'
  EXPORTING
     I_START_COLUMN = 25
     I_START_LINE = 6
     I_END_COLUMN = 120
     I_END_LINE = 20
     I_TITLE = 'ALV'
     I_POPUP = 'X '
     I_PF_STATUS = 'STANDARD'
     I_PROGRAM = sy-repid
  TABLES
     IT_ALV = gt_mara.

Output Screen:


REFERENCE CODE FOR FUNCTION MODULE:


FUNCTION zdisplay_alv .
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     REFERENCE(I_START_COLUMN) TYPE  I DEFAULT 25
*"     REFERENCE(I_START_LINE) TYPE  I DEFAULT 6
*"     REFERENCE(I_END_COLUMN) TYPE  I DEFAULT 120
*"     REFERENCE(I_END_LINE) TYPE  I DEFAULT 20
*"     REFERENCE(I_TITLE) TYPE  STRING DEFAULT 'ALV'
*"     REFERENCE(I_POPUP) TYPE  FLAG DEFAULT ' '
*"     REFERENCE(I_PF_STATUS) TYPE  SYPFKEY DEFAULT ' '
*"     REFERENCE(I_PROGRAM) TYPE  SY-REPID
*"  TABLES
*"      IT_ALV TYPE  STANDARD TABLE
*"----------------------------------------------------------------------

  DATA: lv_alv       TYPE REF TO cl_salv_table,
        lv_functions TYPE REF TO cl_salv_functions_list,
        lv_columns   TYPE REF TO cl_salv_columns_table,
        lv_layout    TYPE REF TO cl_salv_layout,
        lv_key       TYPE salv_s_layout_key.

  TRY.
      cl_salv_table=>factory(
        IMPORTING
          r_salv_table = lv_alv
        CHANGING
          t_table      = it_alv[] ).

    CATCH cx_salv_msg.
  ENDTRY.

  lv_functions = lv_alv->get_functions( ).
  lv_functions->set_all( 'X' ).
  lv_columns = lv_alv->get_columns( ).
  lv_columns->set_optimize( ).
  lv_layout = lv_alv->get_layout( ).

  IF i_program IS NOT INITIAL.
    lv_key-report = i_program.
    lv_layout->set_key( lv_key ).
    lv_layout->set_save_restriction( if_salv_c_layout=>restrict_none ).
  ENDIF.

  IF i_pf_status IS NOT INITIAL.
    lv_alv->set_screen_status( report = i_program
                               pfstatus = i_pf_status
                               set_functions = 2 ).
  ENDIF.

  IF lv_alv IS BOUND.
    IF i_popup = 'X'.
      lv_alv->set_screen_popup(
        start_column = i_start_column
        end_column  = i_end_column
        start_line  = i_start_line
        end_line    = i_end_line ).
    ENDIF.
    lv_alv->display( ).
  ENDIF.

ENDFUNCTION.


*********************************************************************************
Using new features available in SAP after 7.40, one can use escape symbol (@) in open SQL as shown below:

(Note: There is no need to declare your internal table here)

SAMPLE CODE:

REPORT ztest_simple_alv_report.

SELECT matnr, mtart, matkl UP TO 10 ROWS
  FROM mara
  INTO TABLE @DATA(gt_mara).

CALL FUNCTION 'ZDISPLAY_ALV'
  EXPORTING
*   i_start_column = 25
*   i_start_line   = 6
*   i_end_column   = 120
*   i_end_line     = 20
*   i_title   = 'ALV'
*   i_popup   = 'X'
*   i_pf_status    = 'STANDARD'
    i_program = sy-repid
  TABLES
    it_alv    = gt_mara.

Output Screen: