
SAP-ABAP Useful links
DYNAMIC FIELD CATALOG IN ALV ABAP ALV Report
Creating DYNAMIC FIELD CATALOG.
You can change the field catalog during runtime .
To achieve this you can use the method 'get_frontend_fieldcatalog',of class 'cl_gui_alv_grid'.
Step 1.
Create an internal table of type LVC_T_FCAT.
Step 2.
Call method 'get_frontend_fieldcatalog' of class cl_gui_alv_grid and populate the field catalog using the parameter 'et_fieldcatalog' . This parameter will give you the field catalog of the current displayed screen.
Step 3.
Now loop at the field catalog and make the required changes.
Step 4.
Call the method 'set_table_for_first_display' and pass the field catalog.
Example:
In this example ,SLFIGHT details will be displayed first and if you press
the push button on the screen(SWITCH) ,SFLIGHT details will be displayed
with changes like 1. OUTPUT LENGTH OF CONNID WILL BE CHANGED.
2. FIELD PLANETYPE WILL BE HIDDEN.
3. GRAND TOTAL OF FIELD PRICE WILL BE CALCULATED.
REPORT zalv_dynamic_fieldcatalog.
DATA:
t_sflight TYPE TABLE OF sflight,
fs_sflight TYPE sflight.
DATA:
r_container TYPE REF TO cl_gui_custom_container,
r_grid TYPE REF TO cl_gui_alv_grid.
DATA:
t_fcat TYPE lvc_t_fcat,
fs_fcat TYPE lvc_s_fcat.
SELECT * FROM sflight INTO TABLE t_sflight.
CALL SCREEN 100.
*&---------------------------------------------------------------------
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------
MODULE status_0100 OUTPUT.
SET PF-STATUS 'SCREEN'.
SET TITLEBAR 'SFLIGHT DETAILS'.
ENDMODULE. " STATUS_0100 OUTPUT
*&---------------------------------------------------------------------
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------
MODULE user_command_0100 INPUT.
CASE sy-ucomm.
WHEN 'BACK'.
LEAVE TO SCREEN 0.
WHEN 'SWITCH'.
CALL METHOD r_grid->get_frontend_fieldcatalog
IMPORTING
et_fieldcatalog = t_fcat.
LOOP AT t_fcat INTO fs_fcat.
CASE fs_fcat-fieldname.
WHEN 'CONNID'.
fs_fcat-outputlen = '10'.
MODIFY t_fcat FROM fs_fcat.
WHEN 'PLANETYPE'.
fs_fcat-no_out = 'X'.
MODIFY t_fcat FROM fs_fcat.
WHEN 'PRICE'.
fs_fcat-do_sum = 'X'.
MODIFY t_fcat FROM fs_fcat.
ENDCASE.
ENDLOOP.
CALL METHOD r_grid->set_table_for_first_display
CHANGING
it_fieldcatalog = t_fcat
it_outtab = t_sflight.
ENDCASE.
ENDMODULE. "user_command_0100 INPUT
*&---------------------------------------------------------------------
*& Module list OUTPUT
*&---------------------------------------------------------------------
MODULE list OUTPUT.
CREATE OBJECT r_container
EXPORTING
container_name = 'CONTAINER'.
CREATE OBJECT r_grid
EXPORTING
i_parent = r_container.
CALL METHOD r_grid->set_table_for_first_display
EXPORTING
i_structure_name = 'SFLIGHT'
CHANGING
it_outtab = t_sflight.
ENDMODULE. " HANDLER OUTPUT
You can change the field catalog during runtime .
To achieve this you can use the method 'get_frontend_fieldcatalog',of class 'cl_gui_alv_grid'.
Step 1.
Create an internal table of type LVC_T_FCAT.
Step 2.
Call method 'get_frontend_fieldcatalog' of class cl_gui_alv_grid and populate the field catalog using the parameter 'et_fieldcatalog' . This parameter will give you the field catalog of the current displayed screen.
Step 3.
Now loop at the field catalog and make the required changes.
Step 4.
Call the method 'set_table_for_first_display' and pass the field catalog.
Example:
In this example ,SLFIGHT details will be displayed first and if you press
the push button on the screen(SWITCH) ,SFLIGHT details will be displayed
with changes like 1. OUTPUT LENGTH OF CONNID WILL BE CHANGED.
2. FIELD PLANETYPE WILL BE HIDDEN.
3. GRAND TOTAL OF FIELD PRICE WILL BE CALCULATED.
REPORT zalv_dynamic_fieldcatalog.
DATA:
t_sflight TYPE TABLE OF sflight,
fs_sflight TYPE sflight.
DATA:
r_container TYPE REF TO cl_gui_custom_container,
r_grid TYPE REF TO cl_gui_alv_grid.
DATA:
t_fcat TYPE lvc_t_fcat,
fs_fcat TYPE lvc_s_fcat.
SELECT * FROM sflight INTO TABLE t_sflight.
CALL SCREEN 100.
*&---------------------------------------------------------------------
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------
MODULE status_0100 OUTPUT.
SET PF-STATUS 'SCREEN'.
SET TITLEBAR 'SFLIGHT DETAILS'.
ENDMODULE. " STATUS_0100 OUTPUT
*&---------------------------------------------------------------------
*& Module USER_COMMAND_0100 INPUT
*&---------------------------------------------------------------------
MODULE user_command_0100 INPUT.
CASE sy-ucomm.
WHEN 'BACK'.
LEAVE TO SCREEN 0.
WHEN 'SWITCH'.
CALL METHOD r_grid->get_frontend_fieldcatalog
IMPORTING
et_fieldcatalog = t_fcat.
LOOP AT t_fcat INTO fs_fcat.
CASE fs_fcat-fieldname.
WHEN 'CONNID'.
fs_fcat-outputlen = '10'.
MODIFY t_fcat FROM fs_fcat.
WHEN 'PLANETYPE'.
fs_fcat-no_out = 'X'.
MODIFY t_fcat FROM fs_fcat.
WHEN 'PRICE'.
fs_fcat-do_sum = 'X'.
MODIFY t_fcat FROM fs_fcat.
ENDCASE.
ENDLOOP.
CALL METHOD r_grid->set_table_for_first_display
CHANGING
it_fieldcatalog = t_fcat
it_outtab = t_sflight.
ENDCASE.
ENDMODULE. "user_command_0100 INPUT
*&---------------------------------------------------------------------
*& Module list OUTPUT
*&---------------------------------------------------------------------
MODULE list OUTPUT.
CREATE OBJECT r_container
EXPORTING
container_name = 'CONTAINER'.
CREATE OBJECT r_grid
EXPORTING
i_parent = r_container.
CALL METHOD r_grid->set_table_for_first_display
EXPORTING
i_structure_name = 'SFLIGHT'
CHANGING
it_outtab = t_sflight.
ENDMODULE. " HANDLER OUTPUT
Field catalog types in ABAP ALV Report
Field catalog:
Field catalog is a format description of the display area.
There are three procedures to built FIELD CATALOG.
1. Automatic field catalog.
2. Semi automatic field catalog.
3. Manual field catalog.
1. Automatic field catalog.
If the list structure has the same line type as the dictionary structure,then the proxy instance automaticaly will create the field catalog.
To do this, just pass the name of the dictionary structure to form using the parameter
'I_STRUCTURE_NAME'.
Example:
REPORT zalv.
DATA: t_sflight TYPE TABLE OF sflight, " INTERNAL TABLE FOR SFLIGHT
fs_sflight TYPE sflight. " WORK AREA FOR SFLIGHT
DATA: r_container TYPE REF TO cl_gui_custom_container,
r_grid TYPE REF TO cl_gui_alv_grid.
SELECT * FROM sflight INTO TABLE t_sflight.
CALL SCREEN 100.
*&---------------------------------------------------------------------
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------
*text
*----------------------------------------------------------------------
MODULE status_0100 OUTPUT.
SET PF-STATUS 'SCREEN1'.
SET TITLEBAR 'TITLE1'.
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. " STATUS_0100 INPUT
*&---------------------------------------------------------------------
*& Module HANDLER OUTPUT
*&---------------------------------------------------------------------
*text
*----------------------------------------------------------------------
MODULE handler OUTPUT.
CREATE OBJECT r_container
EXPORTING
container_name = 'CONTAINER'.
CREATE OBJECT r_grid
EXPORTING
i_parent = r_container.
CALL METHOD r_grid->set_table_for_first_display
EXPORTING
i_structure_name = 'SFLIGHT'
CHANGING
it_outtab = t_sflight.
ENDMODULE. "USER_COMMAND_0100 INPUT
2. Semi automatic field catalog.
All fields of the global structure type appears in the data table with the same name.
Now the requirment is to make 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 at it and can make changes what ever we want.
Example:
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.
.
.
.
.
.
Now you can pass this field catalog to the method SET_TABLE_FOR_FIRST_DISPLAY.
3. Manual field catalog.
If the data table's line type does not contains the dictionary references or it only contains references to individual
global structure fields,you have to create the field catalog manually.
For this you have to cteate an internal table of type LVC_T_FACT and populate it manually.
Example :
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
Field catalog is a format description of the display area.
There are three procedures to built FIELD CATALOG.
1. Automatic field catalog.
2. Semi automatic field catalog.
3. Manual field catalog.
1. Automatic field catalog.
If the list structure has the same line type as the dictionary structure,then the proxy instance automaticaly will create the field catalog.
To do this, just pass the name of the dictionary structure to form using the parameter
'I_STRUCTURE_NAME'.
Example:
REPORT zalv.
DATA: t_sflight TYPE TABLE OF sflight, " INTERNAL TABLE FOR SFLIGHT
fs_sflight TYPE sflight. " WORK AREA FOR SFLIGHT
DATA: r_container TYPE REF TO cl_gui_custom_container,
r_grid TYPE REF TO cl_gui_alv_grid.
SELECT * FROM sflight INTO TABLE t_sflight.
CALL SCREEN 100.
*&---------------------------------------------------------------------
*& Module STATUS_0100 OUTPUT
*&---------------------------------------------------------------------
*text
*----------------------------------------------------------------------
MODULE status_0100 OUTPUT.
SET PF-STATUS 'SCREEN1'.
SET TITLEBAR 'TITLE1'.
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. " STATUS_0100 INPUT
*&---------------------------------------------------------------------
*& Module HANDLER OUTPUT
*&---------------------------------------------------------------------
*text
*----------------------------------------------------------------------
MODULE handler OUTPUT.
CREATE OBJECT r_container
EXPORTING
container_name = 'CONTAINER'.
CREATE OBJECT r_grid
EXPORTING
i_parent = r_container.
CALL METHOD r_grid->set_table_for_first_display
EXPORTING
i_structure_name = 'SFLIGHT'
CHANGING
it_outtab = t_sflight.
ENDMODULE. "USER_COMMAND_0100 INPUT
2. Semi automatic field catalog.
All fields of the global structure type appears in the data table with the same name.
Now the requirment is to make 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 at it and can make changes what ever we want.
Example:
Report zalv.
DATA: t_fcat TYPE lvc_t_fcat. " INTERNAL TABLE
FIELD-SYMBOLS:
.
.
.
.
.
.
.
.
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
WHEN 'PLANETYPE'.
WHEN 'SEATSMAX'.
ENDCASE.
ENDLOOP.
.
.
.
.
.
Now you can pass this field catalog to the method SET_TABLE_FOR_FIRST_DISPLAY.
3. Manual field catalog.
If the data table's line type does not contains the dictionary references or it only contains references to individual
global structure fields,you have to create the field catalog manually.
For this you have to cteate an internal table of type LVC_T_FACT and populate it manually.
Example :
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
Subscribe to:
Posts (Atom)