How to set the position of column in SAP ALV List using Field catalog

The column position in the SAP ALV List can be changed using the COL_POS field of the field catalog.

In the below example, the ‘contact_address’ column is set in the first position of the SAP ALV List.

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> -col_pos = 1.

ENDLOOP.

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