Calculated fields in ALV
Imagine we want to add few columns in the ALV those which are not part of the table fields. In such a case ALV with IDA provides IF_SALV_IDA_CALC_FIELD_HANDLER can be used. In this demo will add two more calculated columns to the ALV.
The class CL_SALV_GUI_TABLE_IDA – CREATE method can import the reference of the class that implements the interface IF_SALV_IDA_CALC_FIELD_HANDLER.
The interface IF_SALV_IDA_CALC_FIELD_HANDLER provides few methods that helps to add columns & calculate the value for each row of these columns.
Method GET_CALC_FIELD_STRUCTURE can be used to pass the field name to be used as calculated fields.
Method GET_REQUESTED_FIELDS can be used to decide which all column names needed to calculate the values of the calculated columns.
Method CALCULATE_LINE can be used to calculate the value of the calculated columns for each row.
Create a class and implement the interface.
In the TYPES section create a type.
Add the fields. Here we added two fields to be used as calculated fields.
Create an attribute( structure) by referring to the types.
Now we have to implement the methods.
In the method GET_CALC_FIELD_STRUCTURE just use the structure so that it enables which all additional fields are going to be used.
In the method GET_REQUESTED_FIELDS , just inset all those fields which are going to be used to calculate the value of the additional calculated fields.
In the method CALCULATE_LINE which is going to be called for each row in the alv, calculate the value of the additional fields. Other methods of the class just activate.
Create a program and use the above class.
DATA: lr_salv TYPE REF TO if_salv_gui_table_ida,
lo_calc_fld TYPE REF TO zcl_flight_calc_field.
CREATE OBJECT lo_calc_fld.
iv_table_name = ‘SFLIGHT’
io_calc_field_handler = lo_calc_fld
ro_alv_gui_table_ida = lr_salv ).
DATA(lr_fcat) = lr_salv->field_catalog( ).
* Prepare the field catalog for two calculated fields
iv_field_name = ‘FLIGHT_NAME’
iv_header_text = ‘Flight Name’
iv_tooltip_text = ‘Name of the flight’ ).
iv_field_name = ‘FREE_SEATS’
iv_header_text = ‘Free Seats’
iv_tooltip_text = ‘Available Seats’ ).
* Display ALV
lr_salv->fullscreen( )->display( ).