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 activate the functions Toolbar of new ALV Display CL_SALV_TABLE

By default, the function toolbar is not displayed in the new ALV Display. It must be activated. The functions object is retrieved from the ALV Object. In order to activate the complete function toolbar, the set_all( ) method of the function object must be called.

DATA lr_functions_list TYPE REF TO cl_salv_functions_list.

lr_functions_list = lr_alv_object->get_functions( ).

lr_functions->set_all( ).

How to create a SAP ALV Display using the class CL_SALV_TABLE

The new ALV Display model based on the class CL_SALV_TABLE provides various functionalities similar to the old ALV Display. But the data fields of the ALV Table are not directly editable.

There are two Display modes for the new SAP ALV Display based on class CL_SALV_TABLE.

Full Screen mode: There is no need of container and Dynpro.

Container mode: It is created through the Dynpro and container.

Full Screen mode: The following steps are necessary to construct an SAP ALV Display based on the class CL_SALV_TABLE.

  1. Fill an Internal table with the Details.
  2. Create an object of class CL_SALV_Table through FACTORY( ) method.
  3. Display ALV.
  • Loading Internal Table

DATA lt_contact TYPE TABLE OF ztt_db_table2.

SELECT * FROM ztt_db_table2 INTO TABLE lt_contact.

  • Creation of object of type CL_SALV_TABLE

The static method ‘FACTORY’ of the class CL_SALV_TABLE returns the object of class CL_SALV_TABLE as an exporting parameter. This is the instance of the ALV object.

DATA lr_salv_table TYPE REF TO cl_salv_table.

CALL METHOD cl_salv_table => factory

              IMPORTING

                            r_salv_table = lr_salv_table

              CHANGING

                            t_table = lt_contact.

  • ALV Display

Finally the ALV List is displayed by calling the Display method of the ALV object.

lr_salv_table -> Display ().

Container mode :

              In order to display the ALV list in container mode, you need a Dynpro with a container.

The following steps are written to Display the ALV.

  1. Creation of container object.

DATA lr_container TYPE REF TO cl_gui_custom_container.

CREATE OBJECT lr_container

              EXPORTING

                            Container_name = ‘SALV_CONTAINER’.

  • Creation of ALV object.

In this mode as well, the static method ‘FACTORY’ of the class cl_salv_table is used to create the ALV object. The container object is given as an exporting parameter in the method call. The Internal table with the ALV contents is specified in the changing parameter.

              DATA lr_salv_table TYPE REF TO cl_salv_table.

              CALL METHOD cl_salv_table => factory

                            EXPORTING

                                          r_container = lr_container

                            IMPORTING

                                          r_salv_table = lr_salv_table

                            CHANGING

                                          t_table = lt_contact.

  • Display the ALV List.

lr_salv_table -> display ().

CALL SCREEN 1000.

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 design ALV List with total and subtotals.

The columns of the ALV list can be aggregated using the fields subtot of internal table lvc_t_sort.

There are mainly two changes needed to achieve this Aggregation.

  1. The Internal table lvc_t_sort with the field subtot must be set to ‘X’.
  2. The field ‘do_sum’ of field catalog must be set to ‘X’ to the column which needs to be aggregated.

Let us look at the following code example.

DATA lt_sort TYPE lvc_t_sort.

DATA ls_sort TYPE lvc_s_sort.

ls_sort-fieldname = ‘CONTACT_ID’.

ls_sort-subtot = ‘X’.

APPEND ls_sort TO lt_sort.

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

<ls_field_cat>-do_sum = ‘X’.

ENDLOOP.

How to sort columns by default in SAP ALV List.

The colums can be sorted by default in the ALV table. So that it is sorted initially when it is first displayed.

              The internal table of type LVC_T_SORT can be used for this purpose.

              The following key fields of LVC_T_SORT to be filled are:

Fieldname: The column to be sorted must be mentioned in this field.

