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.

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