Skip to Content
-1
Dec 13, 2020 at 01:48 PM

Issue on Displaying Column Header Text of Dynamic Internal Table SALV

88 Views

Hello,

I am having difficulty in Getting Column Header Text for Dynamic Internal Table using SALV,

I could able to Display the Field Name as Column Header Text, but NOT the Description,

Appreciate your Help,

Report z_sg_01.

TYPE-POOLS : icon, vrm, abap.
TYPES : BEGIN OF gty_ztable,
         tabname TYPE tabname,
        END OF gty_ztable.

DATA : gt_list             TYPE vrm_values,
       gwa_list            TYPE vrm_value,
       gt_ztable           TYPE TABLE OF gty_ztable,
       gwa_ztable          LIKE LINE OF gt_ztable,
       gt_values           TYPE TABLE OF dynpread,
       gwa_values          TYPE dynpread.

DATA : t100                TYPE t100,
       gv_id               TYPE vrm_id.

FIELD-SYMBOLS: <dyn_table> TYPE STANDARD TABLE,
               <dyn_wa>,
               <dyn_field>.

DATA:          dy_table    TYPE REF TO data,
               dy_line     TYPE REF TO data,
               gwa_fc      TYPE lvc_s_fcat,
               gt_fc       TYPE lvc_t_fcat.

DATA : lo_data             TYPE REF TO data.

CONSTANTS : gc_x(1)        TYPE c VALUE 'X',
            gc_arbgb       TYPE arbgb   VALUE 'ZV_MSG_FILE_PROC'.

PARAMETERS :     pr_table    TYPE dd02l-tabname AS LISTBOX VISIBLE LENGTH 30. 

AT SELECTION-SCREEN ON pr_table.
  PERFORM get_table_name.

*&---------------------------------------------------------------------*
*& Initialization
*&---------------------------------------------------------------------*
INITIALIZATION.

  CLEAR : gwa_list, gwa_ztable, gwa_fc. " gwa_tab_field.
  FREE  : gt_list, gt_ztable, gt_fc. " git_tab_field.

  SELECT tabname FROM dd02l INTO TABLE gt_ztable WHERE tabname LIKE 'Z%'.

  gv_id = 'PR_TABLE'.

  LOOP AT gt_ztable INTO gwa_ztable.
    MOVE sy-tabix TO gwa_list-key.
    CONDENSE gwa_list-key.
    gwa_list-text = gwa_ztable-tabname.
    APPEND gwa_list TO gt_list.
    CLEAR : gwa_list, gwa_ztable.
  ENDLOOP.

  CALL FUNCTION 'VRM_SET_VALUES'
    EXPORTING
      id              = 'PR_TABLE'
      values          = gt_list
    EXCEPTIONS
      id_illegal_name = 1
      OTHERS          = 2.

*----------------------------------------------------------------------*
*       CLASS lcl_report DEFINITION
*----------------------------------------------------------------------*
CLASS lcl_report DEFINITION.
  PUBLIC SECTION.
    DATA: o_alv TYPE REF TO cl_salv_table.

    METHODS:
      get_data,
      generate_output.

  PRIVATE SECTION.
    METHODS:
      set_pf_status
        CHANGING
          co_alv TYPE REF TO cl_salv_table.

    METHODS:
     set_layout
      CHANGING
        co_alv TYPE REF TO cl_salv_table.

ENDCLASS.                    "lcl_report DEFINITION


START-OF-SELECTION.
  PERFORM get_structure.
  PERFORM create_dynamic_itab.

    DATA: lo_report TYPE REF TO lcl_report.

    CREATE OBJECT lo_report.
    lo_report->get_data( ).
    lo_report->generate_output( ).
END-OF-SELECTION.

*&---------------------------------------------------------------------*
*&      Form  GET_TABLE_NAME
*&---------------------------------------------------------------------*
*       Select Table Name from Search Help
*----------------------------------------------------------------------*
FORM get_table_name .
  CLEAR   : gwa_values, gt_values.
  REFRESH : gt_values.
  gwa_values-fieldname = gv_id.  " 'PR_TABLE'.
  APPEND gwa_values TO gt_values.

  CALL FUNCTION 'DYNP_VALUES_READ'
    EXPORTING
      dyname             = sy-cprog    " sy-repid
      dynumb             = sy-dynnr
      translate_to_upper = space    " 'X'
    TABLES
      dynpfields         = gt_values.

  READ TABLE gt_values INDEX 1 INTO gwa_values.
  IF sy-subrc = 0 AND gwa_values-fieldvalue IS NOT INITIAL.
    READ TABLE gt_list INTO gwa_list
                      WITH KEY key = gwa_values-fieldvalue.
    IF sy-subrc = 0.
      pr_table = gwa_list-text.
    ENDIF.
  ENDIF.
