Sunday, April 16, 2017

Changing Column Names in ALV Display using SALV model (LIST or GRID display)

Data  : alv_table_obj   TYPE REF TO cl_salv_table,
     obj_columns TYPE REF TO cl_salv_columns_table,
     obj_single_column TYPE REF TO cl_salv_column.

CALL METHOD cl_salv_table=>factory
        IMPORTING
          r_salv_table =  alv_table_obj  
        CHANGING
          t_table      = itab_final    "Internal table holding final data to display in report output


*to change the name of the column in ALV.  

obj_columns  =  alv_table_obj->get_columns().

    obj_single_column   =  obj_columns->get_column( 'CARRID' ).

   obj_single_column ->set_long_text ( 'Flight Name' ).

    obj_single_column ->set_medium_text ( 'Flight Name' ).

    obj_single_column ->set_short_text ( 'Flight' ).

obj_single_column->set_optimize().



alv_table_obj ->display( ).    "Method to display internal table  itab_final data as ALV Report(Either list or Grid                                                     based on the parameter value passed to FACTORY method)

Changing Column Names in ALV Grid Display

In a report development using set_table_for_first_display Method of  CL_GUI_ALV_GRID class.
 I am required to over write the text given in the data elements for all the fields and display customized text, for the ALV column texts.When we display the report , by default we will get the field label text (DDIC Objet )assigned to that field in report output.
Here in this post i am going to explain you, how to customize the column headers.


FM to generate  Field catalog :


FM Signature:

CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'

EXPORTING

I_STRUCTURE_NAME = 'ZSTRUCT'    "DDIC object

CHANGING

CT_FIELDCAT = ITAB_FIELDCAT  "Field catalog ref to  lvc_s_fcat

EXCEPTIONS

INCONSISTENT_INTERFACE = 1

PROGRAM_ERROR = 2

OTHERS = 3.

Changing the default display text with customized one.

LOOP AT  itab_fieldcat ASSIGNING <fs_fieldcat>.    "Fied symbol type  lvc_s_fcat
 IF <fs_fieldcat>-fieldname = 'CARRID'.

<fs_fieldcat>-reptext       =  'Flight'.                  
<fs_fieldcat>-scrtext_l     =  'Flight Name'.
<fs_fieldcat>-scrtext_m   =  'Flight Name'.
<fs_fieldcat>-scrtext_s    =  'Flight'.
<fs_fieldcat>-outputlen    =  10.

ENDIF.


Method to display report output

CALL METHOD obj_grid->set_table_for_first_display
            EXPORTING i_structure_name = 'ZSTRUCTURE'    "Global Struct
                                    is_layout               =  wa_layout             " Layout
            CHANGING
                                  it_outtab       =  itab_output            "Final Internal Table refer to struct
                                  it_fieldcatalog  =   itab_fieldcatalog            "Field catalog 




If you run the above report ...

For CARRID column you will get  <fs_fieldcat>-scrtext_s text i.e  'Flight'  or  Medium or Long based the length of Column output value .

If you did n't customize the column headers , you will get ID as column name in Short , in other cases Medium and Long ... it will change according to Field label of CARRID data element.


Note 1 : In order to get customized texts in report output , Short Medium and Long texts should be there.if not,you will get DDIC Reference texts only in output.

Note 2 : If Short text presents , you will get that in output. if not, reptext will get display in output.