How to rename the column in the SAP ALV List through field catalog

The column names can be changed with the help of the fields REPTEXT, SCRTEXT_L, SCRTEXT_M and SCRTEXT_L of field catalog. If you wish to use only text of field ‘scrtext_l’, then fill the text only in this field ‘SCRTEXT_L’. The other texts can be ignored as the ALV takes the filled text automatically.

FIELD-SYMBOLS: <ls_field_cat> TYPE lvc_s_fcat.

LOOP AT lt_field_cat ASSIGNING <ls_field_cat> WHERE fieldname = ‘CONTACT_ADDRESS’.

              <ls_field_cat> – reptext = ‘New Contact address’.

              <ls_field_cat> – scrtext_l = ‘New Contact add’.

              <ls_field_cat> – scrtext_m = ‘New Contact’.

              <ls_field_cat> – scrtext_s = ‘New Con’.

ENDLOOP.

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.

How to hide the column of the SAP ALV List using Field Catalog field ‘NO_OUT’

The column can be hidden in the SAP ALV List using the field ‘NO_OUT’ of the field Catalog.

This field value is set to ABAP_TRUE in order to hide the column.

FIELD_SYMBOLS <ls_field_cat> TYPE lvc_s_fcat.

LOOP AT lt_field_cat ASSIGNING <ls_field_cat> WHERE fieldname = ‘CONTACT_ADDRESS’.

<ls_field_cat>-no_out = abap_true.

ENDLOOP.

The column can be shown once again using the layout button of the SAP ALV toolbar.

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 add user-defined buttons in SAP ALV TOOLBAR

In this blog post, we discuss about the events of the class CL_GUI_ALV_GRID. The old SAP ALV model provides standard functions such as summing, sorting, search function, Excel Export function etc. It also enables us to add new functions to the SAP ALV toolbar.

The Event TOOLBAR of the class CL_GUI_ALV_GRID is used to add functions to the toolbar of the SAP ALV List.

The following steps are needed in order to add a Button.

  1. Define the Event handler.
  2. Register the Event handler for the event.
  3. Activate the event through the method SET_TOOLBAR_INTERACTIVE()
  1. Definition of Event Handler.

The Event handler is defined in the static method of the class like below.

              CLASS zcl_handler DEFINITION PUBLIC.

   PUBLIC SECTION.

                       CLASS-METHODS st_select_all_method FOR EVENT toolbar

                          OF cl_gui_alv_grid IMPORTING e-object.

               ENDCLASS.

               CLASS zcl_handler IMPLEMENTATION.

                      METHOD st_select_all_method.

                            DATA ls_button_toolbar TYPE stb_button.

                            ls_button_toolbar-function = ‘SELALL’.

                            ls_button_toolbar-icon = icon-select-all.

                            ls_button_toolbar-quickinfo = ‘SELECT ALL function’.

                            INSERT ls_button_toolbar INTO e_object -> mt_toolbar INDEX 2.

                     ENDMETHOD.

               ENDCLASS.

2. Event handler Registration.

The Event handler is registered using the ‘SET HANDLER … FOR … ‘ command.

An example below shows the registration.

SET HANDLER zcl_handler => st_select_all_method FOR lr_gui_alv_grid.

3. Activation of Event.

As a last step, the event must be activated through the method SET_TOOLBAR_INTERACTIVE. This ensures that the event is triggered from ALV. It is the method of the ALV object.

CALL METHOD lr_gui_alv_grid -> set_toolbar_interactive( )

 The event handler registration and event activation code section appears between the ALV table call ‘SET_TABLE_FOR_FIRST_DISPLAY’ and the ‘CALL SCREEN’ of Dynpro call.

 The Event handler uses the parameter e_object in order to insert the button entry into the internal table mt_toolbar. The parameter E_OBJECT is of type CL_ALV_EVENT_TOOLBAR_SET.

How to enable adding new records to the SAP ALV List

The primary key fields of the data records must be unique. There should not be duplicate records in the Database table. The fields of the ALV List are editable through the field catalog. The primary key fields of the SAP ALV List cannot be edited directly. If a new line of record is inserted in to SAP ALV List, the primary key fields of the SAP ALV List are disabled by default. It is not possible to insert a new record in the ALV List through the edit field of the field catalog.

Contact_IdContact_nameContact_address
101RamStreet 10
102MahesStreet 11

If a new line of record is inserted in to above SAP ALV List, the field contact_id is disabled by default. The contact_id is the primary key field of the SAP ALV List. If a new line of record must be inserted in the Table, then the primary key fields of the SAP ALV List must be enabled for editing. The primary key fields of the existing data records must be disabled by default.

A new line of record can be inserted in to the ALV Table through the internal table lvc_t_styl. It is introduced to the structure of the internal table of data records. The keywords ‘INCLUDE STRUCTURE’ adds the existing structure of internal table which contains the data records. The following steps helps us to achieve this.

  • Define the structure with lvc_t_styl.
  • Define the stylename field of the layout
  • Define the style field of lvc_s_styl to CL_GUI_ALV_GRID=>MC_STYLE_DISABLED.

  • Define the structure with lvc_t_styl.

TYPES: BEGIN OF st_contact.

INCLUDE STRUCTURE ztt_db_table2.

TYPES: fieldstyle TYPE lvc_t_styl,

END OF st_contact.

DATA lt_contact TYPE TABLE OF st_contact.

  • Define the stylename field of the layout

DATA ls_layout TYPE lvc_s_layo.

ls_layout-stylename = ‘FIELDSTYLE’.

This layout is given as parameter while calling the set_table_for_first_display method of the ALV object.

  • Define the style field of lvc_s_styl to CL_GUI_ALV_GRID=>MC_STYLE_DISABLED.

This style is defined for the primary key fields of the database table. This style is set to each data record of the database table. But it is constrained to specific columns by specifying the fieldname of the data records. It is finally inserted in to the field style of each data record.

Key steps to set the style field are:

a) Define the fieldname of structure lvc_s_styl.

b) Define the style of structure lvc_s_styl.

c) Insert the structure lvc_s_styl in the fieldstyle field of each data record. Fieldstyle field is the internal table of type lvc_t_styl.

The selection of the style cl_gui_alv_grid=>mc_style_disabled blocks the fields of the respective column in editing mode. For example in the above table, the editing of column CONTACT_ID is deactivated.

DATA ls_style_record TYPE lvc_s_styl.

FIELD-SYMBOLS <ls_contact> TYPE ztt_db_table2.

LOOP AT lt_contact ASSIGNING <ls_contact>.

ls_style_record-fieldname = ‘CONTACT_ID’.

ls_style_record-style = CL_GUI_ALV_GRID=>MC_STYLE_DISABLED.

INSERT ls_style_record INTO TABLE <ls_contact>-fieldstyle.

ENDLOOP.

The primary key fields of the table will now be available for insertion of new record. The insert button facilitates insertion of new record. The primary key fields of the ALV List for already existing data records are disabled for editing.

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 create an ALV List based on class CL_GUI_ALV_GRID

It is the only ALV variant which is editable. We need an internal table which is based on a global structure to show the data.

There are two Display modes to display the data.

  • Full screen display : The output of the ALV List occupies the whole screen. It does not contain Container and Dynpro.
  • Container mode: It needs a Dynpro and a container.

ALV Table is built based on the following steps

  1. Loading the data in the internal table
  2. Creation of Containers. This step is optional.
  3. Assign the structure of the internal table to the Field catalog. This step is also optional.
  4. Creation of ALV Object.
  5. Definition of layout. This step is optional.
  6. Display ALV Table contents.
  • Loading data in an internal table: The data from the database table are loaded in to the internal table.

DATA lt_contact TYPE TABLE OF ztt_db_table2.

SELECT * FROM ztt_db_table2 INTO TABLE lt_contact UP TO 50 ROWS.

  • Creation of Containers

