Application Development and Automation 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: 
Read only

Dynamic Alv report

Former Member
0 Likes
1,423

Hi all,

I have to create a ALV report where the fields have to be created dynamically.

the report will look like :

Oct 08 Nov 08 Dec 08

Sales 200 300 4000

Other Expense 40 40 50

1. in the selection screen. , based on the number of months i specify, the number of columns will have to change.

2. the First column in the alv ie....SALES will be a hard coded one.

3. the other columns will get the NETWR value or a value with some calculations.

Can Any one give me some help on this.

Thanks.

Vikki.

How do i design the internal table for this.

Hi all,

I have to create a ALV report where the fields have to be created dynamically.

the report will look like :

Oct 08 Nov 08 Dec 08

Sales 200 300 4000

Other Expense 40 40 50

1. in the selection screen. , based on the number of months i specify, the number of columns will have to change.

2. the First column in the alv ie....SALES will be a hard coded one.

3. the other columns will get the NETWR value or a value with some calculations.

Can Any one give me some help on this.

Thanks.

Vikki.

How do i design the internal table for this.

8 REPLIES 8
Read only

Former Member
0 Likes
1,092

Hello,

You mean to say that

Sales: 200 300 4000........................

Other Expense 40 40 50.......................

there will be 2 rows but any no. of columns.

Read only

0 Likes
1,092

Yes.

The First Column will be having some 10 rows. All the text there will be hard coded.

Oct 08 Nov 08 Dec 08

Sales

Expense

variences

frieght

spending

net sales

etc like this. All these will come in the first column.

we will have as many number of columns depending on the number of months specified.

only thing is the First col should get the above text.

Read only

0 Likes
1,092

Hello Vikki

There may be reasons why your matrix should look like you described but I think it should be just the other way around:

Columns = Sales / Expenses / ... / additional row: Period

The following sample report may demonstrate what I mean:


*&---------------------------------------------------------------------*
*& Report  ZUS_SDN_DYNAMIC_VS_STATIC_ALV
*&
*&---------------------------------------------------------------------*
*& Thread: Dynamic Alv report
*& <a class="jive_macro jive_macro_thread" href="" __jive_macro_name="thread" modifiedtitle="true" __default_attr="1117643"></a>
*&---------------------------------------------------------------------*

REPORT  zus_sdn_dynamic_vs_static_alv.

TABLES: vbak.


TYPES: BEGIN OF ty_s_outtab.
**INCLUDE TYPE vbak.
TYPES: sales      TYPE netwr.
TYPES: date       TYPE erdat.
TYPES: sum        TYPE netwr.
TYPES: period(6)   TYPE c.
TYPES: END OF ty_s_outtab.
TYPES: ty_t_outtab    TYPE STANDARD TABLE OF ty_s_outtab
                      WITH DEFAULT KEY.

DATA: gt_outtab    TYPE ty_t_outtab.

DATA:
  gd_okcode        TYPE ui_func,
  gd_repid         TYPE syrepid,
*
  gt_fcat          TYPE lvc_t_fcat,
  gs_layout        TYPE lvc_s_layo,
  gs_variant       TYPE disvariant,
  go_docking       TYPE REF TO cl_gui_docking_container,
  go_grid          TYPE REF TO cl_gui_alv_grid.


START-OF-SELECTION.

  PERFORM fill_data.

  PERFORM init_controls.
* Build fieldcatalog and set hotspot for field KUNNR
  PERFORM build_fieldcatalog.

  PERFORM set_layout_and_variant.


* Display data
  CALL METHOD go_grid->set_table_for_first_display
    EXPORTING
      is_variant                    = gs_variant
      i_save                        = 'A'
*      i_default                     = 'X'
      is_layout                     = gs_layout
*      is_print                      =
*      it_special_groups             =
*      it_toolbar_excluding          =
*      it_hyperlink                  =
*      it_alv_graphics               =
*      it_except_qinfo               =
*      ir_salv_adapter               =
    CHANGING
      it_outtab                     = gt_outtab
      it_fieldcatalog               = gt_fcat