UP: If the column must be sorted in increasing or ascending order,then the value ‘X’ must be set to the field ‘UP’.

DOWN: If the column must be sorted in descending or decreasing order, then the value ‘X’must be set to the field ‘DOWN’.

SPOS: If two or more columns must be sorted. Then we can prioritize the sorting sequence. For example, second column must be sorted before the first column.

Let us look at the ALV table sorting with the following example.

DATA lt_sort TYPE lvc_t_sort.

DATA ls_sort TYPE lvc_s_sort.

              ls_sort-spos = 1.

              ls_sort-fieldname = ‘CONTACT_NAME’.

              ls_sort-up = ‘X’.

              APPEND ls_sort TO lt_sort.

              ls_sort-spos = 2.

              ls_sort-fieldname = ‘CONTACT_ID’.

              ls_sort-down =’X’.

              APPEND ls_sort TO lt_sort.

              CALL METHOD lr_gui_alv_grid -> set_table_for_first_display

                            EXPORTING

                                          is_layout = ls_layout

                            CHANGING

                                          it_outtab = lt_contact

                                          it_fieldcatalog = lt_field_cat

                                          it_sort = lt_sort.

How to display icons with Infotext in cells of ALV list.

There are three key steps involved in displaying an icon with Infotext in the cells of SAP ALV List.

  1. Extension of Internal table with an additional field of type char40.
  2. Fill the additional field with icon name and information text.
  3. Assign the new field to field catalog.
  • Extension of Internal table with an additional field of type char40.

TYPES: BEGIN OF ty_contact.

                            INCLUDE STRUCTURE ztt_db_table2.

                            TYPES: icon_text TYPE char40,

              END OF ty_contact.

DATA lt_contact TYPE TABLE OF ty_contact.

  • Filling the internal table with icon and information text.

SELECT * FROM ztt_db_table2 INTO TABLE lt_contact.

LOOP AT lt_contact ASSIGNING FIELD-SYMBOL (<ls_contact>).

              <ls_contact>-icon_text = ‘@09\Q Info_text @’.

ENDLOOP.

The value of field icon_text is based on the format ‘@xyz\Q info_text @’

The value of xyz in the above format is determined based on our needs. The various ICON codes are available through the type group ‘ICON’.

Transaction -> SE11.

Type Group -> Enter ‘ICON’ in Type group.

It will list the available icon ids.

Note: Please don’t forget to give a space character after ‘Q’ and before the @symbol.

              The user can give their own text instead of info_text in the format mentioned above.

              In our example, we have considered the icon ‘ICON_YELLOW_LIGHT’. It’s ID is ‘@09@’.

  • Assignment of field ‘icon’ to field catalog.

DATA ls_field_Cat TYPE lvc_s_fcat.

ls_field_cat-fieldname = ‘ICON_TEXT’.

ls_field_cat-icon = ‘X’.

ls_field_cat-outputlen = 40.

APPEND ls_field_cat TO lt_field_cat.

How to display icon in SAP ALV cell

There are three important steps involved in displaying icon in the ALV cell.

  1. Extension of Internal table with the additional field ICON_D.
  2. Filling the icon field to the records of ALV table.
  3. Assignment of field ‘ICON’ to field catalog.
  • Extension of Internal table with field ICON_D.

TYPES: BEGIN OF ty_contact.

                            INCLUDE STRUCTURE ztt_db_table2.

                            TYPES: icon TYPE icon_d,

              END OF ty_contact.

  • Filling the icon field to the records of ALV table.

FIELD_SYMBOLS <ls_contact> TYPE ty_contact.

DATA lt_contact TYPE TABLE OF ty_contact.

SELECT * FROM ztt_db_table2 INTO CORRESPONDING FIELDS OF TABLE lt_contact.

LOOP AT lt_contact ASSIGNING <ls_contact>.

              <ls_contact>-icon = ICON_YELLOW_LIGHT.

