How to save Layout in ALV List.

The user can be able to change the layout such as hiding columns, sorting specific columns or adding a filter in ALV Table List. The user can also be able to save the layouts of ALV Report in the toolbar. In order to enable this feature, the user must activate this option programmatically.

              There are two key steps to activate the save layout option in ALV table list.

  1. Assign the fields report, username of the structure disvariant.
  2. Set the exporting parameters is_variant, i_save during the method call SET_TABLE_FOR_FIRST_DISPLAY of ALV object.
  • Assign fields of structure DISVARIANT

It contains the details of the report and username to which the layout can be saved.

              DATA ls_variant TYPE disvariant.

              ls_variant-report = sy-repid.

              ls_variant-username = sy-uname.

  • Setting the exporting parameters is_variant, i_save.

There are different saving options such as ‘U’, ‘X’, ‘A’ for the exporting parameter i_save.

              U: User defined layout can be saved in this option. The layout cannot be seen by other users.

X: It is the Global layout. Other users can also see this layout.

              A: The user can save both user defined layout and Global layout.

              Let us look at the following SET_TABLE_FOR_FIRST_DISPLAY Method.

              CALL METHOD lr_gui_alv_grid -> set_table_for_first_display

                            EXPORTING

                                          is_variant = ls_variant.

                                          is_save = ‘U’.

              If you are having more than one ALV in the same program, then each ALV must be uniquely differentiated through the field HANDLE. The field HANDLE hold a ID with four characters which is unique. Each ALV variant is differentiated through this unique ID.

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.