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
792

Hi All,

I want to create an ALV report in which columns in the output will depend on the input parameters. For example: in the selection screen there is a field COST CENTER, so if a user enters 5 different cost enters then in the output I want 5 different columns and data is displayed against each columns.

I am able to get dynamic columns but I am not able to populate the internal table.

Does anybody have idea if this type of report?????

Warm Regards,

N.Jain

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
763

i did something like this but quite complex.

i created another program itself from the abap program and then executed it using the submit command.

sample code : please modify according to your requirement.

TYPES: BEGIN OF typ_inctabl ,

line(72),

END OF typ_inctabl.

DATA : itab_inctabl TYPE TABLE OF typ_inctabl,

wa_inctabl TYPE typ_inctabl.

**dynamic report

wa_inctabl-line = 'Program zduplicata_report line-size 600.'.

APPEND wa_inctabl TO itab_inctabl.

wa_inctabl-line = 'type-pools: slis.'.

APPEND wa_inctabl TO itab_inctabl.

**data declarations for dynamic program.

wa_inctabl-line = 'data : lv_col_index_qty type i,'.

APPEND wa_inctabl TO itab_inctabl.

wa_inctabl-line = ' lv_col_index_val type i.'.

APPEND wa_inctabl TO itab_inctabl.

*wa_inctabl-line = 'data : lv_qty_chr(13) type c, lv_val_chr(13) type c.'.

*append wa_inctabl to itab_inctabl.

**variables

wa_inctabl-line = 'data : p_bukrs_string type string'.

APPEND wa_inctabl TO itab_inctabl.

wa_inctabl-line = 'p_currency type string, p_lifnr type lifnr,'.

APPEND wa_inctabl TO itab_inctabl.

wa_inctabl-line = 'lv_date_interval type string, p_unit type string.'.

APPEND wa_inctabl TO itab_inctabl.

**alv declarations

wa_inctabl-line = 'data : lt_fieldcat type SLIS_T_FIELDCAT_ALV,'.

APPEND wa_inctabl TO itab_inctabl.

wa_inctabl-line = 'wa_fieldcat like line of lt_fieldcat,'.

APPEND wa_inctabl TO itab_inctabl.

wa_inctabl-line = 'wa_layout type SLIS_LAYOUT_ALV, lt_events type slis_t_event.'.

APPEND wa_inctabl TO itab_inctabl.

create the dynamic internal table

wa_inctabl-line = 'types : begin of typ_dyntab,'.

APPEND wa_inctabl TO itab_inctabl.

wa_inctabl-line = 'lifnr type lfa1-lifnr,'.

APPEND wa_inctabl TO itab_inctabl.

wa_inctabl-line = 'name1(40) type c,'.

APPEND wa_inctabl TO itab_inctabl.

wa_inctabl-line = 'ebeln(10) type c,'.

APPEND wa_inctabl TO itab_inctabl.

**now dynamic number of columns

do n times.

concatenate 'f' sy-index 'type c,' into wa_inctabl-line separated by space.

APPEND wa_inctabl TO itab_inctabl.

enddo.

wa_inctabl-line = 'end of typ_dyntab.'.

APPEND wa_inctabl TO itab_inctabl.

wa_inctabl-line = ' data : itab_dyntab type table of typ_dyntab,'.

APPEND wa_inctabl TO itab_inctabl.

wa_inctabl-line = 'wa_dyntab type typ_dyntab.'.

APPEND wa_inctabl TO itab_inctabl.

**populate table similarly

do n times.

wa_inctabl-line = 'wa_dyntab-lifnr = '.

concatenate wa_inctabl-line <your value> '.' into wa_inctabl-line.

APPEND wa_inctabl TO itab_inctabl.

wa_inctabl-line = 'append wa_dyntab to itab_dyntab.'.

APPEND wa_inctabl TO itab_inctabl.

enddo.

see if this helps you...i have just put a sample code...you need to improve it for your requirement.

regards,

Priyank

5 REPLIES 5
Read only

p291102
Active Contributor
0 Likes
763

REPORT YMS_DYNAMICALV.

TYPE-POOLS: SLIS.

FIELD-SYMBOLS: <DYN_TABLE> TYPE STANDARD TABLE,

<DYN_WA>.

DATA: ALV_FLDCAT TYPE SLIS_T_FIELDCAT_ALV,

IT_FLDCAT TYPE LVC_T_FCAT.

SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.

PARAMETERS: P_FLDS(5) TYPE C.

SELECTION-SCREEN END OF BLOCK B1.

START-OF-SELECTION.

  • build the dynamic internal table

PERFORM BUILD_DYN_ITAB.

  • write 5 records to the alv grid

DO 5 TIMES.

PERFORM BUILD_REPORT.

ENDDO.

  • call the alv grid.

PERFORM CALL_ALV.

************************************************************************

  • Build_dyn_itab

************************************************************************

FORM BUILD_DYN_ITAB.

DATA: NEW_TABLE TYPE REF TO DATA,

NEW_LINE TYPE REF TO DATA,

