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: 

ALV report with dynamic columns, and repeated structure rows

former_member192818
Active Participant
0 Kudos

Hey Guys,

I've done some ALV programming, but most of the reports were straight forward. This one is a little interesting. So here go the questions...

Q1: Regarding Columns:

What is the best way to code a report with columns being dynamic. This is one of the parameters the user is going to enter in his input.

Q2: Regarding Rows:

I want to repeat a structure(say it contains f1, f2, f3) multiple time in rows. What is the best way to do it? The labels for these fields have to appear in the first column.

Below is the visual representation of the questions.

-


Jan 06 , Feb 06, Mar 06....(dynamic)

material 1

Current Stock

current required

$Value of stock

material 2

Current Stock

current required

$Value of stock

material 3

Current Stock

current required

$Value of stock

.

.

.

Thanks for your help.

Sumit.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hello,

Go ahead in using the below code :

*Data declaration

DATA : dref TYPE REF TO data.

FIELD-SYMBOLS : <g_t_output> TYPE table.

**Method for getting the reference for the structure

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING it_fieldcatalog = g_t_fieldcat

IMPORTING ep_table = dref.

**Passing the reference of the structure to field-symbol of type table

ASSIGN dref->* TO <g_t_output>.

Populate the field catalog first, and then the above two statements can be used.

Award some points, if found useful.

Regards,

Bharadwaja R

14 REPLIES 14

former_member192818
Active Participant
0 Kudos

The above visualization does not look that good. Here is a better one.

-


Jan 06 | Feb 06 | Mar 06 | Apr 06....(dynamic)

material 1

Current Stock

current required

$Value of stock

material 2

Current Stock

current required

$Value of stock

material 3

Current Stock

current required

$Value of stock

.

.

.

0 Kudos

Sumit,

Take a look at these weblogs

/people/ravikumar.allampallam/blog/2005/05/31/expand-the-list-of-columns-in-a-report-dynamically

/people/subramanian.venkateswaran2/blog/2004/11/19/dynamic-internal-table

Regards,

Ravi

Note : Please mark the helpful answers

0 Kudos

Hey Ravi,

Thank you for the replies.

Both your replies are extremely helpful.

I see you are extremely influential in SDN. Congratulations on all the hard work.

Sumit.

0 Kudos

This message was moderated.

0 Kudos

Hi Sumit,

I also want the same report format in ALV. Can you help me? I am a fresher in ABAP.

Thanks in advance,

Regards,

Archana

hymavathi_oruganti
Active Contributor
0 Kudos

for dynamic columns, i am explaining u how to create with an exmaple, check it,

suppose itab has fields A, B, C.

SO ITAB-C HAS THE VALUES OF C. NOW WE WANT A TABLE WITH VALUES OF C RIGHT!!.

NOW BUILD A FIELDCATALOG LIKE BELOW.

LOOP AT ITAB.

LS_FIELDCAT-FIELDNAME = ITAB-C.

LS_FIELDCAT-COL_TEXT = ITAB_C.

APPEND LS_FIELDCAT TO LT_FIELDCAT.

ENDLOOP.

DATA: DREF TYPE REF TO DATA.

FIELD-SYMBOLS: <TEMP_TAB> TYPE ANY.

CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE

EXPORTING

IT_FIELDCATALOG = LT_LVCFIELDCAT

IMPORTING

EP_TABLE = DREF.

ASSIGN dref->* TO <TEMP_TAB>.

<TEMP_TAB> IS THE REQUIRED DYNAMIC INTERNAL TABLE.

NOW IF U WANT TO FILL THE DYNAMIC INTERNAL TABLE.

DO AS BELOW..

DATA: WA_DREF TYPE REF TO DATA.

FIELD-SYMBOLS: <WA_TAB> TYPE ANY.

CREATE DATA WA_DREF LIKE LINE OF <TEMP_TAB>.

ASSIGN WA_DREF->* TO <WA_TAB>.

FIELD-SYMBOLS: <FS1>, <FS2>.

LOOP AT ITAB.

ASSIGN COMPONENT ITAB-C OF STRUCTURE <WA_TAB> TO <FS1>.

<FS1> = {SOME VALUE}.

APPEND <WA_TAB> TO <TEMP_TAB>.

ENDLOOP.

2) regarding the seond issue,

u can just trickily display the look as structure though u cannot bring the whole structure at a time,

apped blanks to the other field of itab and fill only a field with the structure values, so that the display looks as if a structure

example:

DATA: LS_FIELDCAT TYPE SLIS_FIELDCAT_ALV,

LT_FIELDCAT TYPE TABLE OF SLIS_FIELDCAT_ALV,

TYPES:begin of itab,

A ,

C ,

end of itab.

DATA: ITAB1 TYPE TABLE OF ITAB,

WA LIKE LINE OF itab1.

