How to do Sorting and grouping in ALV Table through CL_SALV_SORTS

The sorting and grouping of data in ALV Table can be programmatically achieved. The method add_sort( ) of the class CL_SALV_SORTS can be used to sort or group the data. This sorting object is created from the get_sorts( ) method of the ALV Object.

DATA lr_salv_sorts TYPE REF TO cl_salv_sorts.

Lr_salv_sorts = lr_salv_table->get_sorts( ).

Lr_salv_sorts->add_sort( EXPORTING columnname = ‘AGE’

                                                          sequence       = if_salv_c_sort=>sort_up ).

The following sorting and grouping options are available.

If_salv_c_sort=>sort_none.

If_salv_c_sort=>sort_up.

If_salv_c_sort=>sort_down.

If_salv_c_sort=>group_with_underline.

If_salv_c_sort=>group_with_newpage.

If_salv_c_sort=>group_none.

How to do Aggregation using the class CL_SALV_AGGREGATION

The column of the ALV Display can be aggregated using the add_aggregation( ) method of the class CL_SALV_AGGREGATION.

DATA lr_salv_aggregation TYPE REF TO cl_salv_aggregation.

Lr_salv_aggregation = lr_salv_table=>get_aggregations( ).

Lr_salv_aggregation->set_aggregation_before_items( abap_true ).

Lr_salv_aggregation->add_aggregation( columnname = ‘AGE’

                                                                aggregation  =  if_salv_c_aggregation=>average ).

The following aggregation types are available.

If_salv_c_aggregation=>minimum.

If_salv_c_aggregation=>maximum.

If_salv_c_aggregation=>average.

If_salv_c_aggregation=>total.

If_salv_c_aggregation=>none.

How to fill icons in SAP ALV Display of type CL_SALV_TABLE

In order to fill icons in ALV Table, we must add an additional field of type ICON_D. The type ICON_D is of datatype CHAR with length 4. This is later filled with the icon code.

Step1: Define the icon field

TYPE-POOLS: icon.

TYPES: BEGIN OF ty_contact.

                  INCLUDE STRUCTURE ztt_db_table2.

                  TYPES: icon_field TYPE icon_d,

               END OF ty_contact.

Step2: Fill the internal table with data contents.

DATA lt_contact TYPE TABLE OF ty_contact.

SELECT * FROM ztt_db_table2 INTO TABLE lt_contact.

Step3: Fill the internal table with icon code.

FIELD-SYMBOLS <ls_contact> TYPE ty_contact.

LOOP AT lt_contact ASSIGNING <ls_contact>.

               <ls_contact>-icon = icon_positive.

ENDLOOP.

How to color the cells of SAP ALV Display of type CL_SALV_COLUMNS_TABLE

Step 1:

Desired cells of the SAP ALV Display can be colored using the structure lvc_s_scol. The color codes must be set to each line of record. Therefore the structure of the ALV Display must be extended with the additional field with an internal table of type lvc_t_scol.

TYPES: BEGIN OF ty_contact.

              INCLUDE STRUCTURE ztt_db_table2.

              TYPES: color_field TYPE lvc_t_scol,

              END OF ty_contact.

DATA ls_color_field TYPE lvc_s_scol.

DATA lr_all_columns TYPE REF TO cl_salv_columns_table.

DATA lt_contact TYPE TABLE OF ty_contact.

SELECT * FROM ztt_db_table2 INTO TABLE lt_contact.

Lr_all_columns = lr_salv_table->get_columns( ).

Step2:

Make the ALV Table recognize the newly added ‘color_field’ with the help of the method set_color_column( ) of class CL_SALV_COLUMNS_TABLE.

Lr_all_columns->set_color_column( ‘COLOR_FIELD’ ).

Step3:

Finally fill the internal table ( new field ) with the desired color codes.

DATA ls_contact TYPE ztt_db_table2.

LOOP AT lt_contact ASSIGNING ls_contact WHERE age > 30.

              Ls_color_field-fname = ‘CONTACT_ADDRESS’.

              Ls_color_field-color-col = 7.

              Ls_color_field-color-int = 1.

              Ls_color_field-color-inv = 1.

              APPEND ls_color_field TO ls_contact-color_field.

ENDLOOP.

How to color the complete column of the SAP ALV Display of type CL_SALV_COLUMN_TABLE

The method set_color of the class CL_SALV_COLUMN_TABLE can be used to color the column of SAP ALV Display.

The color codes are much similar to old ALV Display of type CL_GUI_ALV_GRID

DATA lr_all_columns TYPE REF TO cl_salv_columns_table.

DATA lr_single_column TYPE REF TO cl_salv_column_table.

Lr_all_columns = lr_salv_table->get_columns( ).

Lr_single_column ?= lr_all_columns->get_column(‘CONTACT_NAME’).

DATA ls_color_column TYPE lvc_s_scol.

Ls_color_column-col = 7.

Ls_color_column-int = 1.

Ls_color_column-inv = 1.

Lr_single_column->set_color( value = ls_color_column ).

The column ‘Contact_name’ is colored or highlighted in orange colour.

The following table shows the other different colour codes. It is same as the old ALV Display.

Colorcolor_codecolintinv
GreenC510510
RedC610610
OrangeC711711
Color Codes