WA_IT_FLDCAT TYPE LVC_S_FCAT.

  • Create fields .

DO P_FLDS TIMES.

CLEAR WA_IT_FLDCAT.

WA_IT_FLDCAT-FIELDNAME = SY-INDEX.

WA_IT_FLDCAT-DATATYPE = 'CHAR'.

WA_IT_FLDCAT-INTLEN = 5.

APPEND WA_IT_FLDCAT TO IT_FLDCAT .

ENDDO.

  • Create dynamic internal table and assign to FS

CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE

EXPORTING

IT_FIELDCATALOG = IT_FLDCAT

IMPORTING

EP_TABLE = NEW_TABLE.

ASSIGN NEW_TABLE->* TO <DYN_TABLE>.

  • Create dynamic work area and assign to FS

CREATE DATA NEW_LINE LIKE LINE OF <DYN_TABLE>.

ASSIGN NEW_LINE->* TO <DYN_WA>.

ENDFORM. "build_dyn_itab

*********************************************************************

  • Form build_report

*********************************************************************

FORM BUILD_REPORT.

DATA: FIELDNAME(20) TYPE C.

DATA: FIELDVALUE(5) TYPE C.

DATA: INDEX(3) TYPE C.

FIELD-SYMBOLS: <FS1>.

DO P_FLDS TIMES.

INDEX = SY-INDEX.

  • Set up fieldvalue

CONCATENATE 'FLD' INDEX INTO

FIELDVALUE.

CONDENSE FIELDVALUE NO-GAPS.

  • <b> assign component index of structure <dyn_wa> to <fs1>.

  • <fs1> = fieldvalue.</b>

ENDDO.

  • Append to the dynamic internal table

APPEND <DYN_WA> TO <DYN_TABLE>.

ENDFORM. "build_report

************************************************************************

  • CALL_ALV

************************************************************************

FORM CALL_ALV.

DATA: WA_CAT LIKE LINE OF ALV_FLDCAT.

DO P_FLDS TIMES.

CLEAR WA_CAT.

WA_CAT-FIELDNAME = SY-INDEX.

WA_CAT-SELTEXT_S = SY-INDEX.

WA_CAT-OUTPUTLEN = '5'.

APPEND WA_CAT TO ALV_FLDCAT.

ENDDO.

  • Call ABAP List Viewer (ALV)

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

IT_FIELDCAT = ALV_FLDCAT

TABLES

T_OUTTAB = <DYN_TABLE>.

ENDFORM. "call_alv

Read only

Former Member
0 Likes
763

i´m not too sure if that will solve your problem, but if you are doing the columns dynamically, you should probably maintain the field catalog dynamically in the same way.

otherwise at least i wouldnt wonder too much about any errors on the way.

Read only

p291102
Active Contributor
0 Likes
763

REPORT  YMS_DYNAMICALV.

TYPE-POOLS: SLIS.

FIELD-SYMBOLS: <DYN_TABLE> TYPE STANDARD TABLE,
               <DYN_WA>.

DATA: ALV_FLDCAT TYPE SLIS_T_FIELDCAT_ALV,
      IT_FLDCAT TYPE LVC_T_FCAT.

SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001.
PARAMETERS: P_FLDS(5) TYPE C.
SELECTION-SCREEN END OF BLOCK B1.

START-OF-SELECTION.

* build the dynamic internal table
  PERFORM BUILD_DYN_ITAB.

* write 5 records to the alv grid
  DO 5 TIMES.
    PERFORM BUILD_REPORT.
  ENDDO.

* call the alv grid.
  PERFORM CALL_ALV.


************************************************************************
*  Build_dyn_itab
************************************************************************
FORM BUILD_DYN_ITAB.

  DATA: NEW_TABLE TYPE REF TO DATA,
        NEW_LINE  TYPE REF TO DATA,
        WA_IT_FLDCAT TYPE LVC_S_FCAT.

* Create fields .
  DO P_FLDS TIMES.
    CLEAR WA_IT_FLDCAT.
    WA_IT_FLDCAT-FIELDNAME = SY-INDEX.
    WA_IT_FLDCAT-DATATYPE = 'CHAR'.
    WA_IT_FLDCAT-INTLEN = 5.
    APPEND WA_IT_FLDCAT TO IT_FLDCAT .
  ENDDO.

* Create dynamic internal table and assign to FS
  CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
    EXPORTING
      IT_FIELDCATALOG = IT_FLDCAT
    IMPORTING
      EP_TABLE        = NEW_TABLE.

  ASSIGN NEW_TABLE->* TO <DYN_TABLE>.

* Create dynamic work area and assign to FS
  CREATE DATA NEW_LINE LIKE LINE OF <DYN_TABLE>.
  ASSIGN NEW_LINE->* TO <DYN_WA>.

ENDFORM.                    "build_dyn_itab

*********************************************************************
*      Form  build_report
*********************************************************************
FORM BUILD_REPORT.

  DATA: FIELDNAME(20) TYPE C.
  DATA: FIELDVALUE(5) TYPE C.
  DATA: INDEX(3) TYPE C.
  FIELD-SYMBOLS: <FS1>.

  DO P_FLDS TIMES.

    INDEX = SY-INDEX.

