Create a dynamic ABAP internal table

In the selection window, the user needs to insert the table name, and I need to get the first 3 fields from this table and display them in ALV for output. From the reading tutorials, I understand that I need to call the cl_alv_table_create=>create_dynamic_table , but I don't know how to create a field directory.

 DATA: t_newtable TYPE REF TO data, t_fldcat TYPE lvc_t_fcat, CALL METHOD cl_alv_table_create=>create_dynamic_table EXPORTING it_fieldcatalog = t_fldcat IMPORTING ep_table = t_newtable. 
+7
sap abap
source share
1 answer

I assume that the name of the table the user enters is a data dictionary table (e.g. SFLIGHT). If so, then you can generate the field catalog as follows.


 data : it_tabdescr type abap_compdescr_tab, wa_tabdescr type abap_compdescr. data : ref_table_descr type ref to cl_abap_structdescr. ref_table_descr ?= cl_abap_typedescr=>describe_by_name( p_table ). it_tabdescr[] = ref_table_descr->components[]. loop at it_tabdescr into wa_tabdescr. clear wa_fieldcat. wa_fieldcat-fieldname = wa_tabdescr-name . wa_fieldcat-datatype = wa_tabdescr-type_kind. wa_fieldcat-inttype = wa_tabdescr-type_kind. wa_fieldcat-intlen = wa_tabdescr-length. wa_fieldcat-decimals = wa_tabdescr-decimals. append wa_fieldcat to it_fieldcat. endloop. 

Here " p_table " is a selection screen parameter containing a table name.

+9
source share

All Articles