What is the difference between OOP and Functional programming

ParameterObject Oriented ProgrammingFunctional Programming
DefinitionA programming paradigm based on the concept of objects which contains data in the form of fields called attributes and code in the form of methodsA programming paradigm based on the concept of procedure calls
Paradigm
ApproachBottom-up ApproachTop-down Approach
Data ControlIn OOP data in each function is controlled on its ownIn POP every function has different data so no control over it.
EmphasisEmphasis on objectsEmphasis on functions
CommunicationObjects communicate with each other by message passingParameters communicate with each other by parameter passing
InheritanceInheritance is supported by three modes : private, protected and publicInheritance is not supported
Access ControlAccess control is done with access modifiersAccess modifiers is not supported
Data HidingData can be hidden is using EncapsulationNo data hiding. Data is accessible globally
Overloading or PolymorphismOverloading functions, constructors and operators are possibleOverloading is not possible
SecurityMore secured language because external functions can’t access another data.Due to global data, it is less secured language
Code reusabilityExisting code can be reusedNo code reusability
Problem SolvingUsed for solving big problemsNot used for solving big problems
ExampleC++, Java,C#, PHP, PythonCOBOL, C, FORTRAN, BASIC, PASCAL

What is the difference between check box and check box hotspot cell type in CL_SALV_TABLE?

CheckboxCheckbox Hotspot
It is used just as a display of checkbox. It mentions that the particular line of record is selected.It can be used to trigger an event of type LINK_CLICK.
The value of the checkbox cannot be changed.The value of checkbox can be changed.
It is not used to manipulate the values of the fields of ALV Table.It can be used to manipulate the values of the fields of ALV Table. The ALV Table must be refreshed with the refresh method call {lr_alv_grid -> REFRESH ()}.

How to use various cell types in new SAP ALV Display of type CL_SALV_TABLE?

               The various cell types can be used both in container mode and complete mode. The cell types are constructed based on the Attribute of the Interface ‘IF_SALV_C_CELL_TYPE’.

Type of cellAttribute of IF_SALV_C_CELL_TYPEDisplay mode container / completeEvent
Text=> TextBoth
Checkbox=> CheckboxBoth
Checkbox hotspot=> checkbox_hotspotBothlink_click
Button=> ButtonBoth link_click
Dropdown=> dropdownContainer
Hotspot=> HotspotBoth link_click

The method set_cell_type of the column object ( cl_salv_column_table ) can be used to set the cell type for the cells in the ALV Display.

DATA lr_all_columns TYPE REF TO cl_salv_columns_table.

DATA lr_single_column TYPE REF TO cl_salv_column_table.

lr_all_columns = lr_alv_grid->get_columns ().

lr_single_column ?= lr_all_columns->get_column (‘Contact_name’ ).

lr_single_column->set_cell_type (if_salv_c_cell_type =>Button).

How to optimize the width of the column of type CL_SALV_COLUMNS_TABLE

               The width of the column is optimized through three methods.

  1. Optimize all columns.
  2. Optimize one single column.
  3. Manually set the width of columns.

The optimization is based on the content of the ALV Table.

  1. Optimize all columns.

In order to optimize all the columns of the ALV Table, call the set_optimize method of the columns object.

DATA lr_all_columns TYPE REF TO cl_salv_columns_table.

lr_all_columns = lr_alv_grid->get_columns ( ).

lr_all_columns->set_optimize ( ).

  • Optimize one single column.

‘SET_OPTIMIZED ( )’ method of the cl_salv_column_table is used to optimize the single column of the ALV Table.

DATA lr_all_columns TYPE REF TO cl_salv_columns_table.

DATA lr_single_column TYPE REF TO cl_salv_column_table.

lr_all_columns = lr_alv_grid -> get_columns ( ).

lr_single_column ?= lr_all_columns -> get_column ( ‘Contact_name’ ).

lr_single_column -> set_optimized ( abap_true ).

  • Manual adjustment of column width.

The method set_output_length of the class cl_salv_column_table can be used to set the width of the particular column to a specific predefined value.

DATA lr_all_columns TYPE REF TO cl_salv_columns_table.

DATA lr_single_column TYPE REF TO cl_salv_column_table.

lr_all_columns = lr_alv_grid -> get_columns ( ).

lr_single_column ?= lr_all_columns -> get_column ( ‘Contact_name’ ).

lr_single_column -> set_output_length ( 50 ).

The above manual adjustment of column width will be ignored, if the columns are optimized with the set_optimize ( ) method.

How to explain Method overloading and Method overriding in German language?

Method Overloading – Methoden überladung

               Die Klasse hat mehrere Methoden mit dem selben Namen. Die Methode hat aber unterschiedlicher Signatur. Der Parameter hat verschiedene Datentyp. Die Anzahl der Parameter sind unterschiedlich.

Method Overriding – Überschreiben der Methode                Überschreiben der Methode ist zwischen der Klasse. Die Methode von der übergeordneten Klasse wurde in der untergeordnete Klasse überschrieben. Die überschriebene Methode in untergeordnete Klasse hat die gleiche Signatur. Aber die überschriebene Methode wurde nochmal implementiert. Sie kann ganz andere Inhalt beinhalten. Es ist nicht gleich wie in Obere Klasse.