*      it_sort                       =
*      it_filter                     =
    EXCEPTIONS
      invalid_parameter_combination = 1
      program_error                 = 2
      too_many_lines                = 3
      OTHERS                        = 4.
  IF sy-subrc <> 0.
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.


* Link the docking container to the target dynpro
  gd_repid = syst-repid.
  CALL METHOD go_docking->link
    EXPORTING
      repid                       = gd_repid
      dynnr                       = '0100'
*      CONTAINER                   =
    EXCEPTIONS
      OTHERS                      = 4.
  IF sy-subrc <> 0.
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.


* ok-code field = GD_OKCODE
  CALL SCREEN '0100'.

END-OF-SELECTION.


*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
  SET PF-STATUS 'STATUS_0100'.
*  SET TITLEBAR 'xxx'.

**      CALL METHOD go_grid1->refresh_table_display
***        EXPORTING
***          IS_STABLE      =
***          I_SOFT_REFRESH =
**        EXCEPTIONS
**          FINISHED       = 1
**          others         = 2
**              .
**      IF sy-subrc <> 0.
***       MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
***                  WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
**      ENDIF.

ENDMODULE.                 " STATUS_0100  OUTPUT

*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_0100  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE user_command_0100 INPUT.

  CASE gd_okcode.
    WHEN 'BACK' OR
         'END'  OR
         'CANC'.
      SET SCREEN 0. LEAVE SCREEN.



    WHEN OTHERS.
      " Check for changed data at the frontend (= grid control)
      go_grid->check_changed_data( ).



  ENDCASE.

  CALL METHOD go_grid->refresh_table_display
*    EXPORTING
*      IS_STABLE      =
*      I_SOFT_REFRESH =
    EXCEPTIONS
      finished       = 1
      OTHERS         = 2.
  IF sy-subrc <> 0.
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.


  CLEAR: gd_okcode.

ENDMODULE.                 " USER_COMMAND_0100  INPUT


*&---------------------------------------------------------------------*
*&      Form  INIT_CONTROLS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM init_controls .

* Create docking container
  CREATE OBJECT go_docking
    EXPORTING
      parent = cl_gui_container=>screen0
      ratio  = 90
    EXCEPTIONS
      OTHERS = 6.
  IF sy-subrc <> 0.
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.


* Create ALV grid
  CREATE OBJECT go_grid
    EXPORTING
      i_parent = go_docking
    EXCEPTIONS
      OTHERS   = 5.
  IF sy-subrc <> 0.
*   MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*              WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.

ENDFORM.                    " INIT_CONTROLS


*&---------------------------------------------------------------------*
*&      Form  BUILD_FIELDCATALOG
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM build_fieldcatalog.
* define local data
  DATA:
    ls_fcat        TYPE lvc_s_fcat,
    lt_fcat        TYPE lvc_t_fcat.

  REFRESH: gt_fcat.

  CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
    EXPORTING
*     I_BUFFER_ACTIVE              =
      i_structure_name             = 'VBAK'
*     I_CLIENT_NEVER_DISPLAY       = 'X'
*     I_BYPASSING_BUFFER           =
*     I_INTERNAL_TABNAME           =
    CHANGING
      ct_fieldcat                  = lt_fcat
    EXCEPTIONS
      inconsistent_interface       = 1
      program_error                = 2
      OTHERS                       = 3.
  IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.


* Only non-key fields are editable
  LOOP AT lt_fcat INTO ls_fcat.
    CASE ls_fcat-fieldname.
      WHEN 'NETWR'.
        ls_fcat-fieldname = 'SALES'.
        APPEND ls_fcat TO gt_fcat.

        ls_fcat-fieldname = 'SUM'.
        ls_fcat-scrtext_s = 'Sum'.
        ls_fcat-coltext   = 'Sum'.
        APPEND ls_fcat TO gt_fcat.

      WHEN 'WAERK'.
        ls_fcat-tech = 'X'.
        APPEND ls_fcat TO gt_fcat.

      WHEN 'ERDAT'.
        ls_fcat-no_out = 'X'.
        APPEND ls_fcat TO gt_fcat.

        ls_fcat-fieldname = 'PERIOD'.
        ls_fcat-scrtext_s = 'Period'.
        ls_fcat-scrtext_m = 'Period'.
        ls_fcat-scrtext_l = 'Period'.
        ls_fcat-reptext = 'Period'.
        ls_fcat-coltext   = 'Period'.
        APPEND ls_fcat TO gt_fcat.

      WHEN OTHERS.
        CONTINUE.
    ENDCASE.

  ENDLOOP.

  LOOP AT gt_fcat INTO ls_fcat.
    ls_fcat-col_pos = syst-tabix.

    MODIFY gt_fcat FROM ls_fcat.
  ENDLOOP.


