How to fill icons in SAP ALV Display of type CL_SALV_TABLE

In order to fill icons in ALV Table, we must add an additional field of type ICON_D. The type ICON_D is of datatype CHAR with length 4. This is later filled with the icon code.

Step1: Define the icon field

TYPE-POOLS: icon.

TYPES: BEGIN OF ty_contact.

                  INCLUDE STRUCTURE ztt_db_table2.

                  TYPES: icon_field TYPE icon_d,

               END OF ty_contact.

Step2: Fill the internal table with data contents.

DATA lt_contact TYPE TABLE OF ty_contact.

SELECT * FROM ztt_db_table2 INTO TABLE lt_contact.

Step3: Fill the internal table with icon code.

FIELD-SYMBOLS <ls_contact> TYPE ty_contact.

LOOP AT lt_contact ASSIGNING <ls_contact>.

               <ls_contact>-icon = icon_positive.

ENDLOOP.

How to color the cells of SAP ALV Display of type CL_SALV_COLUMNS_TABLE

Step 1:

Desired cells of the SAP ALV Display can be colored using the structure lvc_s_scol. The color codes must be set to each line of record. Therefore the structure of the ALV Display must be extended with the additional field with an internal table of type lvc_t_scol.

TYPES: BEGIN OF ty_contact.

              INCLUDE STRUCTURE ztt_db_table2.

              TYPES: color_field TYPE lvc_t_scol,

              END OF ty_contact.

DATA ls_color_field TYPE lvc_s_scol.

DATA lr_all_columns TYPE REF TO cl_salv_columns_table.

DATA lt_contact TYPE TABLE OF ty_contact.

SELECT * FROM ztt_db_table2 INTO TABLE lt_contact.

Lr_all_columns = lr_salv_table->get_columns( ).

Step2:

Make the ALV Table recognize the newly added ‘color_field’ with the help of the method set_color_column( ) of class CL_SALV_COLUMNS_TABLE.

Lr_all_columns->set_color_column( ‘COLOR_FIELD’ ).

Step3:

Finally fill the internal table ( new field ) with the desired color codes.

DATA ls_contact TYPE ztt_db_table2.

LOOP AT lt_contact ASSIGNING ls_contact WHERE age > 30.

              Ls_color_field-fname = ‘CONTACT_ADDRESS’.

              Ls_color_field-color-col = 7.

              Ls_color_field-color-int = 1.

              Ls_color_field-color-inv = 1.

              APPEND ls_color_field TO ls_contact-color_field.

ENDLOOP.

How to color the complete column of the SAP ALV Display of type CL_SALV_COLUMN_TABLE

The method set_color of the class CL_SALV_COLUMN_TABLE can be used to color the column of SAP ALV Display.

The color codes are much similar to old ALV Display of type CL_GUI_ALV_GRID

DATA lr_all_columns TYPE REF TO cl_salv_columns_table.

DATA lr_single_column TYPE REF TO cl_salv_column_table.

Lr_all_columns = lr_salv_table->get_columns( ).

Lr_single_column ?= lr_all_columns->get_column(‘CONTACT_NAME’).

DATA ls_color_column TYPE lvc_s_scol.

Ls_color_column-col = 7.

Ls_color_column-int = 1.

Ls_color_column-inv = 1.

Lr_single_column->set_color( value = ls_color_column ).

The column ‘Contact_name’ is colored or highlighted in orange colour.

The following table shows the other different colour codes. It is same as the old ALV Display.

Colorcolor_codecolintinv
GreenC510510
RedC610610
OrangeC711711
Color Codes

What is the difference between check box and check box hotspot cell type in CL_SALV_TABLE?

CheckboxCheckbox Hotspot
It is used just as a display of checkbox. It mentions that the particular line of record is selected.It can be used to trigger an event of type LINK_CLICK.
The value of the checkbox cannot be changed.The value of checkbox can be changed.
It is not used to manipulate the values of the fields of ALV Table.It can be used to manipulate the values of the fields of ALV Table. The ALV Table must be refreshed with the refresh method call {lr_alv_grid -> REFRESH ()}.