ENDLOOP.

Through the transaction ‘ICON’, you can find the list of all available icons. The corresponding symbol of the icon is also displayed in the list.

If any message needs to be displayed with this icon, it must be referred to the Type group ‘ICON’ with the statement TYPE-POOLS: icon.

  • Assignment of field ‘icon’ to field catalog.

DATA ls_field_cat TYPE lvc_s_fcat.

ls_field_cat-fieldname = ‘ICON’.

ls_field_cat-icon = ‘X’.

ls_field_cat-outputlen = 3.

APPEND ls_field_cat TO lt_field_cat.

How to set the column width in SAP ALV Display

The width of the column in the SAP ALV List is controlled through the fields OUTPUTLEN and COL_OPT of the field catalog. The field OUTPUTLEN of the field catalog sets the size of the column with an Integer value. By setting the field COL_OPT of the field catalog to ABAP_TRUE, the width of the column is adjusted according to the text content of the column.

So the content with the maximum value defines the width of the column. If both the fields of the field catalog is used, then the optimized column width option is preferred automatically.

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>    WHERE fieldname = ‘CONTACT_ADDRESS’.

              <ls_field_cat>-outputlen = 50.

              <ls_field_cat>-col_opt = ‘X’.

ENDLOOP.

How to color the cell in SAP ALV Display?

The old ALV Display model created using CL_GUI_ALV_GRID enables the user to colour particular cells of ALV Display. Every colour is uniquely identified through a colour code. The following table illustrates the different colour codes.

Type of colourCOLINTINV
Orange711
Green510
Red610
Yellow310
Blue110
Fig. Color codes for cells of ALV Display

The above fields COL,INT,INV are the elements of [lvc_s_scol – COLOR].

There are three key steps involved in colouring a particular cell / column of a SAP ALV Display.

  1. Extend the Internal table with the column lvc_t_scol.
  2. Assignment of column ‘lvc_t_scol’ to the field ‘CTAB_FNAME’ of layout.
  3. Assign the colour codes to the cells of SAP ALV Display.
  • Extension of Internal table.

The internal table which contains the data of the ALV Display is extended with the additional column ‘lvc_t_scol’. The table lvc_t_scol holds the details of the colour code. Let us look at the following structure extension.

TYPES: BEGIN OF ty_contact.

              INCLUDE STRUCTURE ztt_db_table2.

              TYPES: cell_with_color TYPE lvc_t_scol,

            END OF ty_contact.

DATA lt_contact TYPE TABLE OF ty_contact.

  • ASSIGNMENT of field ‘CTAB_FNAME’ to the layout.

The ALV Table recognizes the newly added field of type ‘LVC_T_SCOL’ through the field ‘CTAB_FNAME’ of the layout structure.

DATA ls_layout TYPE lvc_s_layo.

ls_layout-ctab_fname = ‘CELL_WITH_COLOR’.

the above value must be specified in capital letters. otherwise the ALV Grid cannot recognize the newly added field.

  • Assignment of Colour code to the cells of ALV Display.

The final step is to assign the colour codes to the cells of the ALV Table Display. The column is mentioned through the field ‘FNAME’ of the structure ‘lvc_s_scol’.

FIELD-SYMBOLS <ls_contact> TYPE ty_contact.

DATA ls_cell_color TYPE lvc_s_scol.

LOOP AT lt_contact ASSIGNING <ls_contact> WHERE age > 30.

              ls_cell_color-fname = ‘CONTACT_NAME’.

              ls_cell_color-col = 7.

              ls_cell_color-int= 1.

              ls_cell_color-inv = 1.

              APPEND ls_cell_color TO <ls_contact>-cell_with_color.

“The internal table is filled with color code details.

ENDLOOP.

              The above code snippet sets the colour of the ALV Cells. It is constrained to the contacts whose age are more than 30. The column specified here to be colored is ‘CONTACT_NAME’.