ENDFORM.                    " BUILD_FIELDCATALOG


*&---------------------------------------------------------------------*
*&      Form  SET_LAYOUT_AND_VARIANT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM set_layout_and_variant .

  CLEAR: gs_layout,
         gs_variant.

  gs_layout-cwidth_opt = abap_true.

  gs_variant-report = syst-repid.
  gs_variant-handle = 'GRID'.

ENDFORM.                    " SET_LAYOUT_AND_VARIANT


*&---------------------------------------------------------------------*
*&      Form  FILL_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM fill_data .
* define local data
  DATA: ls_outtab   TYPE ty_s_outtab.

  ls_outtab-sales = '100'.
  ls_outtab-date  = '20080110'.

  ls_outtab-period = 'Jan-08'.  " determine from ls_outtab-DATE
  APPEND ls_outtab TO gt_outtab.


  ls_outtab-sales = '120'.
  ls_outtab-date  = '20080222'.

  ls_outtab-period = 'Feb-08'.
  APPEND ls_outtab TO gt_outtab.

  LOOP AT gt_outtab INTO ls_outtab.
    ls_outtab-sum = ls_outtab-sales.
    " add ls_outtab-expenses to ls_outtab-sum.
    " ...

    MODIFY gt_outtab FROM ls_outtab INDEX syst-tabix.
  ENDLOOP.

ENDFORM.                    " FILL_DATA

If the business users still need a transposed list then they can export it to Excel and make the transformation.

Regards

Uwe

Read only

0 Likes
1,092

Hi All,

Thank you for your replies.

what should be the structure of my Internal table for populating the Dynamic internal table.

i shd be getting

Sales for Oct , Nov, Dec..............................

like this for all the others.

so , my internal table will be

 month              text                          Value
         
         Oct                sales                          1000
         Nov                sales                          300
         Dec               sales                           400
 

Am i correct?

And one more thing- i created a dynamic internal table and passed on the dat to it

In the display , i am getting the first col with the text.

The problem i am facing is the other columns are also displaying the same text.

Should i created a Fieldcat also dynamically?

I am confused in this issue in designing the internal table to pass on the sat to the dynamic internal table.

Help me in solving this.

Thanks.

Vikki.

Read only

0 Likes
1,092

Hello Vikki

Since your list layout almost looks like my proposal you do not need any dynamic itab but just define all required values field in a structure including an additional field for the period (e.g. Nov-08) and perhaps a SUM field to summarize the values per row.

This structure can be used to create the fieldcatalog automatically which probably needs some refinement (texts, etc.) afterwards.

Assuming that you can have records for arbitrary dates there are just two things you need to do:

(1) Aggregate values per period (e.g. collect all values from 01/11/08 - 30/11/08 into a single record for period Nov-08)

(2) For each aggregated record create the period description (e.g. Nov-08).

That's all.

Regards

Uwe

Read only

Former Member
0 Likes
1,092

Hi,

U can create dynamic Internal table this way..

This is just a sample code....


DATA: struct_type TYPE REF TO cl_abap_structdescr,
      comp_tab    TYPE cl_abap_structdescr=>component_table,
      dref        TYPE REF TO data,
      itab_type   TYPE REF TO cl_abap_tabledescr,
      wa_comp     LIKE LINE OF comp_tab,
      index       TYPE char10.

FIELD-SYMBOLS : <tab> TYPE STANDARD TABLE,
                <wa> TYPE ANY.