DATA VAR.

WA-A = 1.

WA-C = 2.

APPEND WA TO ITAB1.

WA-A = ''.

WA-C = 3.

APPEND WA TO ITAB1.

LS_FIELDCAT-FIELDNAME = 'A'.

LS_FIELDCAT-SELTEXT_S = 'A'.

APPEND LS_FIELDCAT TO LT_FIELDCAT.

LS_FIELDCAT-FIELDNAME = 'C'.

LS_FIELDCAT-SELTEXT_S = 'C'.

APPEND LS_FIELDCAT TO LT_FIELDCAT.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

I_CALLBACK_PROGRAM = SY-REPID

IT_FIELDCAT = LT_FIELDCAT[]

TABLES

T_OUTTAB = ITAB1.

u just copy and paste the above program and run it, u can understand what iam trying to explain

Message was edited by: Hymavathi Oruganti

0 Kudos

Hey Hymavathi,

Thank you for the code samples.

I am using Ravi Allapalam's code for now. I have read through your code and appreciate your help.

Regards.

Sumit.

Former Member
0 Kudos

HI

GOOD

ANS OF FIRST Q->

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.

THANKS

MRUTYUN

0 Kudos

Mrutyunjaya,

Thank you for outlining the steps. It is very helpful. Appreciate your help.

Best.

Sumit.

Former Member
0 Kudos

Hello,

Go ahead in using the below code :

*Data declaration

DATA : dref TYPE REF TO data.

FIELD-SYMBOLS : <g_t_output> TYPE table.

**Method for getting the reference for the structure

CALL METHOD cl_alv_table_create=>create_dynamic_table

EXPORTING it_fieldcatalog = g_t_fieldcat

IMPORTING ep_table = dref.

**Passing the reference of the structure to field-symbol of type table

ASSIGN dref->* TO <g_t_output>.

Populate the field catalog first, and then the above two statements can be used.

Award some points, if found useful.

Regards,

Bharadwaja R

0 Kudos

Hey Raju,

Thank you for the code snippet. It is very helpful.

Best Regards

Sumit.

former_member188685
Active Contributor
0 Kudos

Hi Sumit,

Just check this sample from one of the SAP site

ABAP Code Sample for Dynamic Table for ALV with Cell Coloring

Applies To:

ABAP / ALV Grid

Article Summary

ABAP Code Sample that uses dynamic programming techniques to build a dynamic internal table for display in an ALV Grid with Cell Coloring.

Code Sample

REPORT zcdf_dynamic_table.

* Dynamic ALV Grid with Cell Coloring.

* Build a field catalog dynamically and provide the ability to color
* the cells.

* To test, copy this code to any program name and create screen 100
* as described in the comments. After the screen is displayed, hit
* enter to exit the screen.

* Tested in 4.6C and 6.20

* Charles Folwell - cfolwell@csc.com - Feb 2, 2005

DATA:
r_dyn_table TYPE REF TO data,
r_wa_dyn_table TYPE REF TO data,
r_dock_ctnr TYPE REF TO cl_gui_docking_container,
r_alv_grid TYPE REF TO cl_gui_alv_grid,

t_fieldcat1 TYPE lvc_t_fcat, "with cell color
t_fieldcat2 TYPE lvc_t_fcat, "without cell color

wa_fieldcat LIKE LINE OF t_fieldcat1,
wa_cellcolors TYPE LINE OF lvc_t_scol,
wa_is_layout TYPE lvc_s_layo.

FIELD-SYMBOLS:
<t_dyn_table> TYPE STANDARD TABLE,
<wa_dyn_table> TYPE ANY,
<t_cellcolors> TYPE lvc_t_scol,
<w_field> TYPE ANY.
START-OF-SELECTION.

* Build field catalog based on your criteria.

wa_fieldcat-fieldname = 'FIELD1'.
wa_fieldcat-inttype = 'C'.
wa_fieldcat-outputlen = '10'.
wa_fieldcat-coltext = 'My Field 1'.
wa_fieldcat-seltext = wa_fieldcat-coltext.

APPEND wa_fieldcat TO t_fieldcat1.

wa_fieldcat-fieldname = 'FIELD2'.
wa_fieldcat-inttype = 'C'.
wa_fieldcat-outputlen = '10'.
wa_fieldcat-coltext = 'My Field 2'.
wa_fieldcat-seltext = wa_fieldcat-coltext.

APPEND wa_fieldcat TO t_fieldcat1.

* Before adding cell color table, save fieldcatalog to pass
* to ALV call. The ALV call needs a fieldcatalog without
* the internal table for cell coloring.

t_fieldcat2[] = t_fieldcat1[].

* Add cell color table.
* CALENDAR_TYPE is a structure in the dictionary with a
* field called COLTAB of type LVC_T_SCOL. You can use
* any structure and field that has the type LVC_T_SCOL.

