如何表明DDIC结构中的某些字段在ALV中应该被隐藏或标记为“技术”字段?

huangapple go评论73阅读模式
英文:

How to signal that some fields from a DDIC structure are intended to be hidden or "technical" in an ALV?

问题

我的目标是创建一个动态的ALV,其字段目录基于传递给它的自定义DDIC结构。我希望我的DDIC结构的某些字段在ALV中是隐藏的,但我不应该硬编码任何特定的字段以隐藏或标记为技术字段,因为我事先不知道ALV的结构。

我想在DDIC结构中定义哪些字段是隐藏的或显示的。
是否有可能实现这一点,如果可以,怎么做?

感谢您的建议。

英文:

My goal is to make a dynamic ALV whose field catalog is based on a custom DDIC structure that is passed to it. I want some fields of my DDIC structure to be hidden in the ALV but I should not hardcode any specific field to be hidden or technical, because I do not know the ALV's structure in advance.

I would like to define in the DDIC structure which fields are hidden or shown.
Is it possible to achieve this, and if so, how?

Thanks for your suggestions.

答案1

得分: 1

DDIC结构本身的数据(以及更改)与ALV无关。将字段设置为技术字段并在ALV中隐藏它们仅与ALV功能相关,可以通过ALV提供的方法设置。此外,更改DDIC结构可能会对已经使用它们的其他代码产生重大影响(更改由SAP提供的结构 - 而不是Z结构 - 已经是对系统的修改,影响更大)。

当您调用函数模块 LVC_FIELDCATALOG_MERGE 时,在返回的字段目录内部表中设置相应的 TECH 标志后,应该不会有问题。您可以这样做:

  DATA: lt_fldcat TYPE lvc_t_fcat.

  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
    EXPORTING
      i_structure_name       = 'DDIC_STRUCTURE_NAME'
    CHANGING
      ct_fieldcat            = lt_fldcat
    EXCEPTIONS
      inconsistent_interface = 1
      program_error          = 2
      OTHERS                 = 3.

  IF sy-subrc = 0.

    lt_fldcat[ fieldname = 'FIELD_NAME_TO_HIDE' ]-tech = 'X'.

  ENDIF.

如果您无法硬编码字段名称,那么您需要以某种方式在代码中动态执行此操作。

例如,将您想要隐藏的字段名称列表作为参数传递给您的子程序(FORM)或方法(如果您使用面向对象编程)。另一种可能性是动态读取结构组件,但无论哪种情况,您都应该在之后编写一些逻辑来决定要隐藏哪些字段 - 例如,特定类型/名称模式/文本模式等。

如果您仍然需要修改DDIC结构,可以通过添加 附加结构 来扩展它(因此您不会修改结构本身中的字段)。附加结构只是ABAP字典中附加到另一个结构的结构,并将其组件附加到它们。附加结构也可以添加到SAP在客户系统中提供的结构中。还请注意,使用附加结构来增强SAP结构在客户系统中不构成修改

在附加结构中,您可以添加带有名称前缀的要隐藏的字段,例如 ZH_FIELDNAME(类型不重要,可以是CHAR1 / FLAG,或者最好为其创建适当的数据元素,并提供适用于这些字段目的的适当文本)。之后,您可以动态读取结构数据,并通过删除名称前缀来将这些字段设置为技术字段。

您可以使用Runtime Type Services (RTTS) 来动态读取结构,具体操作如下,可以使用 describe_by_name 方法传递结构名称或 describe_by_data 方法传递结构变量:

  DATA(lt_components) = CAST cl_abap_structdescr(
    cl_abap_typedescr=>describe_by_data( ls_structure_variable )
  )->components.

components 方法返回一个简单的组件列表,包括名称和类型。如果需要更多数据,包括扩展类型信息和字段的文本,请改用 get_ddic_field_list 方法:

  DATA(lt_field_list) = CAST cl_abap_structdescr(
    cl_abap_typedescr=>describe_by_data( ls_structure_variable )
  )->get_ddic_field_list( p_including_substructres = abap_true ).

根据您选择的方法,使用检索到的有关结构字段的信息进行进一步操作。

英文:

The data in DDIC structure itself (and also the changes) has no relation to ALV. Setting the fields as technical and hiding them in ALV is solely relevant to ALV functionality and can be set by methods provided in ALV. Moreover, changing DDIC structures can have big influence on other code which already uses them (and changing structures provided by SAP - not Z-structures - is already a modification of the system and has even more cosequences).

As you call function module LVC_FIELDCATALOG_MERGE, it should not be an issue afterwards to set the appropriate TECH flag in the returned field catalog internal table for the fields you need to hide. You can do it like this:

  DATA: lt_fldcat TYPE lvc_t_fcat.

  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
    EXPORTING
      i_structure_name       = 'DDIC_STRUCTURE_NAME'
    CHANGING
      ct_fieldcat            = lt_fldcat
    EXCEPTIONS
      inconsistent_interface = 1
      program_error          = 2
      OTHERS                 = 3.

  IF sy-subrc = 0.

    lt_fldcat[ fieldname = 'FIELD_NAME_TO_HIDE' ]-tech = 'X'.

  ENDIF.

If you cannot hardcode the field names, than you need to do it somehow dynamically in code.

For example to pass the list of field names which you want to hide as a parameter to your subroutine (form) or a method (if you use OO).

Another possibility would be to read the structure components dynamically, but in any case you should have afterwards some logic to decide which fields you want to hide - for exampe of certain type / name pattern / text pattern / etc.

If you need nevertheless to modify DDIC structure, you can extend it by adding an append structure (so you do not modify the fields in the structure itself). An append structure is just a structure in ABAP Dictionary appended to another structure and which appends their components to them. Append structures can be also added to structures delivered by SAP in customer systems. Please also note that the enhancement of SAP structures using append structures in customer systems does not constitute a modification.

In the append structure you add the fields you want to hide with some prefix in name, like ZH_FIELDNAME (type is irrelevant, CHAR1 / FLAG or you’d better create own data element for it with appropriate texts to reflect the purpose of these fields). Afterwards you read the structure data dynamically and set the fields as technical referring to them by name prefix removed.

You can read the structure dynamically using Runtime Type Services
(RTTS) as following, by either describe_by_name method passing the structure name or describe_by_data method passing the structure variable:

  DATA(lt_components) = CAST cl_abap_structdescr(
*    cl_abap_typedescr=>describe_by_name( 'DDIC_STRUCTURE_NAME' )
    cl_abap_typedescr=>describe_by_data( ls_structure_variable )
  )->components.

The method components returns a simple components list with names and types. If you need more data including extended type information and also fields' texts, use get_ddic_field_list method instead:

  DATA(lt_field_list) = CAST cl_abap_structdescr(
*    cl_abap_typedescr=>describe_by_name( 'DDIC_STRUCTURE_NAME' )
    cl_abap_typedescr=>describe_by_data( ls_structure_variable )
  )->get_ddic_field_list( p_including_substructres = abap_true ).

Use the retrieved information about stucture's fields for further steps according to the approach you chose.

huangapple
  • 本文由 发表于 2023年6月16日 03:59:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76485148.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定