繁体   English   中英

从字段符号获取字段名称

[英]Getting the fieldnames from the field-symbols

我需要在字段符号<itab>获取字段名称,以便可以将名称用于ALV的字段目录。

所以我使用了cl_abap_structdescr但它总是使我出错。 我用一个内部表尝试了此操作,并且得到了预期的结果,但是我必须使用字段符号而不是内部表。

ASSIGN lo_itab->* TO <itab>

data: go_struct type ref to cl_abap_structdescr,
      gt_comp   type abap_component_tab,
      gs_comp   type abap_componentdescr.


  go_struct ?= cl_abap_typedescr=>describe_by_data( <itab> ).
  gt_comp = go_struct->get_components( ).

  loop at gt_comp into gs_comp.

      PERFORM fill_fieldcat USING : 
     gs_comp-name      ''       gs_comp-name
   .
  endloop.

这是错误; 在此处输入图片说明

因为<itab>显然是一个内部表 ,所以它的类型是“表”,而不是“结构”! (另请参见简短转储,它说describe_by_data返回的类型cl_abap_tabledescr与目标go_struct的类型不兼容,即cl_abap_structdescr

因此,您必须首先获取其表类型,然后获取其行的类型(我在这里假设它是结构化类型,但在某些其他情况下也可以是其他类型)。

data: go_table type ref to cl_abap_tabledescr.
      go_struct type ref to cl_abap_structdescr,
      gt_comp   type abap_component_tab,
      gs_comp   type abap_componentdescr.

go_table ?= cl_abap_typedescr=>describe_by_data( <itab> ).
go_struct ?= go_table->get_table_line_type( ).
gt_comp = go_struct->get_components( ).
...

当您将引用类型分配给另一个引用类型时,您将得到转储。 定义结构类型并像下面的示例中那样传递。 您将不会得到任何转储。

data: go_struct type ref to cl_abap_structdescr,
      gt_comp   type abap_component_tab,
      gs_comp   type abap_componentdescr.

  DATA ls_spfli TYPE spfli.
  go_struct ?= cl_abap_typedescr=>describe_by_data( ls_spfli ).
  gt_comp = go_struct->get_components( ).

  loop at gt_comp into gs_comp.

*      PERFORM fill_fieldcat USING :
*     gs_comp-name      ''       gs_comp-name
*   .
  endloop.

暂无
暂无

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

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