PARAMETER col TYPE i.

DO col TIMES.
  index = sy-index.
  CONCATENATE 'COL' index INTO wa_comp-name.    "Field name
  CONDENSE wa_comp-name NO-GAPS.
  wa_comp-type ?= cl_abap_datadescr=>describe_by_name( 'I' ).     "Data type of Fields...
  APPEND  wa_comp TO comp_tab.
ENDDO.

struct_type = cl_abap_structdescr=>create( comp_tab ).
itab_type   = cl_abap_tabledescr=>create( struct_type ).
CREATE DATA dref TYPE HANDLE itab_type.
ASSIGN dref->* TO <tab>.      " Internal Table
CREATE DATA dref TYPE HANDLE struct_type.
ASSIGN dref->* TO <wa>.      " Work Area..

Read only

Former Member
0 Likes
1,092

Hi See this blog,

we can not give exact sol, so try to get an idea from this.

Amresh.

Read only

Former Member
0 Likes
1,092

Hello,

Tre in this way

first identify the total no of records then create the field catalog

DO y_lv_cntr_max TIMES.

y_v_charno = y_v_charno + 1.

CONCATENATE 'SALES' '-'

y_v_charno

INTO y_wa_fcat-fieldname.

CONCATENATE text-037

y_v_charno

INTO y_wa_fcat-scrtext_m .

APPEND y_wa_fcat TO y_i_fcat.

CLEAR y_wa_fcat.

y_wa_fcat-ref_field = 'CLASS'.

y_wa_fcat-ref_table = 'KLAH'.

CONCATENATE 'CLASS' '-'

y_v_charno

INTO y_wa_fcat-fieldname.

CONCATENATE text-038

y_v_charno

INTO y_wa_fcat-scrtext_m .

APPEND y_wa_fcat TO y_i_fcat.

CLEAR y_wa_fcat.

FIELD-SYMBOLS : <y_fld> TYPE ANY.

DATA: y_lv_comp TYPE string.

  • create dynamic internal table and assign to fs

    • CREATE OBJECT y_obj.

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING

it_fieldcatalog = y_i_fcat

IMPORTING

ep_table = dy_table.

ASSIGN dy_table->* TO <dyn_table>.

  • create dynamic work area and assign to fs

CREATE DATA dy_line LIKE LINE OF <dyn_table>.

ASSIGN dy_line->* TO <dyn_wa>.

CLEAR y_v_charno .

SORT y_i_out BY objnr.

LOOP AT y_i_final_output INTO y_wa_final_output.

y_v_charno = y_v_charno + 1.

CONCATENATE 'ATNAM' '-'

y_v_charno

INTO y_lv_comp.

ASSIGN COMPONENT y_lv_comp OF STRUCTURE <dyn_wa> TO <y_fld> .

<y_fld> = y_wa_out-atnam.

CONCATENATE 'CLASS' '-'

y_v_charno

INTO y_lv_comp.

ASSIGN COMPONENT y_lv_comp OF STRUCTURE <dyn_wa> TO <y_fld> .

<y_fld> = y_wa_out-class.

Endloop,

APPEND <dyn_wa> TO <dyn_table>.

FREE: <dyn_wa>, <y_fld> .

CLEAR y_v_charno.

LOOP AT y_i_fcat INTO y_wa_fcat.

MOVE: y_wa_fcat-fieldname TO y_wa_fieldcat-fieldname,

y_wa_fcat-tabname TO y_wa_fieldcat-tabname,

y_wa_fcat-col_pos TO y_wa_fieldcat-col_pos,

y_wa_fcat-scrtext_m TO y_wa_fieldcat-seltext_m,

y_wa_fcat-datatype TO y_wa_fieldcat-datatype,

y_wa_fcat-intlen TO y_wa_fieldcat-intlen,

y_wa_fcat-decimals_o TO y_wa_fieldcat-decimals_out.

APPEND y_wa_fieldcat TO y_i_fieldcat.

ENDLOOP. "LOOP AT y_i_fcat INTO y_wa_fcat.