Why are developers so fond of ‘REUSE_ALV_GRID_DISPLAY’?

Why are developers so fond of ‘REUSE_ALV_GRID_DISPLAY’

Is it that hard to NOT utilize FM ‘REUSE_ALV_GRID_DISPLAY’ to show ALV? Or on the other hand is it human instinct to remain in our usual range of familiarity? ABAPers who are dependent on this FM, have the code scrap prepared in their vault to reuse in each and every other report, so they don’t really focus to look on some other option.

SAP delivered the class ‘CL_SALV_TABLE’ over 10 years prior yet it couldn’t arrive at the personalities of the vast majority of those FM cherishing ABAPers. SAP has different classes to show the ALV however plant strategy is one of the least difficult to execute. Truth be told, I find manufacturing plant strategy takes even less lines of code than the famous ‘REUSE_ALV_GRID_DISPLAY’.

Web is overwhelmed with instructional exercises of how to involve this class ‘CL_SALV_TABLE’ for ALV show. So I would rather not add another instructional exercise. Here, I would just give the functioning code scrap which can be utilized in any task. Trust with this instant arrangement, some old fashioned ABAPers would take a dunk in this class strategy to show the ALV..

Pronounce a technique in your report class with the underneath code passing your last inward table ( for fledgling you can duplicate it into a subroutine and call it in your program).

Save this code scrap and begin reusing it with certainty..

Strategy display_alv_report .

DATA:
lr_alv TYPE REF TO cl_salv_table,
lr_columns TYPE REF TO cl_salv_columns_table,
lr_column TYPE REF TO cl_salv_column,
lr_functions TYPE REF TO cl_salv_functions_list,
lr_display TYPE REF TO cl_salv_display_settings,
lr_layout TYPE REF TO cl_salv_layout,
lr_sorts TYPE REF TO cl_salv_sorts,
ls_key TYPE salv_s_layout_key.

* Check to make sure the internal table has data.
IF lines( it_final ) > 0.

TRY.
* Create ALV instance
* This is the factory method we were talking about
CALL METHOD cl_salv_table=>factory
IMPORTING
r_salv_table = lr_alv
CHANGING
t_table = it_final.

* Get functions object
lr_functions = lr_alv->get_functions( ).

* Set all the functions
lr_functions->set_all( ).

* Get column object
lr_columns = lr_alv->get_columns( ).

* These are optional functions
* If you want to change the description of columns, call and set as shown below
* CALL METHOD lr_columns->get_column
* EXPORTING
* columnname = 'NETWR_PO'
* RECEIVING
* value = lr_column.

* Important to mention. If you want to give custom short text, then make
* Medium text and long text blank (important trick). Similarly applicable for med and long text overriding.
* CALL METHOD lr_column->set_short_text
* EXPORTING
* value = ' '.
* CALL METHOD lr_column->set_medium_text
* EXPORTING
* value = 'PO Srv Net Val'.
* CALL METHOD lr_column->set_long_text
* EXPORTING
* value = ' '.
*
* Optimize the columns
* lr_columns->set_optimize( ).

* Get sort column
* lr_sorts = lr_alv->get_sorts( ).
* lr_sorts->clear( ).

* Sort table
* lr_sorts->add_sort( columnname = 'AUFNR' ).

* Get Layout
lr_layout = lr_alv->get_layout( ).

ls_key-report = sy-repid.
* Set Layout
lr_layout->set_key( ls_key ).
lr_layout->set_default( ' ' ).
lr_layout->set_save_restriction( cl_salv_layout=>restrict_none ).

* Optional, if there is a layout from selection screen use it
* IF NOT p_layout IS INITIAL.
* lr_layout->set_initial_layout( p_layout ).
* ENDIF.

* Get Display setting
lr_display = lr_alv->get_display_settings( ).

lr_display->set_striped_pattern( cl_salv_display_settings=>true ).

* Display the ALV
lr_alv->display( ).

* Catch exceptions
CATCH cx_salv_msg.
WRITE: 'Error displaying grid CX_SALV_MSG!'.

CATCH cx_salv_not_found.
WRITE: 'Error displaying grid CX_SALV_NOT_FOUND!'.

CATCH cx_salv_data_error.
WRITE: 'Error displaying grid CX_SALV_DATA_ERROR!'.

CATCH cx_salv_existing.
WRITE: 'Error displaying grid CX_SALV_EXISTING!'.

