Skip to Content

We all know about ALV reports and the flexibility and user friendliness that ALV offers to users and ease of development for the technical consultants.

However, off late I have seen requirements where the users want dynamically add or remove columns depending on the data selected. For example, there is a report which compares the same set of fields for a given dimension. To be more precise if for a vendor / RFQ, you want to compare a list of fields and the user wants the comparison to be displayed vertically, that means more number of vendors / RFQ’s, more number of fields need to be added to the output.

Now to the field catalog you can add more fields at runtime depending on the data coming in. However, how to add more columns to the internal table that will be used to display the data on the ALV grid. One crude of way doing is declare an internal table big enough to handle maximum fields and depending on the actual fields coming in at runtime, build the field catalog only for those field required so that the extra fields will not be visible. However, this is not the efficient way doing the same and more over you have every chance that the code might short dump as the field catalog and the structure of the output internal table are in sync.

So the elegant solution for this is to create a table dynamically, with the list of the fields that we have determined at run time. The question is how do we do that. Here is a quick solution.

1. Create your field catalog either manually or automatically using the function module, LVC_FIELDCATALOG_MERGE. Add more rows to the field catalog table (T_FIELDCAT) at run time.


2. Use the field catalog to create a table dynamically using the method below.


DATA: T_OUTPUT TYPE REF TO DATA

FIELD-SYMBOLS: <T_OUTPUT> TYPE TABLE

Call Method CL_ALV_TABLE_CREATE->CREATE_DYNAMIC_TABLE

Exporting

IT_FIELDCATALOG = T_FIELDCAT

Importing

EP_TABLE = T_OUTPUT

ASSIGN T_OUTPUT->* TO <T_OUTPUT>.

Now the field symbol <T_OUTPUT> is pointing to an output table of the structure that contains the fields which were determined at runtime. Now fill this table with the data and pass <T_OUTPUT> to the method SET_TABLE_FOR_FIRST_DISPLAY and the ALV grid should show the data properly.

However, if you want to access or modify or fill in the data for specific columns, you have to use the ASSIGN COMPONENT statement for the specific field name and do it.

To report this post you need to login first.

5 Comments

You must be Logged on to comment or reply to a post.

  1. Former Member
    Hi Ravi,

    Just a coincidence, but look at this weblog as well : /people/subramanian.venkateswaran2/blog/2004/11/19/dynamic-internal-table

    Regards,
    Subramanian V.

    (0) 
  2. José Gabriel Martínez
    Hi Ravikumar,

    I hope that besides this blog is very old, you could be still able to track it.

    I have a problem with a dynamic table and the event ‘before_line_output’.

    Usually, I use this event to perform specific calculations for totals line (with REUSE_ALV_LIST_DISPLAY). I just write the desired code there and modify the respective columns of my outtab at the header line, checking:
    “if p_lineinfo-endsum EQ ‘X’ OR p_lineinfo-subtot EQ ‘X’.”.

    But, with a dynamic table ,there is a problem: there is no header line, so, I don’t know how to “pass” my calculations to specific columns.

    I posted the issue at the forum, with tittle:
    “Dynamic itab (field-symbols) and ALV event BEFORE_LINE_OUTPUT”.

    Is really important to keep the code the it is, I mean, with no mayo changes…but if I can not use the event before_line_output…then I would like to know if there is a method or function (may be LVC_TOTALS_GET?, I don’t know) to do this.

    I have spent a lot of time looking foe demo codes in SAP or examples in internet, but it has been an unsuccesful search.

    Thanks in advance if you can read this “late” entry for your blog.

    Regards.
    José Gabriel Martínez.

    (0) 
  3. Former Member
    Hi Ravikumar,

    how to set the heading dynamically in alv report.
    for eg. if i have 00001 production order which having operation 10,20,30…..so on then i want to display heading for this like oprn1 oprn2 oprn3 ….and so on.

    please reply.

    (0) 

Leave a Reply