* Set up fieldvalue
    CONCATENATE 'FLD' INDEX INTO
             FIELDVALUE.
    CONDENSE   FIELDVALUE NO-GAPS.

*   <b> assign component  index  of structure <dyn_wa> to <fs1>.
*    <fs1> =  fieldvalue.</b>
  ENDDO.

* Append to the dynamic internal table
  APPEND <DYN_WA> TO <DYN_TABLE>.


ENDFORM.                    "build_report

************************************************************************
*  CALL_ALV
************************************************************************
FORM CALL_ALV.

  DATA: WA_CAT LIKE LINE OF ALV_FLDCAT.

  DO P_FLDS TIMES.
    CLEAR WA_CAT.
    WA_CAT-FIELDNAME = SY-INDEX.
    WA_CAT-SELTEXT_S = SY-INDEX.
    WA_CAT-OUTPUTLEN = '5'.
    APPEND WA_CAT TO ALV_FLDCAT.
  ENDDO.


* Call ABAP List Viewer (ALV)
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
    EXPORTING
      IT_FIELDCAT = ALV_FLDCAT
    TABLES
      T_OUTTAB    = <DYN_TABLE>.

ENDFORM.                    "call_alv

Read only

Former Member
0 Likes
764

i did something like this but quite complex.

i created another program itself from the abap program and then executed it using the submit command.

sample code : please modify according to your requirement.

TYPES: BEGIN OF typ_inctabl ,

line(72),

END OF typ_inctabl.

DATA : itab_inctabl TYPE TABLE OF typ_inctabl,

wa_inctabl TYPE typ_inctabl.

**dynamic report

wa_inctabl-line = 'Program zduplicata_report line-size 600.'.

APPEND wa_inctabl TO itab_inctabl.

wa_inctabl-line = 'type-pools: slis.'.

APPEND wa_inctabl TO itab_inctabl.

**data declarations for dynamic program.

wa_inctabl-line = 'data : lv_col_index_qty type i,'.

APPEND wa_inctabl TO itab_inctabl.

wa_inctabl-line = ' lv_col_index_val type i.'.

APPEND wa_inctabl TO itab_inctabl.

*wa_inctabl-line = 'data : lv_qty_chr(13) type c, lv_val_chr(13) type c.'.

*append wa_inctabl to itab_inctabl.

**variables

wa_inctabl-line = 'data : p_bukrs_string type string'.

APPEND wa_inctabl TO itab_inctabl.

wa_inctabl-line = 'p_currency type string, p_lifnr type lifnr,'.

APPEND wa_inctabl TO itab_inctabl.

wa_inctabl-line = 'lv_date_interval type string, p_unit type string.'.

APPEND wa_inctabl TO itab_inctabl.

**alv declarations

wa_inctabl-line = 'data : lt_fieldcat type SLIS_T_FIELDCAT_ALV,'.

APPEND wa_inctabl TO itab_inctabl.

wa_inctabl-line = 'wa_fieldcat like line of lt_fieldcat,'.

APPEND wa_inctabl TO itab_inctabl.

wa_inctabl-line = 'wa_layout type SLIS_LAYOUT_ALV, lt_events type slis_t_event.'.

APPEND wa_inctabl TO itab_inctabl.

create the dynamic internal table

wa_inctabl-line = 'types : begin of typ_dyntab,'.

APPEND wa_inctabl TO itab_inctabl.

wa_inctabl-line = 'lifnr type lfa1-lifnr,'.

APPEND wa_inctabl TO itab_inctabl.

wa_inctabl-line = 'name1(40) type c,'.

APPEND wa_inctabl TO itab_inctabl.

wa_inctabl-line = 'ebeln(10) type c,'.

APPEND wa_inctabl TO itab_inctabl.

**now dynamic number of columns

do n times.

concatenate 'f' sy-index 'type c,' into wa_inctabl-line separated by space.

APPEND wa_inctabl TO itab_inctabl.

enddo.

wa_inctabl-line = 'end of typ_dyntab.'.

APPEND wa_inctabl TO itab_inctabl.

wa_inctabl-line = ' data : itab_dyntab type table of typ_dyntab,'.

APPEND wa_inctabl TO itab_inctabl.

wa_inctabl-line = 'wa_dyntab type typ_dyntab.'.

APPEND wa_inctabl TO itab_inctabl.

**populate table similarly

do n times.

wa_inctabl-line = 'wa_dyntab-lifnr = '.

concatenate wa_inctabl-line <your value> '.' into wa_inctabl-line.

APPEND wa_inctabl TO itab_inctabl.

wa_inctabl-line = 'append wa_dyntab to itab_dyntab.'.

APPEND wa_inctabl TO itab_inctabl.

enddo.

see if this helps you...i have just put a sample code...you need to improve it for your requirement.

regards,

Priyank

Read only

Former Member
0 Likes
763

thanks for your valuable replies.