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 delete a column from the SAP ALV List

The column can be deleted from the SAP ALV List using the field catalog. Column deletion is constrained based on the fieldname of the field catalog.

Let us look at the following example:

DELETE lt_field_cat WHERE fieldname = ‘Contact_address’.

Once it is deleted through the field catalog, it cannot be further added using the layout button of the toolbar.

How to insert a new column in to the SAP ALV list

A new column can be inserted in to the SAP ALV list by adjusting the following two entities.

  1. Structure or Type of the table.
  2. New entry in the field catalog internal table.
  • Structure or Type of the Table.

A local type is created with the new column.

              TYPES: BEGIN OF ty_contact.

                                INCLUDE STRUCTURE ztt_db_table2.

                                TYPES: new_contact_address TYPE char0064.

                            END OF ty_contact.

  • Insertion of new column details entry in field catalog.

DATA ls_field_cat TYPE lvc_s_fcat.

ls_field_cat -col_pos = 5.

ls_field_cat -fieldname = ‘NEW_CON_ADD’.

ls_field_cat -inttype = ‘C’.

ls_field_cat -datatype = ‘CHAR’.

ls_field_cat -intlen = 64.

ls_field_cat -scrtext_s= ‘New’.

ls_field_cat -scrtext_m= ‘New Contact’.

ls_field_cat -scrtext_l= ‘New Contact Address’.

APPEND ls_field_cat TO lt_field_cat.

What are the different events of the class CL_GUI_ALV_GRID?

The class CL_GUI_ALV_GRID of the old ALV model provides different events. It is available in the Tab EVENTS of the class CL_GUI_ALV_GRID.

EventParameterDescription
BUTTON_CLICKE_ROW -> Row
E_COLUMN -> Column
E_ROW_NO -> Row id
It is triggered when a button is clicked.
USER_COMMANDE_UCOMM -> Function codeIt is triggered when a function is triggered,
DOUBLE_CLICK E_ROW -> Row
E_COLUMN -> Column
E_ROW_NO -> Row Id
It is triggered when a cell of the ALV Table is double clicked.
HOT_SPOT_CLICKE_ROW -> Row
E_COLUMN -> Column
E_ROW_NO -> Row Id
It is triggered when a Hotspot is clicked.
TOOLBARE_OBJECT -> It is an object of type CL_ALV_EVENT_TOOLBAR_SETIt is triggered when any entity on the toolbar is clicked.
Events of class CL_GUI_ALV_GRID

The implementation of the above events is similar for all events. It involves definition of Eventhandler, registering the Event handler with the SET HANDLER command.

The implementation of Double click event is shown below

CLASS zcl_handler DEFINITION PUBLIC.

PUBLIC SECTION.

CLASS_METHODS double_click_method FOR EVENT double_click

OF cl_gui_alv_grid IMPORTING e_row e_column es_row_no.

ENDCLASS.

CLASS zcl_handler IMPLEMENTATION.

METHOD double_click_method.

CALL FUNCTION ‘POPUP_TO_CONFIRM’

EXPORTING

titlebar = ‘Double Click occurred’

text_question = e_column-fieldname

text_button_1 = ‘Yes’

text_button_2 = ‘No’.

ENDMETHOD.

ENDCLASS.

Registration of Double Click event through SET HANDLER

SET HANDLER zcl_handler => double_click_method FOR lr_gui_alv_grid.

The class CL_GUI_ALV_GRID provides three methods to register events.

REGISTER_DELAYED_EVENT ( ) : If the cell of the ALV table or the line of record is changed, then this event can be used.

REGISTER_EDIT_EVENT ( ) :

If the data in the ALV is changed, then this event can be triggered automatically. The key benefit here is user does not have to click the ‘ENTER’ Button explicitly.

Let us look at this event with an example:

The edit event is registered with the help of the REGISTER_EDIT_EVENT method of the ALV object like below.

CALL METHOD lr_gui_alv_grid -> register_edit_event

EXPORTING

i_event_id = cl_gui_alv_grid => mc_evt_modified.

The parameter i_event_id is set to mc_evt_modified. By setting this event_id, the variable m_cell_edit is set explicitly to ‘X’. Even if the user forgets to click the enter key, then this variable m_cell_edit is set automatically.

REGISTER_F4_FOR_FIELDS ( ) :

If the F4- Value help for a field is clicked, then this event can be used.

How to set the column as editable in SAP ALV Grid

One of the key advantage of the old ALV Models is that it enables the user to edit the values in the table.

There are two different ways to set the ALV column as editable.

a) Field Catalog

b) SET_READY_FOR_INPUT

a) Field Catalog: The fields such as field name is used to set the column name of the ALV. The field ‘EDIT’ of the field catalog structure must be set to abap_true. Let us look at the following example.

FIELD-SYMBOLS <ls_field_cat> TYPE lvc_s_fcat.

LOOP AT lt_field_cat ASSIGNING <ls_field_cat>.

IF <ls_field_cat>-fieldname = ‘contact_address’.

<ls_field_cat>-edit = abap_true.

ENDIF.

ENDLOOP.

Thus the editing mode of the ALV Grid for the respective columns are activated. The ALV Grid facilitates

additional buttons for editing data.

b) The other way of editing data is through the SET_READY_FOR_INPUT method of the CL_GUI_ALV_GRID class. The editing mode is activated or deactivated through the parameter i_ready_for_input.

For activation:

CALL METHOD lr_gui_alv_grid->set_ready_for_input

EXPORTING

i_ready_for_input = 1.

For deactivation:

CALL METHOD lr_gui_alv_grid->set_ready_for_input

EXPORTING

i_ready_for_input = 0.

The data is modified through the above technique. It is much similar to the conventional

DISPLAY <-> CHANGE options of the ABAP Editor.

once the data is edited, it must be saved initially to the internal table. Based on the needs, the data can be saved to the Database table. The method check_changed_data of the class CL_GUI_ALV_GRID is used to check if the data of the ALV Grid has been changed.

DATA lv_alv_changed TYPE c.

CALL METHOD lr_gui_alv_grid->check_changed_data

IMPORTING

c_valid = lv_alv_changed.

The variable lv_alv_changed holds the value ‘X’ or initial. If the data of the grid is changed, then it is ‘X’. If it is not changed, it’s value is INITIAL. Therefore the changes are saved to the Internal table. If it is required, it can also be saved to the Database table.

How to Delete records from the internal table in SAP ABAP

The Data records present in the internal table can be deleted in following ways.

  • Key or work area (structure)
  • WHERE conditions.
  • Index
  • From Index TO Index
  • Clean, Refresh, free

DATA lt_internal_table TYPE TABLE OF ztt_db_table2.

DATA ls_structure TYPE ztt_db_table2.

SELECT *FROM ztt_db_Table2 INTO TABLE lt_internal_table.

              ls_structure – contact_id = 102.

              ls_structure – contact_name = ‘Thiru’.

              ls_structure – contact_address = ‘Street 10’.

Key or Work Area:

DELETE TABLE lt_internal_Table FROM ls_structure.

Only for key or structure, ‘TABLE’ is written explicitly after DELETE in the syntax.

WHERE:

DELETE lt_internal_table WHERE contact_id = 102.

INDEX:

DELETE lt_internal_table INDEX 2.

FROM INDEX TO INDEX:

DELETE lt_internal_table FROM 2 TO 3.

If the deletion is successful, then the system field sy-subrc will hold the value 0. If it is not successful, it will hold the value 4.