简体   繁体   English

从字段符号获取字段名称

[英]Getting the fieldnames from the field-symbols

I need to get the field names in the field-symbol <itab> , so that I can use the names for the field catalog of ALV. 我需要在字段符号<itab>获取字段名称,以便可以将名称用于ALV的字段目录。

So I used cl_abap_structdescr but it throws me always an error. 所以我使用了cl_abap_structdescr但它总是使我出错。 I tried this with an internal table and i had the expected result, but I have to use the field symbol instead of internal table. 我用一个内部表尝试了此操作,并且得到了预期的结果,但是我必须使用字段符号而不是内部表。

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.

this is the error; 这是错误; 在此处输入图片说明

Because <itab> is obviously an internal table , its type is "table", not "structure"! 因为<itab>显然是一个内部表 ,所以它的类型是“表”,而不是“结构”! (also see the short dump, it says that describe_by_data returned a type cl_abap_tabledescr which is not compatible with the type of target go_struct , ie cl_abap_structdescr ) (另请参见简短转储,它说describe_by_data返回的类型cl_abap_tabledescr与目标go_struct的类型不兼容,即cl_abap_structdescr

So you must first get its table type, then get the type of its lines (I assume here that it's a structured type, but it could be other types in some other situations). 因此,您必须首先获取其表类型,然后获取其行的类型(我在这里假设它是结构化类型,但在某些其他情况下也可以是其他类型)。

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( ).
...

As you are assigning reference type to another reference type therefore you getting dump. 当您将引用类型分配给另一个引用类型时,您将得到转储。 Define a structure type and pass in place of as i did in the below example. 定义结构类型并像下面的示例中那样传递。 You will not get any dump. 您将不会得到任何转储。

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