Optimize Column width, Change color and Hide column in OO ALV

 Optimize Column width, Change color and Hide column in OO ALV

To optimize the width, change the color or hide any column we must first get the columns from ALV table using method GET_COLUMNS of class CL_SALV_COLUMNS_TABLE and to change the color and hide any column use method GET_COLUMN from CL_SALV_COLUMN_TABLE.

To Optimize columns width call method SET_OPTIMIZE from CL_SALV_COLUMNS_TABLE.

Methods used from CL_SALV_COLUMN_TABLE to hide and change the color.

  • SET_VISIBLE( VALUE = ABAP_FALSE ) – To Hide
  • SET_COLOR( VALUE = LS_COLOR ) – To Color the column


The code below will display the ALV report by optimizing the width of columns, hiding a column and change color of few columns. 


REPORT ztest_alv.

DATA: lo_alv   TYPE REF TO cl_salv_table.
DATA: lo_display TYPE REF TO cl_salv_display_settings.
DATA: lo_columns TYPE REF TO cl_salv_columns_table.
DATA: lo_column TYPE REF TO cl_salv_column_table.
DATA: ls_color TYPE lvc_s_colo.

START-OF-SELECTION.

  SELECT * FROM spfli INTO TABLE @DATA(lt_spfli).

  IF sy-subrc IS INITIAL.

    cl_salv_table=>factory(
      IMPORTING
        r_salv_table   = lo_alv
      CHANGING
        t_table        = lt_spfli[]
    ).

    lo_columns = lo_alv->get_columns( ).

    lo_columns->set_optimize( ). " Optimize column width

    lo_column ?= lo_columns->get_column( columnname = 'FLTYPE' ).

    lo_column->set_visible( value = abap_false ). " Hide Flight Type Column

    lo_column ?= lo_columns->get_column( columnname = 'FLTIME' ).
    ls_color-col = '5'. "Green
    lo_column->set_color( value = ls_color ). " Set Color

    lo_column ?= lo_columns->get_column( columnname = 'DISTANCE' ).
    ls_color-col = '6'. " Red
    lo_column->set_color( value = ls_color ).

    lo_alv->display( ).

  ENDIF. 

Before applying column width optimization, color and hiding column the ALV report looks like below.











After implementing the column optimize, color and hiding logic

Post a Comment

Previous Post Next Post