Describes how to show the Dynamic Internal Table Creation using class CL_ALV_TABLE_CREATE
In the post
Dynamic Internal Table creation and
Dynamic Internal Table with Deep Structure, we have seen how we can use the RTTS - Run Time Type Services to create dynamic internal table.
Today we will eloborate another way of creating the dynamic internal table. We will see how we can use the class CL_ALV_TABLE_CREATE to create a dynamic internal table for ALV.
Basic pricipal would be, we need to fill the field catalog table and pass it to static method CREATE_DYNAMIC_TABLE from class CL_ALV_TABLE_CREATE.
We will use the same example as the reference in the post
Dynamic Internal Table creation.
Here is the code snippet:
Code Snippet to Display ALV output on the Selection Screen |
*&---------------------------------------------------------------------* *& Report ZTEST_NP_DYN *& This Code snippet shows how to *& Create Dynamic Internal Table using the fieldcatalog and *& class CL_ALV_TABLE_CREATE *& Dynamic Selection of data *& Accessing Dynamic data selection *& Displaying Dynamic internal table in ALV *&---------------------------------------------------------------------* * REPORT ztest_np_dyn. * DATA: lt_fieldcat TYPE lvc_t_fcat, la_fieldcat TYPE lvc_s_fcat, lo_table TYPE REF TO data. * DATA: lf_months TYPE monat, lf_run_mon TYPE monat. * * Dynamic Selection fields TYPES: BEGIN OF ty_fields, field TYPE char30, END OF ty_fields. * DATA: lt_fields TYPE STANDARD TABLE OF ty_fields, la_fields TYPE ty_fields. * * field symbols to access the dynamic table FIELD-SYMBOLS: TYPE ANY TABLE, TYPE ANY, TYPE ANY. * * Selection Screen PARAMETERS: p_mon_fr TYPE monat, p_mon_to TYPE monat. * * START-OF-SELECTION. * 1. Adding fields in the field catalog la_fieldcat-fieldname = 'KSTAR'. la_fieldcat-datatype = 'CHAR'. la_fieldcat-outputlen = 10. APPEND la_fieldcat TO lt_fieldcat. CLEAR la_fieldcat. * * 2. Adding required fields based on the selection months * Determining Number of fields lf_months = ( p_mon_to - p_mon_fr ) + 1. lf_run_mon = p_mon_fr. * DO lf_months TIMES. * Field name CONCATENATE 'WTG0' lf_run_mon INTO la_fieldcat-fieldname. la_fieldcat-datatype = 'CURR'. la_fieldcat-outputlen = 17. * * Filling the fieldcatalog APPEND la_fieldcat TO lt_fieldcat. CLEAR la_fieldcat. * lf_run_mon = lf_run_mon + 1. ENDDO. * * Calling method to generate the dynamic internal table based on * ALV field catalog cl_alv_table_create=>create_dynamic_table( EXPORTING it_fieldcatalog = lt_fieldcat IMPORTING ep_table = lo_table ). * ASSIGN lo_table->* TO . * *$*$*...............Dynamic Selection.............................*$*$* * Filling up the table for the Selection fields of Select Query LOOP AT lt_fieldcat INTO la_fieldcat. la_fields-field = la_fieldcat-fieldname. APPEND la_fields TO lt_fields. CLEAR: la_fieldcat, la_fields. ENDLOOP. * * Selecting data SELECT (lt_fields) INTO TABLE FROM cosp UP TO 10 ROWS. * *$*$*...............Accessing dynamic table.......................*$*$* LOOP AT ASSIGNING . ASSIGN COMPONENT 'WTG004' OF STRUCTURE TO . = '100.00'. ENDLOOP. * *$*$*...............Displaying using SALV model...................*$*$* DATA: lo_alv TYPE REF TO cl_salv_table. * TRY. cl_salv_table=>factory( EXPORTING list_display = abap_false IMPORTING r_salv_table = lo_alv CHANGING t_table = ). CATCH cx_salv_msg . ENDTRY. * lo_alv->display( ).
|
I would suggest to use the RTTS to create dynamic internal table as depicted in posts:
0 comments
Post a Comment