Reputation: 1820
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.
Upvotes: 0
Views: 477
Reputation: 1722
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.
Upvotes: 3