ENDTRY.

ELSE.

WRITE: 'There is no data to display!'.

ENDIF.

ENDMETHOD. " DISPLAY_ALV_REPORT

If you do not want to display certain fields of the final table, we can set it to invisible.

Hide some fields or make them Invisible.

lr_column->set_visible( value if_salv_c_bool_sap=>false ).

Or

CALL METHOD lr_column->set_visible
EXPORTING
value = if_salv_c_bool_sap=>false.

Updated 12/26/2018

To put header data on top of the ALV, you can utilize the underneath piece.

Underneath capability would put the header.

lr_alv->set_top_of_list( lr_content ).

Check the reference snippet to build the header content.

*&---------------------------------------------------------------------*
*&      Form  built_header
*&---------------------------------------------------------------------*
FORM built_header CHANGING cr_content TYPE REF TO cl_salv_form_element.

  DATA: lr_grid   TYPE REF TO cl_salv_form_layout_grid,
        lv_row    TYPE i,
        lv_fdate  TYPE char10,
        lv_tdate  TYPE char10,
        lwa_error TYPE ty_error,
        lv_head   TYPE string VALUE 'Report of Dunned Accounts with No POSID',
        lv_text   TYPE string,
        lr_grid_1 TYPE REF TO cl_salv_form_layout_grid,
        lr_text   TYPE REF TO cl_salv_form_text,
        lr_label  TYPE REF TO cl_salv_form_label.

  lv_row = lines( it_error ).

  lv_row = lv_row + 2.

  CREATE OBJECT lr_grid.

  lr_grid->create_header_information(
    row    = 1
    column = 1
    text    = lv_head
    tooltip = lv_head ).

  lr_grid->add_row( ).

  lr_grid_1 = lr_grid->create_grid( row    = lv_row
                                          column = 1 ).

  lr_label =  lr_grid_1->create_label(
                      row     = 1
                      column  = 1
                      text    = 'Dunning Run Date:'(015)
                      tooltip = 'Dunning Run Date:'(015) ).

  IF s_id IS NOT INITIAL.

    lr_label =  lr_grid_1->create_label(
                      row     = 2
                      column  = 1
                      text    = 'Dunning Run ID:'(016)
                      tooltip = 'Dunning Run ID:'(016) ).
  ENDIF.

  CLEAR lv_text.

  IF s_date-high IS NOT INITIAL.
    WRITE s_date-low TO lv_fdate.
    WRITE s_date-high TO lv_tdate.
    CONCATENATE lv_fdate 'to' lv_tdate INTO lv_text SEPARATED BY space.
  ELSE.
    WRITE s_date-low TO lv_fdate.
    lv_text = lv_fdate.
  ENDIF.


  lr_text = lr_grid_1->create_text(
                    row     = 1
                    column  = 2
                    text    = lv_text    " s_date-low
                    tooltip = lv_text ). " s_date ).

  CLEAR lv_text.

  IF s_id IS NOT INITIAL.
    IF s_id-high IS NOT INITIAL.
      CONCATENATE s_id-low 'to' s_id-high INTO lv_text SEPARATED BY space.
    ELSE.
      lv_text = s_id-low.
    ENDIF.

    lr_text = lr_grid_1->create_text(
                      row     = 2
                      column  = 2
                      text    = lv_text      " s_id-low
                      tooltip = lv_text ).   "s_id ).

  ENDIF.

  CLEAR lv_row.
  lv_row = 2.
  LOOP AT it_error INTO lwa_error.
    lv_row = lv_row + 1.

    lr_label =  lr_grid_1->create_label(
                      row     = lv_row
                      column  = 1
                      text    = 'Dunning Lock Error:'(017)
                      tooltip = 'Dunning Lock Error:'(017) ).

    lr_text = lr_grid_1->create_text(
                      row     = lv_row
                      column  = 2
                      text    = lwa_error-message
                      tooltip = lwa_error-message ).
  ENDLOOP.

  lr_label->set_label_for( lr_text ).

  cr_content = lr_grid.

ENDFORM.                    " built_header

Kindly let us know, assuming we want to give further explanation on this subject.

YOU MAY LIKE THIS

Comprehensive Blog Series on ABAP on Cloud

Business Object Processing Framework (BOPF)

SAP Business Workflow

SAP ABAP Interview Questions Real time Expectations

Proxy in SAP Abap: Seamless Communication

WhatsApp WhatsApp us