How to use various cell types in new SAP ALV Display of type CL_SALV_TABLE?

               The various cell types can be used both in container mode and complete mode. The cell types are constructed based on the Attribute of the Interface ‘IF_SALV_C_CELL_TYPE’.

Type of cellAttribute of IF_SALV_C_CELL_TYPEDisplay mode container / completeEvent
Text=> TextBoth
Checkbox=> CheckboxBoth
Checkbox hotspot=> checkbox_hotspotBothlink_click
Button=> ButtonBoth link_click
Dropdown=> dropdownContainer
Hotspot=> HotspotBoth link_click

The method set_cell_type of the column object ( cl_salv_column_table ) can be used to set the cell type for the cells in the ALV Display.

DATA lr_all_columns TYPE REF TO cl_salv_columns_table.

DATA lr_single_column TYPE REF TO cl_salv_column_table.

lr_all_columns = lr_alv_grid->get_columns ().

lr_single_column ?= lr_all_columns->get_column (‘Contact_name’ ).

lr_single_column->set_cell_type (if_salv_c_cell_type =>Button).

How to optimize the width of the column of type CL_SALV_COLUMNS_TABLE

               The width of the column is optimized through three methods.

  1. Optimize all columns.
  2. Optimize one single column.
  3. Manually set the width of columns.

The optimization is based on the content of the ALV Table.

  1. Optimize all columns.

In order to optimize all the columns of the ALV Table, call the set_optimize method of the columns object.

DATA lr_all_columns TYPE REF TO cl_salv_columns_table.

lr_all_columns = lr_alv_grid->get_columns ( ).

lr_all_columns->set_optimize ( ).

  • Optimize one single column.

‘SET_OPTIMIZED ( )’ method of the cl_salv_column_table is used to optimize the single column of the ALV Table.

DATA lr_all_columns TYPE REF TO cl_salv_columns_table.

DATA lr_single_column TYPE REF TO cl_salv_column_table.

lr_all_columns = lr_alv_grid -> get_columns ( ).

lr_single_column ?= lr_all_columns -> get_column ( ‘Contact_name’ ).

lr_single_column -> set_optimized ( abap_true ).

  • Manual adjustment of column width.

The method set_output_length of the class cl_salv_column_table can be used to set the width of the particular column to a specific predefined value.

DATA lr_all_columns TYPE REF TO cl_salv_columns_table.

DATA lr_single_column TYPE REF TO cl_salv_column_table.

lr_all_columns = lr_alv_grid -> get_columns ( ).

lr_single_column ?= lr_all_columns -> get_column ( ‘Contact_name’ ).

lr_single_column -> set_output_length ( 50 ).

The above manual adjustment of column width will be ignored, if the columns are optimized with the set_optimize ( ) method.

How to rename columns in SAP ALV DISPLAY of type CL_SALV_COLUMNS_TABLE.

The columns can be renamed with the object of the class cl_salv_column_table. There are four methods available such as SET_SHORT_TEXT (), SET_MEDIUM_TEXT(), SET_LONG_TEXT and SET_TOOLTIP(). The method can be chosen based on our requirement of text length.

DATA lr_salv_columns_table TYPE REF TO cl_salv_columns_table.

DATA lr_salv_column_table TYPE REF TO cl_salv_column_table.

DATA lv_short_text TYPE scrtext_s.

DATA lv_medium_text TYPE scrtext_m.

DATA lv_long_text TYPE scrtext_l.

DATA lv_tip_text TYPE lvc_tip.

lr_salv_columns_table = lr_alv_grid -> get_columns ().

lr_salv_column_table ?= lr_salv_columns_table -> get_column(‘CONTACT_NAME’).

lv_short_text = ‘short_contact_name’.

lr_salv_column_table -> set_short_text (lv_short_text).

lv_medium_text = ‘medium_contact_name’.

lr_salv_column_table -> set_medium_text (lv_medium_text).

lv_long_text = ‘long_contact_name’.

lr_salv_column_table -> set_long_text (lv_long_text).

lv_tip_text = ‘tip_contact_name’.