Container object is created with an unique ID. It is used for the created Dynpro. This is mainly used to show the ALV List in a particular area of the screen. If the display is not a requirement, then you can opt for the full screen display and skip this step. But it is necessary to provide the parameter cl_gui_custom_container=>default_screen in the constructor during the creation of the ALV List.

DATA lr_container TYPE REF TO cl_gui_custom_container.

CREATE OBJECT lr_container

EXPORTING

container_name = ‘container_id’.

  • Assignment of Structure to Field catalog

The structure of the internal table is assigned to the field catalog. It includes important elements such as column heading, Display size, Data type of the column. It is always recommended to use the global structure created from Transaction SE11.

DATA lt_field_cat TYPE lvc_t_fcat.

CALL FUNCTION ‘LVC_FIELDCATALOG_MERGE’

EXPORTING

i_structure_name = ‘ZTT_DB_TABLE2’

CHANGING

ct_fieldcat = lt_field_cat.

Note: This step can be skipped , if you would not like to modify the columns in the display. But the parameter i_structure_name must be specified during the call of the method SET_TABLE_FOR_FIRST_DISPLAY of the ALV Object. The field catalog is automatically assigned in the ALV.

  • Creation of ALV Object

An object is created for the class CL_GUI_ALV_GRID. Assign the created container object in the parameter of the constructor. For the full screen display, instead of the container object, cl_gui_custom_container=>default_screen is assigned to the parameter.

DATA lr_gui_alv_grid TYPE REF TO cl_gui_alv_grid.

CREATE OBJECT lr_gui_alv_grid

EXPORTING i_parent = lr_container.

  • Layout definition

The layout of the ALV Table can be changed further using this option. The various fields of the layout structure is used to design the layout of the ALV table in different formats.

DATA ls_struc_layout TYPE lvc_s_layo.

ls_struc_layout-no_toolbar = abap_true.

If this field is set, then the ALV Grid Toolbar is hidden. There are also other fields such as GRID_TITLE which displays the title on the ALV Grid.

  • Display of ALV Table.

The SET_TABLE_FOR_FIRST_DISPLAY( ) method of the ALV object is called in the final step. The layout structure is given as the exporting parameter, the internal table and field catalog is specified as the changing parameter. After this method call, the Dynpro is called using the command CALL SCREEN.

CALL METHOD lr_gui_alv_grid->set_table_for_first_display

EXPORTING

is_layout = ls_struc_layout

CHANGING

it_outtab = lt_contact

it_fieldcatalog = lt_field_cat.

CALL SCREEN 1000.

What are the list of available models to program the SAP ALV Tables

Displaying Table contents is one of the important requirement in any application. ALV is the short form of ABAP List viewer. ALV is used to display the table of contents. It generally shows the data of the internal table. It can also be used to export data in to Excel file.

REUSE_ALV_GRID_DISPLAY: It is based on the function module. DDIC Structure, Field catalog und merged field catalog are parameters of the function module.

CL_GUI_ALV_GRID: It is one of the old models of the SAP ALV List. This old model facilitates users to edit and input data in the list. Therefore the user must have authorization.

CL_SALV_TABLE: It is the new model of SAP ALV List. It has a simplified user interface. The User cannot edit or input data. The functions are very similar to old models. The new ALV models can be editable by using the old ALV Types and Adapter Class.

CL_SALV_GUI_TABLE_IDA: It is the newly introduced ALV with Integrated data access(IDA). It has a very high performance. It is directly connected to a database or view.

Most important variables used in ALV List:

  1. Internal table : It contains the table of data which is read from the Database table.
  2. Containers: It is used to show the ALV Table in a certain area of the screen.
  3. Field Catalog: It contains information such as the Column heading, breadth of the column and the data type of the column.
  4. ALV Object: It is the reference variable which specfies the ALV Object.
  5. Layout: It is variable of type structure ( lvc_s_layo ). As the name clearly states that it is used to change the layout of the table.

Creation of ALV List is an easy task. But to bring additional features in the ALV List such as editing fields, disabling fields, Column changes, Highlighting rows or columns, Icons, sorting data, functions, events are more complicated.

ALV List models