ENDFORM.                    " GET_TABLE_NAME
*&---------------------------------------------------------------------*
*&      Form  get_structure
*&---------------------------------------------------------------------*
FORM get_structure.
  DATA : lt_details  TYPE abap_compdescr_tab,
         lwa_details TYPE abap_compdescr.
  DATA : ref_table_des TYPE REF TO cl_abap_structdescr.

  ref_table_des ?=
      cl_abap_typedescr=>describe_by_name( pr_table ).
  lt_details[] = ref_table_des->components[].

  LOOP AT lt_details INTO lwa_details.
    CLEAR gwa_fc.
    gwa_fc-fieldname = lwa_details-name.

    CASE lwa_details-type_kind.
      WHEN 'C'.
        gwa_fc-datatype = 'CHAR'.
      WHEN 'N'.
        gwa_fc-datatype = 'NUMC'.
      WHEN 'D'.
        gwa_fc-datatype = 'DATE'.
      WHEN 'P'.
        gwa_fc-datatype = 'PACK'.
      WHEN OTHERS.
        gwa_fc-datatype = lwa_details-type_kind.
    ENDCASE.

    gwa_fc-inttype  = lwa_details-type_kind.
    gwa_fc-intlen   = lwa_details-length.
    gwa_fc-decimals = lwa_details-decimals.
    APPEND gwa_fc TO gt_fc.
    CLEAR : lwa_details.
  ENDLOOP.
ENDFORM.                    "get_structure

*&---------------------------------------------------------------------*
*&      Form  create_dynamic_itab
*&---------------------------------------------------------------------*
FORM create_dynamic_itab.
  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog  = gt_fc
      i_length_in_byte = 'X'
    IMPORTING
      ep_table         = dy_table.
  ASSIGN dy_table->* TO <dyn_table>.

  CREATE DATA dy_line LIKE LINE OF <dyn_table>.
  ASSIGN dy_line->* TO <dyn_wa>.
ENDFORM.                    "create_dynamic_itab

*----------------------------------------------------------------------*
*       CLASS lcl_report IMPLEMENTATION
*----------------------------------------------------------------------*
CLASS lcl_report IMPLEMENTATION.
  METHOD get_data.
    SELECT *
           INTO  TABLE <dyn_table>
           FROM  (pr_table).
  ENDMETHOD.                    "get_data


  METHOD generate_output.
    DATA: lx_msg TYPE REF TO cx_salv_msg.
    TRY.
        cl_salv_table=>factory(
          IMPORTING
            r_salv_table = o_alv
          CHANGING
            t_table      =  <dyn_table> ).
      CATCH cx_salv_msg INTO lx_msg.
    ENDTRY.

    DATA: lr_functions TYPE REF TO cl_salv_functions_list.

    o_alv->get_display_settings( )->set_striped_pattern( abap_true ).
    lr_functions = o_alv->get_functions( ).
    lr_functions->set_default( abap_true ).

    DATA: lr_columns   TYPE REF TO cl_salv_columns.
    lr_columns = o_alv->get_columns( ).
    lr_columns->set_optimize( abap_true ).

    PERFORM set_column_names USING lr_columns.

    CALL METHOD set_pf_status
      CHANGING
        co_alv = o_alv.

    CALL METHOD set_layout
      CHANGING
        co_alv = o_alv.

    o_alv->display( ).

  ENDMETHOD.                    "generate_output


  METHOD set_pf_status.
    DATA: lo_functions TYPE REF TO cl_salv_functions_list.
    lo_functions = co_alv->get_functions( ).
    lo_functions->set_default( abap_true ).
  ENDMETHOD.                    "set_pf_status



  METHOD set_layout.
    DATA: lo_layout  TYPE REF TO cl_salv_layout,
          lf_variant TYPE slis_vari,
          ls_key    TYPE salv_s_layout_key.

    lo_layout = co_alv->get_layout( ).
    ls_key-report = sy-repid.

    lo_layout->set_key( ls_key ).
    lo_layout->set_save_restriction( if_salv_c_layout=>restrict_none ).

    lf_variant = 'DEFAULT'.
    lo_layout->set_initial_layout( lf_variant ).
  ENDMETHOD.                    "set_layout
ENDCLASS.                    "lcl_report IMPLEMENTATION

*&---------------------------------------------------------------------*
*&      Form  SET_COLUMN_NAMES
*&---------------------------------------------------------------------*
*       ALV Column Text
*----------------------------------------------------------------------*
FORM set_column_names
  USING ir_columns TYPE REF TO cl_salv_columns.

  DATA: lr_column   TYPE REF TO cl_salv_column,
        lt_column   TYPE salv_t_column_ref,
        ls_column   TYPE salv_s_column_ref,
        lv_coltxt   TYPE scrtext_m.

  lt_column  = ir_columns->get( ).

  LOOP AT lt_column INTO ls_column.
    lv_coltxt = ls_column-columnname.
    REPLACE ALL OCCURRENCES OF '_' IN lv_coltxt WITH ` `.
    ls_column-r_column->set_medium_text( lv_coltxt ).  
  ENDLOOP.
ENDFORM.                    " set_column_names