lr_salv_column_table -> set_tooltip(lv_tip_text).

If you wish to force the ABAP execution to use the short text instead of other texts, then there is an easy way to achieve this. Just ignore assigning the medium and long text, then it automatically considers the short text. The other two text formats are ignored by default.

How to insert and delete column in SAP ALV Display of type CL_SALV_TABLE.

The column is inserted through the field catalog of the ALV Table. It is set during the method call FACTORY ().

Step1: Extend the structure either locally or globally through the Transaction SE11 with the additional field.

TYPES: BEGIN OF ty_contact.

                            INCLUDE STRUCTURE ztt_db_table2.

                            TYPES: contact_age TYPE ZDE_contact_age.

              END OF ty_contact.

DATA lt_contact TYPE STANDARD TABLE OF ty_contact.

              It is always recommended to use an unique Data element for the fields of the structure, so that the column heading text can be easily maintained.

Step2: Call the Factory() Method of class CL_SALV_TABLE

CALL METHOD cl_salv_table => factory

              IMPORTING

                            r_salv_table = lr_alv_object

              CHANGING

                            t_table = lt_contact.

Through the field catalog of the ALV Table, a new column can be inserted or deleted.

There is a method REMOVE_COLUMN() for the column class ‘CL_SALV_COLUMN_TABLE’. But it is protected. Therefore the inheritance is needed to remove the column.

How to hide the column in new SAP ALV Display of type CL_SALV_COLUMNS_TABLE

The column can be edited using the column object. The Column object can be retrieved from the get_columns( ) method of the ALV object.

Step1: Get the columns object from the ALV object.

DATA lr_columns TYPE REF TO cl_salv_columns_table.

Lr_columns = lr_alv_object->get_columns( ).

Step2: From the columns object, get the particular specific column object ‘CONTACT_ID’. The name mentioned in the structure must be used. Even if the displayed name in ALV output is different, please always use the name in the structure.

DATA lr_specific column TYPE REF TO cl_salv_column_table.

Lr_specific_column = lr_columns->get_column( ‘CONTACT_ID’ ).

Step3: Call the set_visible method of the column object to hide the column in ALV Display. Please set the abap_false value to the importing parameter.

Lr_specific_column->set_visible( abap_false ).

How to use events of class CL_SALV_EVENTS_TABLE in SAP ABAP ALV Display

The new ALV Display provides different events based on the class CL_SALV_EVENTS_TABLE. It is much lesser compared to the old ALV Display. The Events can be found in the ‘Events’ tab of the class.

Let us discuss about the following important Events of the class CL_SALV_EVENTS_TABLE.

EventsDescription
DOUBLE_CLICKThis event will be triggered, when the user double clicks.
LINK_CLICKThis event will be triggered, when the user clicks on Hotspot or Button. Parameters are Row and Column
ADDED_FUNCTIONThe defined function will be triggered. The parameter is E_SALV_FUNCTION.

The following steps are needed to facilitate an Event action.

  1. Define the Event handler
  2. Register the Event handler
  • Definition of Event Handler

CLASS zcl_handler DEFINITION.

    PUBLIC SECTION.

         CLASS-METHODS:  on_link_click FOR EVENT link_click OF cl_salv_events_table

              IMPORTING row column.

ENDCLASS.

CLASS zcl_handler IMPLEMENTATION.

    METHOD on_link_click.

          FIELD-SYMBOLS <ls_contact>  TYPE  ztt_db_table2.

          READ TABLE lt_contact INDEX row  ASSIGNING <ls_contact>.

          IF  sy-subrc  = 0   AND column = ‘CONTACT_ID’.

              “” DETAILS OF THE CONTACT PERSON IS DISPLAYED.

         ENDIF.

    ENDMETHOD.

ENDCLASS.

  • Register the Event Handler

START-OF-SELECTION.

DATA lr_salv_events_table TYPE REF TO cl_salv_events_table.

Lr_salv_events_table = lr_alv_grid->get_event( ).

SET HANDLER zcl_handler=>link_click  FOR  lr_salv_events_table.