Enhancing ALV Display by Modifying REUSE_ALV_FIELDCATALOG_MERGE
Modifying the function Module REUSE_ALV_FIELDCATALOG_MERGE :-
Q. Requirement :-
-
When we run the program and we are using the above function module, the output shows like below.
-
Now, Customer wants that, Time should be available at the second position and Created on should be at third position.
-
Also In place of Sales Document, it should be visible as Document Number.
Solution :-
-
We will loop inside the internal table of field catalog and perform the required changes.
Code :-
*************************************************
*Start of Program
*Type Strcuture fo Both header and Item table
TYPES : BEGIN OF ty_vbak,
vbeln TYPE vbeln_va,
erdat TYPE erdat,
erzet TYPE erzet,
ernam TYPE ernam,
vbtyp TYPE vbtypl,
END OF ty_vbak.
TYPES : BEGIN OF ty_vbap,
vbeln TYPE vbeln_va,
posnr TYPE posnr_va,
matnr TYPE matnr,
END OF ty_vbap.
********************************************
*Type structure for final table.
TYPES : BEGIN OF ty_final,
vbeln TYPE vbeln_va,
erdat TYPE erdat,
erzet TYPE erzet,
ernam TYPE ernam,
vbtyp TYPE vbtypl,
posnr TYPE posnr_va,
matnr TYPE matnr,
END OF ty_final.
*************************************************
*Declaring the internal table and work area
DATA : lt_vbak TYPE TABLE OF ty_vbak,
ls_vbak TYPE ty_vbak,
lt_vbap TYPE TABLE OF ty_vbap,
ls_vbap TYPE ty_vbap,
lt_final TYPE TABLE OF ty_final,
ls_final TYPE ty_final,
lv_vbeln TYPE vbeln_va.
DATA : lt_fieldcat TYPE TABLE OF slis_fieldcat_alv,
ls_fieldcat TYPE slis_fieldcat_alv.
DATA : ls_layout TYPE slis_layout_alv.
***************
*Declaring a select option.
SELECT-OPTIONS : s_vbeln FOR lv_vbeln.
START-OF-SELECTION.
SELECT vbeln erdat erzet ernam vbtyp
FROM vbak INTO TABLE lt_vbak
WHERE vbeln IN s_vbeln.
IF lt_vbak IS NOT INITIAL.
SELECT vbeln posnr matnr
FROM vbap
INTO TABLE lt_vbap
FOR ALL ENTRIES IN lt_vbak
WHERE vbeln = lt_vbak-vbeln.
LOOP AT lt_vbap INTO ls_vbap.
ls_final-posnr = ls_vbap-posnr.
ls_final-matnr = ls_vbap-matnr.
READ TABLE lt_vbak INTO ls_vbak WITH KEY vbeln = ls_vbap-vbeln.
IF sy-subrc EQ 0.
ls_final-vbeln = ls_vbak-vbeln.
ls_final-erdat = ls_vbak-erdat.
ls_final-erzet = ls_vbak-erzet.
ls_final-ernam = ls_vbak-ernam.
ls_final-vbtyp = ls_vbak-vbtyp.
ENDIF.
APPEND ls_final TO lt_final.
CLEAR : ls_final, ls_vbak, ls_vbap.
ENDLOOP.
***************************************
*Call REUSE ALV fieldcatalog merge
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = 'ZAR_ORDER_DETAILS'
CHANGING
ct_fieldcat = lt_fieldcat
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.
LOOP AT lt_fieldcat INTO ls_fieldcat.
IF ls_fieldcat-fieldname = 'VBELN'.
ls_fieldcat-seltext_s = 'Document No'.
ls_fieldcat-seltext_m = 'Document No'.
ls_fieldcat-seltext_l = 'Document No'.
MODIFY lt_fieldcat FROM ls_fieldcat TRANSPORTING seltext_s seltext_m seltext_l.
ENDIF.
IF ls_fieldcat-fieldname = 'ERDAT'.
ls_fieldcat-col_pos = '3'.
MODIFY lt_fieldcat FROM ls_fieldcat TRANSPORTING col_pos.
ENDIF.
IF ls_fieldcat-fieldname = 'ERZET'.
ls_fieldcat-col_pos = '2'.
MODIFY lt_fieldcat FROM ls_fieldcat TRANSPORTING col_pos.
ENDIF.
ENDLOOP.
ls_layout-colwidth_optimize = 'X'.
ls_layout-zebra = 'X'.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
* I_CALLBACK_PROGRAM = ' '
* I_CALLBACK_PF_STATUS_SET = ' '
* I_CALLBACK_USER_COMMAND = ' '
* I_CALLBACK_TOP_OF_PAGE = ' '
is_layout = ls_layout
it_fieldcat = lt_fieldcat
* IT_EXCLUDING =
* IT_SPECIAL_GROUPS =
* IT_SORT =
* IT_FILTER =
TABLES
t_outtab = lt_final
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
ENDIF.
*****************
*End of Program
*****************************************************
Execute the code :-
Displaying ALV without creating a field catalog :-
-
We can achieve this by simply passing the structure of our final table directly into the function module REUSE_ALV_GRID_DISPLAY.
Code :-
*************************************************
*Start of Program
*Type Strcuture fo Both header and Item table
TYPES : BEGIN OF ty_vbak,
vbeln TYPE vbeln_va,
erdat TYPE erdat,
erzet TYPE erzet,
ernam TYPE ernam,
vbtyp TYPE vbtypl,
END OF ty_vbak.
TYPES : BEGIN OF ty_vbap,
vbeln TYPE vbeln_va,
posnr TYPE posnr_va,
matnr TYPE matnr,
END OF ty_vbap.
********************************************
*Type structure for final table.
TYPES : BEGIN OF ty_final,
vbeln TYPE vbeln_va,
erdat TYPE erdat,
erzet TYPE erzet,
ernam TYPE ernam,
vbtyp TYPE vbtypl,
posnr TYPE posnr_va,
matnr TYPE matnr,
END OF ty_final.
*************************************************
*Declaring the internal table and work area
DATA : lt_vbak TYPE TABLE OF ty_vbak,
ls_vbak TYPE ty_vbak,
lt_vbap TYPE TABLE OF ty_vbap,
ls_vbap TYPE ty_vbap,
lt_final TYPE TABLE OF ty_final,
ls_final TYPE ty_final,
lv_vbeln TYPE vbeln_va.
***************
*Declaring a select option.
SELECT-OPTIONS : s_vbeln FOR lv_vbeln.
START-OF-SELECTION.
SELECT vbeln erdat erzet ernam vbtyp
FROM vbak INTO TABLE lt_vbak
WHERE vbeln IN s_vbeln.
IF lt_vbak IS NOT INITIAL.
SELECT vbeln posnr matnr
FROM vbap
INTO TABLE lt_vbap
FOR ALL ENTRIES IN lt_vbak
WHERE vbeln = lt_vbak-vbeln.
LOOP AT lt_vbap INTO ls_vbap.
ls_final-posnr = ls_vbap-posnr.
ls_final-matnr = ls_vbap-matnr.
READ TABLE lt_vbak INTO ls_vbak WITH KEY vbeln = ls_vbap-vbeln.
IF sy-subrc EQ 0.
ls_final-vbeln = ls_vbak-vbeln.
ls_final-erdat = ls_vbak-erdat.
ls_final-erzet = ls_vbak-erzet.
ls_final-ernam = ls_vbak-ernam.
ls_final-vbtyp = ls_vbak-vbtyp.
ENDIF.
APPEND ls_final TO lt_final.
CLEAR : ls_final, ls_vbak, ls_vbap.
ENDLOOP.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_STRUCTURE_NAME = 'ZAR_ORDER_DETAILS'
TABLES
t_outtab = lt_final
EXCEPTIONS
PROGRAM_ERROR = 1
OTHERS = 2
.
ENDIF.
*****************
*End of Program
*****************************************************
Execute the code :-
Limitations of Not Creating the Field catalog :-
Directly passing a structure to REUSE_ALV_GRID_DISPLAY
without using a field catalog can have several limitations:
- Dynamic Column Handling: Field catalogs allow for dynamic manipulation of columns, such as hiding or rearranging columns, changing column headers, or specifying column widths. Without a field catalog, you lose the ability to dynamically control these aspects of the ALV grid.
- Field Attributes: Field catalogs allow you to specify various attributes for each field, such as field type, alignment, hotspot, and more. Not using a field catalog means you can't define these attributes dynamically, potentially limiting the usability and interactivity of your ALV grid.
- Data Binding: Field catalogs help in mapping the structure fields to the corresponding ALV grid columns. Without a field catalog, you would need to handle this mapping manually, which can be cumbersome and error-prone, especially if the structure and the ALV grid layout change frequently.
- Runtime Flexibility: Field catalogs provide flexibility at runtime to modify the ALV grid's structure and appearance based on user preferences or application logic. Without a field catalog, making such changes dynamically becomes much more complex and may require significant coding effort.
- Reuse and Maintainability: Using field catalogs promotes code reuse and maintainability since you can define the ALV grid's structure and behavior separately from the application logic. Without a field catalog, the ALV grid's configuration is tightly coupled with the application code, making it harder to maintain and reuse.
- Standardization: Field catalogs allow you to define a standardized structure for displaying data in ALV grids across different applications or screens. Without a field catalog, each application may implement its own logic for displaying data in ALV grids, leading to inconsistency and lack of standardization.
Overall, while it may be possible to display data in an ALV grid without using a field catalog, doing so can limit the flexibility, maintainability, and standardization of your ALV-based applications. It's generally recommended to use field catalogs whenever possible to leverage the full functionality and benefits of the ALV framework.
Comments
Post a Comment