wa_fieldcat-fieldname = 'T_CELLCOLORS'.
wa_fieldcat-ref_field = 'COLTAB'.
wa_fieldcat-ref_table = 'CALENDAR_TYPE'.

APPEND wa_fieldcat TO t_fieldcat1.

* Create dynamic table including the internal table
* for cell coloring.

CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
it_fieldcatalog = t_fieldcat1
IMPORTING
ep_table = r_dyn_table
EXCEPTIONS
generate_subpool_dir_full = 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.

* Get access to new table using field symbol.

ASSIGN r_dyn_table->* TO <t_dyn_table>.

* Create work area for new table.

CREATE DATA r_wa_dyn_table LIKE LINE OF <t_dyn_table>.

* Get access to new work area using field symbol.

ASSIGN r_wa_dyn_table->* TO <wa_dyn_table>.

* Get data into table from somewhere. Field names are
* known at this point because field catalog is already
* built. Read field names from the field catalog or use
* COMPONENT <number> in a DO loop to access the fields. A
* simpler hard coded approach is used here.

ASSIGN COMPONENT 'FIELD1' OF STRUCTURE <wa_dyn_table> TO <w_field>.

<w_field> = 'ABC'.

ASSIGN COMPONENT 'FIELD2' OF STRUCTURE <wa_dyn_table> TO <w_field>.

<w_field> = 'XYZ'.

APPEND <wa_dyn_table> TO <t_dyn_table>.

ASSIGN COMPONENT 'FIELD1' OF STRUCTURE <wa_dyn_table> TO <w_field>.

<w_field> = 'TUV'.

ASSIGN COMPONENT 'FIELD2' OF STRUCTURE <wa_dyn_table> TO <w_field>.

<w_field> = 'DEF'.

APPEND <wa_dyn_table> TO <t_dyn_table>.

* Color cells based on your criteria. In this example, a test on
* FIELD2 is used to decide on color.

LOOP AT <t_dyn_table> INTO <wa_dyn_table>.

ASSIGN COMPONENT 'FIELD2' OF STRUCTURE <wa_dyn_table> TO <w_field>.

* Get access to internal table used to color cells.

ASSIGN COMPONENT 'T_CELLCOLORS'
OF STRUCTURE <wa_dyn_table> TO <t_cellcolors>.

CLEAR wa_cellcolors.

wa_cellcolors-fname = 'FIELD2'.

IF <w_field> = 'DEF'.
wa_cellcolors-color-col = '7'.
ELSE.
wa_cellcolors-color-col = '5'.
ENDIF.

APPEND wa_cellcolors TO <t_cellcolors>.

MODIFY <t_dyn_table> FROM <wa_dyn_table>.

ENDLOOP.


* Display screen. Define screen 100 as empty, with next screen
* set to 0 and flow logic of:
*
* PROCESS BEFORE OUTPUT.
* MODULE initialization.
*
* PROCESS AFTER INPUT.

CALL SCREEN 100.

*---------------------------------------------------------------------*
* MODULE initialization OUTPUT
*---------------------------------------------------------------------*

MODULE initialization OUTPUT.

* Set up for ALV display.

IF r_dock_ctnr IS INITIAL.

CREATE OBJECT r_dock_ctnr
EXPORTING
side = cl_gui_docking_container=>dock_at_left
ratio = '90'.

CREATE OBJECT r_alv_grid
EXPORTING i_parent = r_dock_ctnr.

* Set ALV controls for cell coloring table.

wa_is_layout-ctab_fname = 'T_CELLCOLORS'.

* Display.

CALL METHOD r_alv_grid->set_table_for_first_display
EXPORTING
is_layout = wa_is_layout
CHANGING
it_outtab = <t_dyn_table>
it_fieldcatalog = t_fieldcat2.

ELSE. "grid already prepared

* Refresh display.

CALL METHOD r_alv_grid->refresh_table_display
EXPORTING
i_soft_refresh = ' '
EXCEPTIONS
finished = 1
OTHERS = 2.

ENDIF.

ENDMODULE. " initialization OUTPUT

Regards

vijay

0 Kudos

Hey Vijay,

Thank you for the code snippet. Is very helpful.

Best Regards

Sumit.

rosenberg_eitan
Active Contributor
0 Kudos

Hi ,

I believe that "dynamic" tables should be used as last resort.

What is the maximum numbres of colums that are to be expected ? How many monthes .

Here is a report that the only thing that is dynamic is the field catalog that expose the relevant columns.

http://scn.sap.com/message/14337353#14337353

http://scn.sap.com/servlet/JiveServlet/download/14337353-185507/y_r_eitan_test_06_03.txt.zip

Full source is included.

It can handel up to 053 periods (Weeks in this sampel)

Regards.