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

What is the difference between data reference and object reference in SAP ABAP

PARAMETERDATAOBJECT
DEFINITIONDATA lr_ref TYPE REF TO data.DATA lr_object TYPE REF TO object.
CREATIONCREATE DATA lr_ref TYPE REF TO (‘ZIF_INSTANCE’).CREATE OBJECT lr_object TYPE (‘ZCL_CLASS’).
DEREFERENCEIt can be dereferenced. The object or any value can be assigned to dereferenced reference variable.
lr_ref->* = lr_object
Object references cannot be dereferenced. This notation
->* cannot be used.
CONVERSION OR CASTINGHow to convert a value or an object in to a reference:
it can be converted in the following two ways.
lr_ref->* = value or object
lr_ref = REF #(value/obj)
How to cast an object.
lr_object2 = CAST zcl_object2( lr_object ).
USAGEused to create data referencesUsed to create objects for classes.
ASSIGNMENTAn object or any value cannot be assigned to a data reference variable. It must be deferenced in order to assign a value.
lr_ref->* = lr_object.
lr_ref->* = ‘string_value’
Only objects can be assigned. It can be assigned directly without dereference.
DATA lr_object TYPE REF TO object.
DATA lr_object2 TYPE REF TO object.
CREATE OBJECT lr_object TYPE (‘ZCL_CLASS’).
lr_object2 = lr_object.
POSSIBLE SYNTAX ERRORAs mentioned before, values cannot be assigned directly without dereference.
For example:
lr_ref = ‘String_value’. This line of code will result in the below Error.
“The type of ‘lr_interface1’ cannot be converted to the type of ‘lr_ref’
If the types of data objects are different, then it must be casted during the assignment. Otherwise you will get the below error.
‘The type of ‘lr_object’ cannot be converted to the type of ‘lr_object2’.

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.

How to insert functions to the Functions toolbar of new SAP ALV Display CL_SALV_TABLE

The new ALV Display based on the class CL_SALV_TABLE offers you the possibility to add a new function. It is completely based on the Display mode. In the container mode, the function is added through the functions object. In the complete mode, it is added through the GUI status for the Dynpro.

Adding functions in Container mode:

The Add_function( ) method of the functions object is used to insert functions to the functions toolbar.

DATA lr_functions_list TYPE REF TO cl_salv_functions_list.

lr_functions_list = lr_alv_object->get_functions( ).

lr_functions_list->set_all( ).

lr_functions_list->add_function( name = ‘SUM’  text = ‘SUM’  tooltip = ‘SUM’

position = if_salv_c_function_position=>right_of_salv_function ).

Adding functions in complete mode:

In order to insert a new function in complete mode, copy the GUI status SALV_TABLE_STANDARD in your report.

  1. Open the function group SALV_METADATA_STATUS through the transaction SE80. Right click on the GUI status SALV_TABLE_STANDARD.
  2. Select the copy from the options in order to copy the GUI status.
  3. Give the program name and a name for the copied GUI status in the popup window and finally confirm the entries.
  4. The ALV object must be able to recognize the newly added GUI-status. It is recognized through the set_screen_status method of the ALV object.

Lr_alv_object->set_screen_status( report = ‘ZALV_NEW_FUNCTION’ pfstatus = ‘SALV_TABLE_STANDARD’ set_functions = lr_alv_object->c_functions_all ).

The GUI status can be further adjusted to insert the new function code.

How to activate the functions Toolbar of new ALV Display CL_SALV_TABLE

By default, the function toolbar is not displayed in the new ALV Display. It must be activated. The functions object is retrieved from the ALV Object. In order to activate the complete function toolbar, the set_all( ) method of the function object must be called.

DATA lr_functions_list TYPE REF TO cl_salv_functions_list.

lr_functions_list = lr_alv_object->get_functions( ).

lr_functions->set_all( ).