Skip to Content
Technical Articles

Set ALV column name from data element or internal table component

This is a short blog. The purpose is to provide ABAP code to set name from other data element or internal table component using CL_SALV_TABLE=>FACTORY. Enjoy!

*&---------------------------------------------------------------------*
*& Report ZALV_COLUMN_NAMES
*&---------------------------------------------------------------------*

*https://answers.sap.com/questions/5640634/how-to-get-fieldnames-of-internal-table.html
*http://abapmentor.expertise-team.com/post/display-alv-list-easily-in-abap-using-cl-salv-table-part-i-simple-list.aspx

REPORT ZALV_COLUMN_NAMES.

TYPES: BEGIN OF TYP_ITAB,
         AAA(3) TYPE C,
         BBB TYPE I,
         CCC TYPE P LENGTH 3 DECIMALS 2,
         ZYIN TYPE GJAHR,
         ZRYE TYPE RYEA1,
       END OF TYP_ITAB.

DATA: I_ITAB TYPE TABLE OF TYP_ITAB.
DATA: L_ITAB LIKE LINE OF I_ITAB.

DATA: L_TABLEDESCR_REF TYPE REF TO CL_ABAP_TABLEDESCR,
      L_DESCR_REF      TYPE REF TO CL_ABAP_STRUCTDESCR,
      WA_TABLE TYPE ABAP_COMPDESCR.

DATA: GR_TABLE TYPE REF TO CL_SALV_TABLE.
DATA: GR_COLUMNS TYPE REF TO CL_SALV_COLUMNS_TABLE.
DATA: GR_COLUMN TYPE REF TO CL_SALV_COLUMN_TABLE.
DATA: _SCRTEXT_L TYPE SCRTEXT_L,
      _SCRTEXT_M TYPE SCRTEXT_M,
      _SCRTEXT_S TYPE SCRTEXT_S.

DATA: L_T_DD04T LIKE DD04T OCCURS 0,
      L_ROLL_NAME LIKE DD04T-ROLLNAME,
      L_S_DD04T LIKE DD04T.

L_ITAB-AAA = 'ABC'. L_ITAB-BBB = 1000. L_ITAB-CCC = '12.34'. L_ITAB-ZYIN = 2020. L_ITAB-ZRYE = 2021.
APPEND L_ITAB TO I_ITAB.

CL_SALV_TABLE=>FACTORY( IMPORTING R_SALV_TABLE = GR_TABLE CHANGING T_TABLE = I_ITAB ).

GR_COLUMNS = GR_TABLE->GET_COLUMNS( ).

L_TABLEDESCR_REF ?= CL_ABAP_TYPEDESCR=>DESCRIBE_BY_DATA( I_ITAB ).
L_DESCR_REF ?= L_TABLEDESCR_REF->GET_TABLE_LINE_TYPE( ).

  LOOP AT L_DESCR_REF->COMPONENTS INTO WA_TABLE.
    CLEAR: _SCRTEXT_S, _SCRTEXT_M, _SCRTEXT_L.

    IF WA_TABLE-NAME = 'ZRYE'. "SET FROM DICTIONARY RYEA1
       CONTINUE.
    ENDIF.

    IF WA_TABLE-NAME = 'AAA' OR "SET FROM COMPONENT OF INTERNAL TABLE
       WA_TABLE-NAME = 'BBB' OR
       WA_TABLE-NAME = 'CCC'.
       _SCRTEXT_L = WA_TABLE-NAME.
       _SCRTEXT_M = WA_TABLE-NAME.
       _SCRTEXT_S = WA_TABLE-NAME.
    ENDIF.

    IF WA_TABLE-NAME = 'ZYIN'. "SET FROM DATA ELEMENT BT_YR
       L_ROLL_NAME =   'BT_YR'.
       PERFORM F_ROLLNAME USING L_ROLL_NAME CHANGING L_S_DD04T.
      _SCRTEXT_L = L_S_DD04T-SCRTEXT_L.
      _SCRTEXT_M = L_S_DD04T-SCRTEXT_M.
      _SCRTEXT_S = L_S_DD04T-SCRTEXT_S.
    ENDIF.

    GR_COLUMN ?= GR_COLUMNS->GET_COLUMN( WA_TABLE-NAME ).
    GR_COLUMN->SET_SHORT_TEXT( _SCRTEXT_S ).
    GR_COLUMN->SET_MEDIUM_TEXT( _SCRTEXT_M ).
    GR_COLUMN->SET_LONG_TEXT( _SCRTEXT_L ).
  ENDLOOP.

GR_TABLE->DISPLAY( ).

FORM F_ROLLNAME USING L_ROLL_NAME CHANGING L_S_DD04T.
CALL FUNCTION 'DD_DTEL_GET'
     EXPORTING
          LANGU         = SY-LANGU
          WITHTEXT      = 'X'
          ROLL_NAME     = L_ROLL_NAME
     TABLES
          DD04T_TAB_A   = L_T_DD04T
     EXCEPTIONS
          ILLEGAL_VALUE = 1
          OTHERS        = 2.

READ TABLE L_T_DD04T INTO L_S_DD04T WITH KEY ROLLNAME = L_ROLL_NAME.

ENDFORM.

Output:

/
Be the first to leave a comment
You must be Logged on to comment or reply to a post.