简体   繁体   English

创建动态ABAP内表

[英]Create dynamic ABAP internal table

On selection screen, the user needs to insert a table name, and I need to get first 3 fields from that table and display them in an ALV for the output.在选择屏幕上,用户需要插入一个表名,我需要从该表中获取前 3 个字段并将它们显示在 ALV 中以供输出。 What I understand from reading tutorials is that I need to call method cl_alv_table_create=>create_dynamic_table , but I don't know how to create the fieldcatalog.我从阅读教程中了解到,我需要调用方法cl_alv_table_create=>create_dynamic_table ,但我不知道如何创建 fieldcatalog。

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.

I assume that the table name which user enters is a data dictionary table (like SFLIGHT).我假设用户输入的表名是数据字典表(如 SFLIGHT)。 If yes, 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 the selection screen parameter containing the table name.这里,“ p_table ”是包含表名的选择屏幕参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM