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

How to display color for each row in SAP ALV List?

The old SAP ALV Display facilitates the user to color the cells, lines of record, columns etc. Every color is uniquely identified through a colour code. The following table illustrates the different colour codes.

Type of ColorCode
BlueC110
YellowC310
OrangeC711
GreenC510
RedC610
fig. Table of Colour Codes.

There are three important steps involved in colouring a line of record.

  1. Extend the internal table with the field (colour code) of Type C
  2. Set the value of field ‘info_fname’ of the layout.
  3. Assign the colour code to each line of record.
  • Extension of Internal table.

In order to colour the complete line of record, a new field must be added. This field is of Data type c. The field holds the colour code of the particular line of record.

TYPES: BEGIN OF ty_contact.

              INCLUDE STRUCTURE ztt_db_table2.

TYPES: line_of_record (4) TYPE c,

              END OF ty_contact.

DATA lt_contact TYPE TABLE OF ty_contact.

  • ASSIGNMENT of field INFO_FNAME in the layout.

The ALV List can be able to identify the newly added field (like above) through the field ‘INFO_FNAME’ of layout.

The Assignment is as shown below.

DATA ls_layout TYPE lvc_s_layo.

ls_layout-info_fname = ‘LINE_OF_RECORD’.

Please remember that the value assigned must be in capital letters. Otherwise it will not be able to identify correctly.

  • ASSIGNMENT OF COLOUR CODE TO EACH LINE OF RECORD.

The last and final step involved is to colour each line of record with the unique colour code. Each line of record is read through ‘LOOP AT’ loop. The Rows can be filtered through additional constraints. Let us look at the following example.

FIELD-SYMBOLS <ls_contact> TYPE ty_contact.

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

<ls_contact> – line_of_record = ‘C310’.

ENDLOOP.

              As shown in above code, each line of records in SAP ALV List is set with the colour code ‘C310’. This colour code indicates the colour ‘YELLOW’.

How to set the cell type as hotspots or Links in SAP ALV Display

The cell type of CL_GUI_ALV_GRID->MC_STYLE_HOTSPOT enables the user to navigate to a new page or session. It can also open a new window or display a list. It appears as a Link in the SAP ALV List.

The following steps are necessary to implement the HOTSPOT / LINKS

  1. Extent the Internal table with the additional field of type lvc_t_styl.
  2. Set the fields fieldname, style of lvc_t_Styl to each row of the ALV List. The column is specified in the fieldname.
  3. The stylename field of the layout is assigned with the fieldstyle component.

  • Extension of Internal table with Fieldstyle component

TYPES: BEGIN OF ty_contact_link.

              INCLUDE STRUCTURE ztt_db_table2.

TYPES: fieldstyle TYPE lvc_t_styl,

              END OF ty_contact_link.

  • Set the column in the SAP ALV List with the field style ‘CL_GUI_ALV_GRID=>mc_style_hotspot’.

DATA lt_contact TYPE TABLE OF ty_contact_link.

DATA ls_style TYPE lvc_s_styl.

FIELD SYMBOLS <ls_contact> TYPE ty_contact_link.

LOOP AT lt_contact ASSIGNING <ls_contact>.

ls_style-fieldname = ‘CONTACT_NAME’.

ls_style-style = cl_gui_alv_grid => mc_style_hotspot.

APPEND ls_style TO <ls_contact>-fieldstyle.

ENDLOOP

  • Setting the stylename field of the layout.

ls_layout-stylename = ’FIELDSTYLE’.

Note: The hotspot can be activated throught the field HOTSPOT of field catalog. The field HOTSPOT of the field catalog is set to ‘X’. The key advantage of this field is that the user does not have to add any additional field to the Internal table.

How to set the cell type as button in the SAP ALV List?

Button: The cell type of cl_gui_alv_grid=>mc_style_button resembles as a button in the user interface of the SAP ALV list. As the user clicks this button, any event can be executed like opening up a new window.

