Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Dynamic ALV Report with Dynamic Columns

former_member214709
Participant
0 Kudos
4,000

Hello ,

I want to create a Dynamic ALV Report, in which the columns of the ALV report would vary according to the data available in the internal table after the Select queries.

Ex:  If the internal table (say IT_TAB) contains 5 records then 5 columns would be displayed as output.

       If there are 4 records in internal table (say IT_TAB) then 4 Columns ... etc.

And the (Heading) description of the columns would be coming from the internal table IT_TAB itself.

This is part of the field catalog which i am able to handle.

But how do i take care of the Final Internal table (say IT_FINAL) which i have to pass to the FM 'REUSE_ALV_GRID_DISPLAY'.

As the fields of the Final Internal table (say IT_FINAL) have to be declared at the top, which have been fixed and declared once.

And how do i have to assign the correct amount values to the corresponding columns which may vary as per requirement.

Kindly Help.

Cheers,

Dinesh    

5 REPLIES 5

0 Kudos
611

Hi Dinesh,

You can carry out this task using field symbols and reference variables...

Try to think and do..

for any query please let me know then i will share the code.

(Hint : Create table with type any)

Regards,

Pathum.

Former Member
0 Kudos
611

Hi Dinesh,

Have a look at the method CL_ALV_TABLE_CREATE->CREATE_DYNAMIC_TABLE.

I haven't had the chance to use it yet. But I believe you can create a dynamic table based on your fieldcat table.

There are many examples of this method if you search on google and scn.

Please tell me if it worked.

Regards,

Thales Schmidt

0 Kudos
611

Hi Dinesh!

Thales is right!

If you use the method CREATE_DYNAMIC_TABLE from class CL_ALV_TABLE_CREATE, you could create a dynamic internal table based on fieldcatalog!

I've been using this a lot - I created a template that uses different content tables and after pick up a specific one, creates a dynamic output table to show on ALV report - and works very fine!

Here there is a simple example how to do this:

FIELD-SYMBOLS:
  <fs_table_ret>                  TYPE STANDARD TABLE                     . "#EC NEEDED

DATA: pgt_table                   TYPE REF TO data                        , "#EC NEEDED

            pgt_fieldcat               TYPE lvc_t_fcat                              .

DATA: pgw_layout               TYPE lvc_s_layo                              ,

            pgw_variant1            TYPE disvariant                         . "#EC NEEDED

DATA: pgv_error                   TYPE flag                               . "#EC NEEDED

DATA: pcl_gui_alv_grid            TYPE REF TO cl_gui_alv_grid             . "#EC NEEDED

* (...)

PERFORM create_dynamic_table
    USING pgt_fieldcat
CHANGING pgt_table
          pgv_error.

  ASSIGN pgt_table->* TO <fs_table_ret> .

  APPEND LINES OF IT_FINAL TO <fs_table_ret>.

* (...)

  CALL METHOD pcl_gui_alv_grid->set_table_for_first_display
    EXPORTING
      is_layout            = pgw_layout
*      it_toolbar_excluding = lt_toolbar[]
      is_variant           = pgw_variant1
      i_save               = 'A'
      i_default            = 'X' "
    CHANGING
      it_outtab            = <fs_table_ret>
      it_fieldcatalog      = pgt_fieldcat .

* (...)


*&---------------------------------------------------------------------*
*&      Form  CREATE_DYNAMIC_TABLE
*&---------------------------------------------------------------------*
FORM create_dynamic_table
                        USING ptu_fieldcatalog   TYPE lvc_t_fcat
                     CHANGING ptc_new_table      TYPE data
                              pvc_error          TYPE flag                . "#EC CALLED

  CLEAR: pvc_error.

  CALL METHOD cl_alv_table_create=>create_dynamic_table
    EXPORTING
      it_fieldcatalog           = ptu_fieldcatalog
    IMPORTING
      ep_table                  = ptc_new_table
    EXCEPTIONS
      generate_subpool_dir_full = 1
      OTHERS                    = 2.

  IF sy-subrc NE 0.
    pvc_error = 'X'.
  ENDIF.

ENDFORM.                    " CREATE_DYNAMIC_TABLE

Best regards,

Alexandre B. Dambrowski

0 Kudos
611

Hi Dinesh,

If you still need further information on how to solve your issue, check out this post by Susmitha Susan Thomas:

Best regards,

Alexandre B. Dambrowski

former_member216168
Active Participant
0 Kudos
611

I'm not sure if you can use this function to create dynamic columns, probably not...

After create the dynamic columns and fill them, I've displayed it using cl_gui_alv_grid class, setting it_outtab as a field symbol.

FIELD-SYMBOLS: <fs_t_alv> TYPE STANDARD TABLE.

cl_gui_alv_grid->set_table_for_first_display( .... changing it_outtab = <fs_t_alv> ..... ).