SAP ABAP ALV: LVC_FIELDCATALOG_MERGE how to use?

Understanding Field Catalog in ALV (ABAP List Viewer)

In ABAP, the field catalog is a key element when working with ALV (ABAP List Viewer) to define how the data from internal tables is displayed. It provides control over various properties of the fields displayed in the ALV, such as column headers, alignment, visibility, editability, etc. Key Concepts:

Field Catalog: This is a table (usually of type lvc_t_fcat for modern ALV grid or slis_t_fieldcat_alv for old ALV grid) that contains metadata about each field of the internal table displayed in the ALV. It defines how each column should appear (e.g., column headers, field alignment, output length).

Creating a Field Catalog: You can manually define a field catalog or use a function module like LVC_FIELDCATALOG_MERGE to automatically create one based on a data dictionary structure (DDIC). After generating the catalog, you can modify it to adjust field properties.

Understanding Field Catalog in ALV (ABAP List Viewer)

In ABAP, the field catalog is a key element when working with ALV (ABAP List Viewer) to define how the data from internal tables is displayed. It provides control over various properties of the fields displayed in the ALV, such as column headers, alignment, visibility, editability, etc.
Key Concepts:

  Field Catalog: This is a table (usually of type lvc_t_fcat for modern ALV grid or slis_t_fieldcat_alv for old ALV grid) that contains metadata about each field of the internal table displayed in the ALV. It defines how each column should appear (e.g., column headers, field alignment, output length).

  Creating a Field Catalog: You can manually define a field catalog or use a function module like LVC_FIELDCATALOG_MERGE to automatically create one based on a data dictionary structure (DDIC). After generating the catalog, you can modify it to adjust field properties.

Field catalog

All fields of the global structure type appears in the data table with the same name. Now the requirement is to introduce changes to the dictionary structure or the additional columns are to be displayed.

In this case we can call a function module ( LVC_FIELDCATALOG_MERGE ) that returns the field catalog of the dictionary structure to a table of type lvc_t_fcat.
Now we can loop it and can make whatever changes we want.

  1. The REUSE_*ALV* function modules are unsupported. I’d suggest switching to the CL_SALV_* classes. The documentation is better, there are more sample programs (DEMO_SALV_*) and you get support.
  2. You need a dictionary structure if you want to get dictionary-based field descriptions (duh). If you assemble a structure type on the ABAP level using TYPE ... BEGIN OF ... END OF ..., as far as I know, the dictionary types for the individual fields are converted to ABAP types first and only then assembled into a structure type. Anyway, the dictionary reference of the original fields is lost. Instead of defining the structure of the output table in your code, use a dictionary structure.

There are several different text components provided by structure slis_fieldcat_alv that are used as column labels. The chosen text depends on the current column width (which itself usually depends on the length of the data displayed). Make sure that you change them all accordingly!

The usual technique is: By passing the I_STRUCTURE_NAME, you get a field catalog corresponding to this DDIC structure (the changing parameter ct_fieldcat). You then modify this internal table according to your needs and pass that modified table to the REUSE_ALV_GRID_DISPLAY.

In cases where I don’t distinguish the different-sized text versions, I use the following macros to set all the text fields to the same value.
define set_field.

data:
lt_fcat type lvc_t_fcat.
data:
tabname type ref to data,
tabline type ref to data.

field-symbols:
<gt> type standard table,
<gs> type any.

call function 'LVC_FIELDCATALOG_MERGE'
exporting
i_structure_name = 'DDIC_STRUCTURE'
changing
ct_fieldcat = lt_fcat
exceptions
inconsistent_interface = 1
program_error = 2
others = 3.

    Creating table
    call method cl_alv_table_create=>create_dynamic_table
    exporting
    it_fieldcatalog = lt_fcat
    importing
    ep_table = tabname.

assign tabname->* to <gt>.
create data tabline like line of <gt>.
assign tabline->* to <gs>.
Report zalv. 
DATA: t_fcat TYPE lvc_t_fcat. " INTERNAL TABLE
FIELD-SYMBOLS:  TYPE lvc_s_fcat.
.
.
.
  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
    EXPORTING
      i_structure_name       = 'SFLIGHT'
    CHANGING
      ct_fieldcat            = t_fcat
    EXCEPTIONS
      inconsistent_interface = 1
      program_error          = 2
      OTHERS                 = 3.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

  LOOP AT t_fcat ASSIGNING .
    CASE -fieldname .
      WHEN 'PLANETYPE'.
        -coltext = 'PLANE'.
      WHEN 'SEATSMAX'.
        -no_out = 'X'.
    ENDCASE.
  ENDLOOP.
.
.
.

REPORT zalv.
TYPES:
  BEGIN OF stu,
    carrid TYPE sflight-carrid,
    connid TYPE sflight-connid,
    fldate TYPE sflight-fldate,
    w_check,
  END OF stu.
DATA:
fs_itab TYPE stu,                 " INTERNAL TABLE
t_itab LIKE TABLE OF fs_itab.     " WORK AREA
DATA:
r_grid TYPE REF TO cl_gui_alv_grid,
r_container TYPE REF TO cl_gui_custom_container.
DATA:
t_fcat TYPE lvc_t_fcat,
wa_fcat TYPE lvc_s_fcat.
SELECT carrid connid fldate FROM
sflight INTO CORRESPONDING FIELDS OF TABLE t_itab.
CALL SCREEN 100.
*&---------------------------------------------------------------------
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------
MODULE status_0100 OUTPUT.
  SET PF-STATUS 'SCREEN'.
  SET TITLEBAR 'TITLE'.
ENDMODULE. " STATUS_0100 OUTPUT
*&---------------------------------------------------------------------
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------
*text
*--------------------------------------------------------------------
MODULE user_command_0100 INPUT.
  CASE sy-ucomm.
    WHEN 'BACK'.
      LEAVE TO SCREEN 0.
  ENDCASE.
ENDMODULE. " USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------
*& Module SET_HANDLER OUTPUT
*&-------------------------------------------------------------------
*text
*----------------------------------------------------------------------
MODULE set_handler OUTPUT.
  CREATE OBJECT r_container
    EXPORTING
      container_name = 'CONTAINER'.
  CREATE OBJECT r_grid
    EXPORTING
      i_parent = r_container.
*POPULATING THE FIELD CATALOG.
  wa_fcat-fieldname = 'W_CHECK'.
  wa_fcat-coltext = 'CHECK'.
  wa_fcat-checkbox = 'X'.
  wa_fcat-edit = 'X'.
  wa_fcat-col_pos = 1.
  APPEND wa_fcat TO t_fcat.
  CLEAR wa_fcat.
  wa_fcat-fieldname = 'CARRID'.
  wa_fcat-ref_table = 'SFLIGHT'.
  wa_fcat-ref_field = 'CARRID'.
  wa_fcat-col_pos = 2.
  APPEND wa_fcat TO t_fcat.
  CLEAR wa_fcat.
  wa_fcat-fieldname = 'CONNID'.
  wa_fcat-ref_table = 'SFLIGHT'.
  wa_fcat-ref_field = 'CONNID'.
  wa_fcat-col_pos = 3.
  APPEND wa_fcat TO t_fcat.
  CLEAR wa_fcat.
  CALL METHOD r_grid->set_table_for_first_display
    CHANGING
      it_fieldcatalog = t_fcat
      it_outtab       = t_itab.
ENDMODULE. " SET_HANDLER OUTPUT

Links