Dropdown List: It is used to provide a list of values as a dropdown list.

IMPLEMENTATION OF BUTTON:

              The following steps are necessary to implement the Button

  1. Extend the Internal table with the additional field of type LVC_T_STYL.
  2. Set the fields fieldname, style of lvc_t_styl to each row of the ALV List. The column is specified in the fieldname.
  3. The stylename field of the layout is assigned with the field style component.
  • Extension of Internal table with Fieldstyle component.

TYPES: BEGIN OF ty_contact.

              INCLUDE STRUCTURE ztt_db_table2.

TYPES: fieldstyle TYPE lvc_t_styl.

              END OF ty_contact.

  • Set the column in the SAP ALV List with the field style ‘cl_gui_alv_grid=>mc_style_button’.

DATA lt_contact TYPE TABLE OF ty_contact.

DATA ls_style TYPE lvc_s_styl.

FIELD_SYMBOLS <ls_contact> TYPE ty_contact.

LOOP AT lt_contact ASSIGNING <ls_contact>.

              ls_style-fieldname = ‘CONTACT_NAME’.

              ls_style-style = cl_gui_Alv_grid =>mc_style_button.

              APPEND ls_style TO <ls_contact>-fieldstyle.

ENDLOOP.

  • Setting the stylename field of the layout.

ls_layout-stylename = ’FIELDSTYLE’.

How to set the cell of the ALV List as a Dropdown list in SAP ABAP?

It is used to provide a list of values as a Drop down List.

There are three important steps involved in setting the cell as a drop down list.

  1. Filling the internal table lvc_t_dral.
  2. Setting the fields drdn_hndl, drdn_alias of field catalog.
  3. Call the method set_drop_down_table of the ALV object.
  • Filling the internal table lvc_t_dral.

Handle: The field Handle holds the value, which can be used to refer to the value mentioned in the field drdn_hndl of field catalog. Therefore it connects to the field catalog.

Value: It is considered as the Display name. It holds the value to be used in the Drop down list. If the field DRDN_ALIAS of the field catalog is initial, then this value is considered for the Drop down list.

INT_VALUE: It is considered as the Technical name. It also holds the value to be displayed in the Drop down List. If the field DRDN_ALIAS of the field catalog is ‘X’, then the value from the int_value field is used in the Drop down list.

Let us consider the following example:

DATA lt_dral TYPE lvc_t_dral.

DATA ls_dral TYPE lvc_s_dral.

ls_dral-handle = 3.

ls_dral-value = ‘Street1_value’.

ls_dral-int_value = ‘Street1_int_value’.

APPEND ls_dral TO lt_dral.

ls_dral-value = ‘Street2_value’.

ls_dral-int_value = ‘Street2_int_value’.

APPEND ls_dral TO lt_dral.

  • Setting the ‘DRDN’ fields of Field Catalog.

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> – drdn_hndl = 3.

<ls_field_cat> – drdn_alias = ‘X’.

ENDLOOP.

              In the above field catalog assignment, the field ‘DRDN_ALIAS’ is set to ‘X’, which means the value of field ‘INT_VALUE’ is considered in the Drop down list. The value of field ‘VALUE’ is ignored. If the field ‘DRDN_ALIAS’ is initial like <ls_field_cat> – drdn_alias = ‘ ‘, Then the value of field ‘VALUE’ is considered in the Drop down list.

  • Passing the internal table values lvc_t_dral to the ALV object.

The method set_drop_down_table of the ALV object is used to set the drop down values (Internal table) to the partical column ‘CONTACT_ADDRESS’ of the ALV List.

CALL METHOD lr_gui_alv_grid -> set_drop_down_table(

              EXPORTING

                            it_drop_down_alias = lt_dral ).

The above method call must happen before the method ‘SET_TABLE_FOR_FIRST_DISPLAY’ is called.