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 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 set the column position using the class CL_SALV_COLUMNS_TABLE?

The column position in the ALV Display can be controlled using the set_column_position method of the class CL_SALV_COLUMNS_TABLE. There are two parameter used in the method call. The column name identifies the column to be positioned. The position parameter mentions the position of the column in ALV Display.

Let us look at the following example:

DATA lr_salv_columns_table TYPE REF TO cl_salv_columns_table.

lr_salv_columns_table = lr_alv_grid->get_columns ().

lr_salv_columns_table->set_column_position ( columnname = ‘CONTACT_ADDRESS’

position = 3).

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 ).